473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

query

15
hi,
i faced a problem when i tried to retrive value from two table using a common value.
ie
table1 has emp_id and name,table2 has emp_id and prod..
i tried to use the query
select name,prod from table1,table2 where table1.emp_id='some value';
The problem was there was redundancy of data in table2 and using distinct key word also i got unwanted values.
ie.
select distinct name,prod from table1,table2 where table1.emp_id='some value';
i got all the values of prod.
what kind of query i should use to retrive value from both the table to avoid redundant and unwanted data.
Mar 5 '08 #1
11 1739
amitpatel66
2,367 Expert 2GB
Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT t1.empid,t1.name,t2.prod FROM table1 t1, table2 t2 WHERE t1.empid = t2.empid
  3.  
  4.  
Mar 5 '08 #2
debasisdas
8,127 Expert 4TB
To avoid redundancy of data in JOIN, you need to qualify the field names by the table name.
Mar 6 '08 #3
sinner
15
Thanks for the query its work fine.
i faced a problem when i tried to know the number of products sold by a particular emp_id
ie
select t1.name,count(prod) from t1,t2 group t2.id;

The error was not a GROUP BY expression.
Mar 6 '08 #4
sinner
15
Thanks for the query its work fine.
i faced a problem when i tried to know the number of products sold by a particular emp_id
ie
select t1.name,count(prod) from t1,t2 group t2.id;

The error was not a GROUP BY expression.
what can i do for retriving name and product count from the table.
Mar 6 '08 #5
amitpatel66
2,367 Expert 2GB
Thanks for the query its work fine.
i faced a problem when i tried to know the number of products sold by a particular emp_id
ie
select t1.name,count(prod) from t1,t2 group t2.id;

The error was not a GROUP BY expression.
what can i do for retriving name and product count from the table.
Try This:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT t1.name,x.cnt Prod_Count FROM
  3. (SELECT t2.id, COUNT(t2.prod) cnt FROM table2 t2 GROUP BY t2.id) x, table1 t1 WHERE t1.id = x.id
  4.  
  5.  
Mar 6 '08 #6
sinner
15
Try This:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT t1.name,x.cnt Prod_Count FROM
  3. (SELECT t2.id, COUNT(t2.prod) cnt FROM table2 t2 GROUP BY t2.id) x, table1 t1 WHERE t1.id = x.id
  4.  
  5.  

Thanks amitpatel for the code.It is working fine.
Mar 6 '08 #7
amitpatel66
2,367 Expert 2GB
Thanks amitpatel for the code.It is working fine.

You are Welcome. Do post back in case of any further issues
Mar 6 '08 #8
sinner
15
You are Welcome. Do post back in case of any further issues
hi,
i used a query to retrive the top product sold by a particular user by the following

query:
select tab1.name,x.cnt post from (SELECT tab2.id,count(tab2.post) cnt from tab2 group by tab2.id ) x,tab1 where tab1.id = x.id order by post desc;

it works fine.
can i get get more feesable query to retrive the value.
Mar 6 '08 #9
amitpatel66
2,367 Expert 2GB
hi,
i used a query to retrive the top product sold by a particular user by the following

query:
select tab1.name,x.cnt post from (SELECT tab2.id,count(tab2.post) cnt from tab2 group by tab2.id ) x,tab1 where tab1.id = x.id order by post desc;

it works fine.
can i get get more feesable query to retrive the value.
There are many ways of doing it. What you tried is also fine and simple.
Mar 6 '08 #10
sinner
15
There are many ways of doing it. What you tried is also fine and simple.
thank amit .
can you tell me the otherways so that i could get the knowledge of that too.
Mar 7 '08 #11
amitpatel66
2,367 Expert 2GB
thank amit .
can you tell me the otherways so that i could get the knowledge of that too.
Expand|Select|Wrap|Line Numbers
  1.  
  2. select tab1.name,tab2.id,(SELECT count(*) from tab2 WHERE id = tab2.id) post FROM  table1 tab1,table2 tab2 where tab1.id = tab2.id order by post desc;
  3.  
  4. select tab1.name, x.id,x.cnt FROM
  5. (SELECT id,ROW_NUMBER(PARTITION BY id ORDER BY COUNT(prod) DESC) cnt FROM table2 GROUP BY id) x, tanle1 tab1
  6. WHERE tab1.id = x.id
  7.  
  8.  
Mar 7 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
3
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
4
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets...
14
by: Dave Thomas | last post by:
If I have a table set up like this: Name | VARCHAR Email | VARCHAR Age | TINYINT | NULL (Default: NULL) And I want the user to enter his or her name, email, and age - but AGE is optional. ...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
4
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.