473,406 Members | 2,281 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,406 software developers and data experts.

how to write the select query for this situation

Hi all
I have a table in which i have following values

id name grade city

1 aaa A hyd
2 bbb B che
3 ccc C del
4 ddd A hyd
5 eee C che
6 fff B che
7 ggg A hyd
8 hhh B che
9 iii A hyd
10 jjj C ban


Now i want to write a qurey which should give me the result as

all the name which are having grade and city combination as same

in this case o/p is

1 aaa A hyd
4 ddd A hyd
7 ggg A hyd
2 bbb B che
6 fff B che
8 hhh B che


how to write the query ... please someone help
Mar 13 '07 #1
1 1839
michaelb
534 Expert 512MB
Let's consider this table
Expand|Select|Wrap|Line Numbers
  1. select * from game;
  2.  
  3.  id |  name  | grade |    city     
  4. ----+--------+-------+-------------
  5.   1 | john   | A     | boston
  6.   2 | mike   | A     | boston
  7.   3 | leo    | B     | LA
  8.   4 | vik    | C     | LA
  9.   5 | carrie | C     | NY
  10.   6 | john   | A     | cleveland
  11.   7 | mary   | B     | boston
  12.   8 | van    | B     | boston
  13.   9 | rose   | B     | springfield
  14.  10 | dan    | C     | NY
  15.  11 | mark   | C     | boston
  16.  12 | linda  | B     | NY
  17.  13 | max    | C     | NY
  18. (13 rows)
  19.  
In order to get what we need we will use the GROUP BY clause:
Expand|Select|Wrap|Line Numbers
  1. SELECT grade, city from game GROUP BY grade, city HAVING COUNT(*) > 1;
  2.  
  3.  grade |  city  
  4. -------+--------
  5.  A     | boston
  6.  B     | boston
  7.  C     | NY
  8. (3 rows)
  9.  
This gives us some idea on which combinations of grade and city should be included in the output.
However this is not the result we need, but rather something we can use
for a subquery, or perhaps for a join condition:

Expand|Select|Wrap|Line Numbers
  1. SELECT G1.* from game G1 JOIN 
  2.  (select grade, city from game group by grade, city having count(*) > 1) AS G2
  3. using (grade, city) order by grade, city;
  4.  
  5.   id |  name  | grade |  city  
  6. ----+--------+-------+--------
  7.   1 | john   | A     | boston
  8.   2 | mike   | A     | boston
  9.   7 | mary   | B     | boston
  10.   8 | van    | B     | boston
  11.  13 | max    | C     | NY
  12.  10 | dan    | C     | NY
  13.   5 | carrie | C     | NY
  14. (7 rows)
  15.  
  16.  
The result above includes rows with the same grade and city.

I would suggest these links for further reading:

SELECT (group by)
Table Expressions
Aggregate Functions
Mar 14 '07 #2

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

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
0
by: Tanamon | last post by:
Hello All, I am a MySQL newbie trying to write a query that selects file_name records possessing the highest numbered version for that unique file_name. I show sample data and two trial queries...
1
by: Belmin | last post by:
I'm not sure if this might be a left join situation or what but Im trying to query and retrieve id, text, category id, number of comments on that id and the description for that category id. now...
3
by: jw56578 | last post by:
Which way of retrieving a record is more effecient?: Select tbl1.field1, tbl2.field1 from table1 tbl1 inner join table2 tbl2 on tbl1.id = tbl2.id where someid = somevalue and someid =...
12
by: TP | last post by:
Here is my problem. I need to display a table about which I have no information except the table name. Using metadata I can somehow show the column names and record values. But my table has 1...
19
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main...
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: MLH | last post by:
Suppose you're using a query to update or append many records in a large table. And suppose one of those fields is a T/D stamp table and you're using Now() to write its value. It is conceivable...
36
by: CK | last post by:
How do I write a set based query? I have a groupSets table with fields setId, idField, datasource, nameField, prefix, active Data: 1,someIDfield, someTable, someField, pre1, 1 2,someotherIDfield,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.