Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: How do you select only groups that do not contain a certain value?

Tags:

sql

group-by

Say I have a table:

Restaurant locations:   RESTAURANT_NO | RESTAURANT_LOCATION ----------------------------------- 1             |            City A 1             |            City B 2             |            City A 2             |            City B 2             |            City C 3             |            City C 4             |            City A 4             |            City B 

How would I be able to group them together and also only select the RESTAURANT_NO that do not have locations in city C?

Using this example, I want to return:

 RESTAURANT_NO  -------------  1  4 

Since RESTAURANT_NO 2 and 3 both have locations in city C.

I do not know how to group RESTAURANT_NO together while also trying only to select the groups that meet this requirement.

EDIT: I got this working.

However, there is one last thing that I still have not been able to figure out. The following table has the ID number of people along with cities they have worked in:

PERSON_NO | CITY_NAME --------------------- 1         |    City A 2         |    City B 3         |    City A 3         |    City B 3         |    City C 4         |    City A 4         |    City B 4         |    City C 

How would I be able to get the PERSON_NO of all the people who have lived in all three cities, A,B, and C?

I want to return

PERSON_NO --------- 3 4 

Thanks, again. I haven't had that much experience with SQL and so I'm not sure what to do.

like image 297
vesselll Avatar asked Feb 20 '12 01:02

vesselll


People also ask

Can we select column which is not part of GROUP BY in SQL?

The answer is yes, but only in a very certain way. Now, we want to add two more columns—let's call them other1 and other2—but we don't want to add them to the GROUP BY, because we still want only one row per foo in the results.

Can we use select * with GROUP BY?

Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause. The original idea was to create the table in beginning of the query, so the (SELECT * FROM #TBL) could be used on the query itself, instead of defining the names on each GROUP BY.

How do I exclude columns from a GROUP BY in SQL?

To avoid grouping by a specific column that returns multiple values, you can either remove it from the query, or you can explicitly tell it which value you want. You can do this using aggregate or analytic functions, like: MAX(ExtensionID) MIN(ExtensionID)

How do you exclude items in SQL?

The SQL EXCEPT operator is used to exclude like rows that are found in one query but not another. It returns rows that are unique to one result. To use the EXCEPT operator, both queries must return the same number of columns and those columns must be of compatible data types.


2 Answers

One way:

SELECT RESTAURANT_NO FROM restaurant WHERE RESTAURANT_NO NOT IN (SELECT RESTAURANT_NO FROM restaurant WHERE RESTAURANT_LOCATION = 'City C') 
like image 164
John Pick Avatar answered Oct 02 '22 18:10

John Pick


SELECT DISTINCT       Restaurant_no FROM        TableX t WHERE        NOT EXISTS       ( SELECT *         FROM TableX c         WHERE c.Restaurant_no = t.Restaurant_no           AND c.Restaurant_location = 'City C'       ) 
like image 39
ypercubeᵀᴹ Avatar answered Oct 02 '22 20:10

ypercubeᵀᴹ