Trying to figure out an issue that is causing my SQL Server query to return no results. I have a query which calls out where there is more than one unique 'requested delivery date' at a size level on a single PO. I do this using COUNTand DISTINCT. It works perfect until I add the fields 'PO_ITEM_NUMBER' and 'REQ_DELIV_DATE'- this was requested by the business. I am not sure why that would cause an issue - for reference our tier for PO tables is Header,Item,Size - size being the most granular and it is SAP based.
Query:
SELECT E.TEAM_MEMBER_NAME [EMPLOYEE],
H.PO_TYPE,
H.PO_ISSUE_DATE,
S.PO_NUMBER,
S.MATERIAL,
M.DESCRIPTION,
H.PO_ORDERED_QUANTITY [PO_QUANTITY], -- if you sue SUM(S.PO_ORDERED_QUANTITY - you get more results but wrong totals
K.BUSINESS_SEGMENT_DESC,
S.PO_REQ_DELIV_DATE,
S.PO_ITEM_NUMBER
FROM PDX_SAP_USER..VW_PO_SIZE S --- you can use .. insetead of .dbo.
JOIN ADI_USER_MAINTAINED..SCM_PO_Employee_Name E --- join the po to employee assigment table
ON S.PO_NUMBER = E.PO_NUMBER
JOIN PDX_SAP_USER..VW_PO_HEADER H
ON E.PO_NUMBER = H.PO_NUMBER
JOIN PDX_SAP_USER..VW_PO_ITEM I
ON E.PO_NUMBER = I.PO_NUMBER
JOIN PDX_SAP_USER..VW_MM_MATERIAL M
ON E.MATERIAL = M.MATERIAL
JOIN PDX_SAP_USER..vw_kd_BUSINESS_SEGMENT K
ON M.BUSINESS_SEGMENT_CODE = K.BUSINESS_SEGMENT_CODE
WHERE I.PO_BALANCE_QUANTITY > 0 ---exclude any fully received PO's
AND NOT EXISTS (SELECT * FROM VW_PO_ITEM I1 WHERE DEL_INDICATOR = 'L' AND I.PO_NUMBER = I1.PO_NUMBER)
GROUP BY S.PO_NUMBER,
E.TEAM_MEMBER_NAME,
H.PO_TYPE,
H.PO_ISSUE_DATE,
S.MATERIAL,
M.DESCRIPTION,
K.BUSINESS_SEGMENT_DESC,
H.PO_ORDERED_QUANTITY,
S.PO_REQ_DELIV_DATE,
S.PO_ITEM_NUMBER
HAVING COUNT(DISTINCT S.PO_REQ_DELIV_DATE) > 1
ORDER BY S.PO_NUMBER
Adding query that works along with the results:
SELECT E.TEAM_MEMBER_NAME [EMPLOYEE],
H.PO_TYPE,
CONVERT(VARCHAR(12),H.PO_ISSUE_DATE,101) [PO_ISSUE_DATE],
S.PO_NUMBER,
S.MATERIAL,
M.DESCRIPTION,
H.PO_ORDERED_QUANTITY [PO_QUANTITY], --- if you use SUM(S.PO_ORDERED_QUANTITY) - you get more results but wrong totals
K.BUSINESS_SEGMENT_DESC
FROM PDX_SAP_USER..VW_PO_SIZE S --- you can use .. insetead of .dbo.
JOIN ADI_USER_MAINTAINED..SCM_PO_Employee_Name E --- join the po to employee assigment table
ON S.PO_NUMBER = E.PO_NUMBER
JOIN PDX_SAP_USER..VW_PO_HEADER H
ON E.PO_NUMBER = H.PO_NUMBER
JOIN PDX_SAP_USER..VW_PO_ITEM I
ON E.PO_NUMBER = I.PO_NUMBER
JOIN PDX_SAP_USER..VW_MM_MATERIAL M
ON E.MATERIAL = M.MATERIAL
JOIN PDX_SAP_USER..vw_kd_BUSINESS_SEGMENT K
ON M.BUSINESS_SEGMENT_CODE = K.BUSINESS_SEGMENT_CODE
WHERE I.PO_BALANCE_QUANTITY > 0 ---exclude any fully received PO's
AND NOT EXISTS (SELECT * FROM VW_PO_ITEM I1 WHERE DEL_INDICATOR = 'L' AND I.PO_NUMBER = I1.PO_NUMBER)
GROUP BY S.PO_NUMBER,
E.TEAM_MEMBER_NAME,
H.PO_TYPE,
H.PO_ISSUE_DATE,
S.MATERIAL,
M.DESCRIPTION,
K.BUSINESS_SEGMENT_DESC,
H.PO_ORDERED_QUANTITY
HAVING COUNT(DISTINCT S.PO_REQ_DELIV_DATE) > 1
ORDER BY S.PO_NUMBER
Results:
EMPLOYEE PO_TYPE PO_ISSUE_DATE PO_NUMBER MATERIAL DESCRIPTION PO_QUANTITY BUSINESS_SEGMENT_DESC
------------------------------ ------- ------------- ---------- ------------------ ---------------------------------------- --------------------------------------- ----------------------------------------------------------------------------------------------------
Christopher Olson NB 01/19/2017 0282238419 CD7078 ESS 3S PANT WVN 2054 CORE APP MEN SPORT ADIDAS
Juan Gomez NB 02/23/2017 0282524995 S98775 ESS LIN P/O FT 103 CORE APP MEN SPORT ADIDAS
Christopher Olson NB 03/09/2017 0282598957 BK7410 ESS LGO T P SJ 619 ATHLETICS APP MEN ADIDAS
Juan Gomez NB 03/28/2017 0282706115 S97155 ESS LIN TIGHT 961 CORE APP WOMEN SPORT ADIDAS
Juan Gomez NB 09/21/2017 0283752965 CF8152 BOS LABEL 7900 ATHLETICS APP MEN ADIDAS
Julie Lange-May 12 10/02/2017 0283796594 DQ1421 WOVEN JACKET W 1020 ATHLETICS APP WOMEN ADIDAS
Kekai Ariola NB 10/10/2017 0283837426 AC7366 PW HU HOLI Tennis Hu MC 5655 STATEMENT FTW ADIDAS
Cody Lofquist NB 11/10/2017 0283944933 DB2061 PREDATOR TANGO 18.1 TR 1756 FOOTBALL FTW ADIDAS
Andrew Zapata 05 11/13/2017 0283961402 CG6440 NEMEZIZ 18.1 FG W 543 FOOTBALL FTW ADIDAS
Christopher Olson NB 11/20/2017 0283981666 CV7748 ASSITA 17 GK Y 1648 FOOTBALL APP GENERIC ADIDAS
Cody Lofquist NB 11/21/2017 0283984539 DB2165 COPA 18.1 FG 501 FOOTBALL FTW ADIDAS
Julie Lange-May NB 11/26/2017 0284043157 CE4368 I GRPHC STSET 1333 ORIGINALS APP KIDS ADIDAS
Trey Pflug NB 11/27/2017 0284048754 CQ3168 SOLAR BOOST M 3500 RUNNING FTW MEN ADIDAS
Dave Laws NB 11/28/2017 0284059045 DB2966 YEEZY 500 15334 YEEZY FTW ADIDAS
Dave Laws NB 11/28/2017 0284059047 DB2966 YEEZY 500 12584 YEEZY FTW ADIDAS
Christopher Olson NB 12/06/2017 0284094060 BJ9165 TASTIGO17 SHO W 7522 FOOTBALL APP GENERIC ADIDAS
Christopher Olson NB 12/06/2017 0284094212 BK0350 TIRO17 TRG PNTW 7091 FOOTBALL APP GENERIC ADIDAS
Cody Lofquist NB 12/08/2017 0284107301 DB2062 PREDATOR TANGO 18.1 TR 2110 FOOTBALL FTW ADIDAS
Trey Pflug NB 12/11/2017 0284115640 BC0674 SOLAR BOOST W 1752 RUNNING FTW WOMEN ADIDAS
Kim Moreland NB 12/12/2017 0284137355 DJ3033 D2M K SHT 1730 CORE APP WOMEN SPORT ADIDAS
Cody Lofquist NB 12/12/2017 0284141196 DB2126 PREDATOR TANGO 18.3 IN 1988 FOOTBALL FTW ADIDAS
Cody Lofquist NB 12/12/2017 0284141253 AQ0612 NEMEZIZ MESSI TANGO 18.3 TF 526 FOOTBALL FTW ADIDAS
Dave Laws NB 12/15/2017 0284170426 DB2966 YEEZY 500 2918 YEEZY FTW ADIDAS
Cody Lofquist NB 12/16/2017 0284174671 DB2248 X 18.1 FG 668 FOOTBALL FTW ADIDAS
Cody Lofquist NB 12/16/2017 0284174673 DB2039 PREDATOR 18.1 FG 489 FOOTBALL FTW ADIDAS
Christopher Olson ER 12/20/2017 0284207872 BS4250 TASTIGO17 SHO 404 FOOTBALL APP GENERIC ADIDAS
Ben Paul NB 12/19/2017 0284208137 CG0584 REAL A JSY 811 FOOTBALL APP LICENSED ADIDAS
Julie Lange-May NB 01/07/2018 0284316616 DN4273 UAS BEANIE 120 ORIGINALS APP MEN ADIDAS
Cody Lofquist NB 01/08/2018 0284319552 DB2063 PREDATOR TANGO 18.1 TR 2001 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464341 DB2214 X 18+ FG 582 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464343 DB2013 PREDATOR 18+ FG 2201 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464344 DB2072 NEMEZIZ 18+ FG 1467 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464346 DB2251 X 18.1 FG 620 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464348 DB2167 COPA 18.1 FG 1714 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464349 DB2089 NEMEZIZ MESSI 18.1 FG 988 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284464350 DB2040 PREDATOR 18.1 FG 2061 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/19/2018 0284465944 DB2001 PREDATOR 18.3 FG 7008 FOOTBALL FTW ADIDAS
Cody Lofquist NB 01/23/2018 0284489924 772109 SAMBA CLASSIC 419 FOOTBALL FTW ADIDAS
Andrew Zapata 05 02/02/2018 0284539184 DH3869 CLIMA 3.0 TEE 1853 ACTION SPORTS APP ADIDAS
Cody Lofquist NB 02/06/2018 0284550445 BB0571 Goletto VI FG J 5562 FOOTBALL FTW ADIDAS
Cody Lofquist NB 02/24/2018 0284666220 DM2092 MLS ASG OMB 424 FOOTBALL ACC HW ADIDAS
Christopher Olson NB 01/19/2018 0284666914 BP9111 D2M 3S SHORT 11811 CORE APP MEN SPORT ADIDAS
Cody Lofquist NB 02/27/2018 0284684097 019228 MUNDIAL TEAM 657 FOOTBALL FTW ADIDAS
Cody Lofquist NB 03/06/2018 0284704098 CD4683 GENERICWCBOX 80000 FOOTBALL ACC HW ADIDAS
Kekai Ariola NB 03/08/2018 0284728508 BB7619 Sobakov 1775 ORIGINALS FTW MEN ADIDAS
Kim Moreland NB 03/08/2018 0284730274 BP9733 ULT SS T 2557 TRAINING APP MEN ADIDAS
Kekai Ariola 05 03/27/2018 0284865999 B37532 EQT SUPPORT SK PK W 347 ORIGINALS FTW WOMEN ADIDAS
Kekai Ariola 05 03/27/2018 0284866000 B37545 EQT SUPPORT SK PK W 357 ORIGINALS FTW WOMEN ADIDAS
Kim Moreland NB 04/06/2018 0284914322 DH3591 Tech Tee 10042 TRAINING APP WOMEN ADIDAS
Cody Lofquist NB 04/10/2018 0284930265 CW5627 Pred FS JR MN 1651 FOOTBALL ACC HW ADIDAS
Kekai Ariola NB 04/10/2018 0284930449 B41794 PW TENNIS HU 315 ORIGINALS FTW MEN ADIDAS
(51 row(s) affected)
If you added HAVING clause recently then comment and check.
If not then change the INNER JOIN TO LEFT join from bottom to top one at a time and execute the query and check.
This way you can troubleshoot the issue easily.
Unless we have the sample data for all the tables you are used the query it will be difficult to answer.
WITH POsToReturn AS (
SELECT S.PO_NUMBER
FROM PDX_SAP_USER..VW_PO_SIZE S
JOIN PDX_SAP_USER..VW_PO_ITEM I
ON I.PO_NUMBER = S.PO_NUMBER
WHERE I.PO_BALANCE_QUANTITY > 0
GROUP BY S.PO_NUMBER
HAVING COUNT(DISTINCT S.PO_REQ_DELIV_DATE) > 1
AND COUNT(CASE WHEN I.DEL_INDICATOR = 'L' THEN 1 END) = 0
)
SELECT <your columns>
FROM POsToReturn P
JOIN PDX_SAP_USER..VW_PO_SIZE S
ON S.PO_NUMBER = P.PO_NUMBER
... <join the rest of the tables for the detail columns>
-- leave out the entire group by!
Or you could go with an approach like this:
with data as (
select
count(distinct S.PO_REQ_DELIV_DATE)
over (partition by S.PO_NUMBER) as rdd_count
<insert rest of main query>
)
select ... from data
where rdd_count > 1
order by S.PO_NUMBER;
As for understanding why, I think you've seen other queries where people added columns to a group by clause so they could get around a mysterious error message about non-aggregates. If not that then you've seen those systems that confusingly permit a non-standard behavior that gives results without erroring at all.
Usually what people want to accomplish is something like this: I'm grouping on Customer ID already but I want Customer Name in the results too. So they add that extra column to the 'group by' list and everything works fine. But if you think about it the reason is that the new column didn't change the groups at all since each Customer ID always has the same Customer Name anyway and it's ultimately just an easy way to get rid of the error. In your query that's not true though: you do have more than one date in all the groups you care about.
In my opinion it's better to use dummy aggregates like min(Customer Name) as CustomerName. Remember an aggregate function's purpose is collapsing multiple values into a single value. When necessary some systems will just pick a value at random, without warning you it did that. Many MySQL and Sybase developers got burned by this when they relied on this quirky behavior and/or never really learned how it's supposed to work.
Also remember that in general a group is a set of multiple rows collapsed into just one row. For count(distinct) to work it needed to see multiple dates across a single group. But that conflicts with your need to keep the output as separate rows.
Essentially the row by itself doesn't give you enough information to decide whether to keep it. I solved that by using a second query to figure a list of PO_Numbers meeting the criteria and then using that as a filter (via inner join.)
In the second example I used a window function instead, which lets you look outside each row without the row-collapsing behavior of group by. Both of the basically let you do what the having was intended to accomplish.
Related
Hello Everyone I have a problem…
Table 1 (sorted) is laid out like this:
User ID Producer ID Company Number
JWROSE 23401 234
KXPEAR 23903 239
LMWEEM 27902 279
KJMORS 18301 183
Table 2 (unsorted) looks like this:
Client Name City Company Number
Rajat Smith London JWROSE
Robert Singh Cleveland KXPEAR
Alberto Johnson New York City LMWEEM
Betty Lee Dallas KJMORS
Chase Galvez Houston 23401
Hassan Jackson Seattle 23903
Tooti Fruity Boise 27902
Joe Trump Tokyo 18301
Donald Biden Cairo 234
Mike Harris Rome 239
Kamala Pence Moscow 279
Adolf Washington Bangkok 183
Now… Table 1 has all of the User IDs and Producer IDs properly rowed with the Company Number.
I want to pull all the data and correctly sorted….
Client Name City User ID Producer ID Company Number
Rajat Smith London JWROSE 23401 234
Robert Singh Cleveland KXPEAR 23903 239
Alberto Johnson New York City LMWEEM 27902 279
Betty Lee Dallas KJMORS 18301 183
Chase Galvez Houston JWROSE 23401 234
Hassan Jackson Seattle KXPEAR 23903 239
Tooti Fruity Boise LMWEEM 27902 279
Joe Trump Tokyo KJMORS 18301 183
Donald Biden Cairo JWROSE 23401 234
Mike Harris Rome KXPEAR 23903 239
Kamala Pence Moscow LMWEEM 27902 279
Adolf Washington Bangkok KJMORS 18301 183
Query:
Select
b.client_name,
b.city.,
a.user_id,
a.producer_id,
a.company_number
From Table 1 A
Left Join Table 2 B On a.company….
And this is where I don’t know what do to….because both tables have all the same variables, but Company Number in Table 2 is mixed with User IDs and Producer IDs... however we know what company Number those ID's are associated to.
As I mention in the comments, and others do, the real problem is your design. "The fact that UserID is clearly a varchar, while the other 2 columns are an int really does not make this any better", and makes this not simple (and certainly not SARGable).
To get the data in the correct order, as well, you need a column to order it on which the data lacks. I have therefore added a pseudo column, MissingIDColumn, to represent this missing column you need to add to your data; which you can do when you fix the design:
SELECT T2.ClientName,
T2.City,
T1.UserID,
T1.ProducerID,
T1.CompanyNumber
FROM (VALUES('JWROSE',23401,234),
('KXPEAR',23903,239),
('LMWEEM',27902,279),
('KJMORS',18301,183))T1(UserID,ProducerID,CompanyNumber)
JOIN (VALUES(1,'Rajat Smith ','London ','JWROSE'),
(2,'Robert Singh ','Cleveland ','KXPEAR'),
(3,'Alberto Johnson ','New York City','LMWEEM'),
(4,'Betty Lee ','Dallas ','KJMORS'),
(5,'Chase Galvez ','Houston ','23401'),
(6,'Hassan Jackson ','Seattle ','23903'),
(7,'Tooti Fruity ','Boise ','27902'),
(8,'Joe Trump ','Tokyo ','18301'),
(9,'Donald Biden ','Cairo ','234'),
(10,'Mike Harris ','Rome ','239'),
(11,'Kamala Pence ','Moscow ','279'),
(12,'Adolf Washington','Bangkok ','183'))T2(MissingIDColumn,ClientName,City,CompanyNumber) ON T2.CompanyNumber IN (T1.UserID,CONVERT(varchar(6),T1.ProducerID),CONVERT(varchar(6),T1.CompanyNumber))
ORDER BY MissingIDColumn;
I have the below query and results. What can I add to this query to combine the calories for people with the same name? For example, Dave's total should be 3045+3129 to show 6174.
SELECT
likes.*, beer.cal, (beer.cal * 21) AS 'Drink 3 in a Week'
FROM
likes
INNER JOIN
beer ON likes.beer = beer.beer;
**name beer cal Drink 3 in a Week**
dave bud 145 3045
dave coors 149 3129
gary miller 143 3003
linda coors 149 3129
mike bud 145 3045
mike miller 143 3003
sally coors 149 3129
sally miller 143 3003
You can use window funtion
SELECT
likes.*, beer.cal,
SUM(beer.cal * 21) OVER(PARTITION BY NAME) AS 'Drink 3 in a Week'
FROM
likes
INNER JOIN
beer ON likes.beer = beer.beer;
Another option is group by.
I have got a client table which pretty much looks like below:
Client List
customer no. Customer name
123 Kristen Smith
128 Jeremy Church
127 Alan Li
132 Ryan Nelson
I need to map it to a Customer_Dim table
Customer_Dim
customer no. Customer name Group no. Group Name Cust_Active Flag
123 Kristen Smith 5491 Zealong Tea Estate Y
167 Anna Hathaway 5823 AA Insurance Y
146 Simon Joe 5671 Direct Automobile Y
148 Henry Wilson 5823 AA Insurance Y
195 Graham Brown 5491 Zealong Tea Estate Y
172 Daria Smith 5671 Direct Automobile N
122 Dyana Smith 5823 AA Insurance N
132 Ryan Nelson 5671 Direct Automobile N
128 Jeremy Church 5823 AA Insurance Y
127 Alan Li 5671 Direct Automobile Y
to get their group numbers from below table (which I am able to do by a simple left join)
to list all the remaining customers (who are active) from the group numbers of the client customer [I AM UNABLE TO DO THIS 2nd PART] :
Required Results :
Customer No. Customer name Group No. Group Name
123 Kristen Smith 5491 Zealong Tea Estate
128 Jeremy Church 5823 AA Insurance
127 Alan Li 5671 Direct Automobile
195 Graham Brown 5491 Zealong Tea Estate
167 Anna Hathaway 5823 AA Insurance
148 Henry Wilson 5823 AA Insurance
146 Simon Joe 5671 Direct Automobile
Please let me know if any other information is needed.
Sorry, if a similar question has been asked earlier - did several searches but was unable to find anything.
Thanks
join the tables to get all the group numbers of the clients in the client list and then select from customer_dim only the clients of these group numbers who are active:
select * from customer_dim
where
cust_active_flag = 'Y'
and
groupno in (
select groupno
from client_list l inner join customer_dim d
on d.customerno = l.customerno
)
See the demo.
Results:
> customerno | customername | groupno | groupname | cust_active_flag
> ---------: | :------------ | ------: | :----------------- | :---------------
> 123 | Kristen Smith | 5491 | Zealong Tea Estate | Y
> 167 | Anna Hathaway | 5823 | AA Insurance | Y
> 146 | Simon Joe | 5671 | Direct Automobile | Y
> 148 | Henry Wilson | 5823 | AA Insurance | Y
> 195 | Graham Brown | 5491 | Zealong Tea Estate | Y
> 128 | Jeremy Church | 5823 | AA Insurance | Y
> 127 | Alan Li | 5671 | Direct Automobile | Y
for get required results you need a condition in your join
SELECT *
FROM Client c
JOIN Customer_Dim cd on c.CustomerNo = cd.CustomerNo
and cd.Cust_ActiveFlag ='Y'
or
SELECT *
FROM Client c
JOIN Customer_Dim cd on c.CustomerNo = cd.CustomerNo
WHERE cd.Cust_ActiveFlag ='Y'
I think it is pretty simple to get your posted result from Customer_Dim table.
if you don't want Group No. of ClientList
select * from Customer_Dim
where [Cust_Active Flag] = 'Y'
and [Group No.] not in (
select CD.[Group No.] from [Client List] as CL inner join Customer_Dim as CD where CL.[customer no.] = CD.[customer no.] )
And
if you only want Group No. of ClientList
select * from Customer_Dim
where [Cust_Active Flag] = 'Y'
and [Group No.] in (
select CD.[Group No.] from [Client List] as CL inner join Customer_Dim as CD where CL.[customer no.] = CD.[customer no.] )
For the case of a client being inactive and wanting to identify the client in the result set you can do a LEFT JOIN with a GROUP BY and leverage Proc SQL automatic remerging in a HAVING clause for selection criteria.
data client_list; input
custno custname:& $30.; datalines;
123 Kristen Smith
128 Jeremy Church
127 Alan Li
132 Ryan Nelson
899 Julius Caesar
run;
data customer_dim; input
custno custname:& $30. groupnum groupname:& $30. Cust_Active_Flag: $1.; datalines;
123 Kristen Smith 5491 Zealong Tea Estate Y
167 Anna Hathaway 5823 AA Insurance Y
146 Simon Joe 5671 Direct Automobile Y
148 Henry Wilson 5823 AA Insurance Y
195 Graham Brown 5491 Zealong Tea Estate Y
172 Daria Smith 5671 Direct Automobile N
122 Dyana Smith 5823 AA Insurance N
132 Ryan Nelson 5671 Direct Automobile N
128 Jeremy Church 5823 AA Insurance Y
127 Alan Li 5671 Direct Automobile Y
231 Donald Duck 7434 Orange Insurance Y
899 Julius Caesar 4999 Emperors N
900 Joshua Norton 4999 Emperors N
925 Joaquin Guzman 4999 Emperors Y
925 Naruhito 4999 Emperors Y
run;
proc sql;
create table want(label="Active customers of clients groups") as
select
LIST.custno as client,
DIM.*
from
customer_dim DIM
left join
client_list LIST
on
DIM.custno = LIST.custno
group by
groupnum
having
N(LIST.custno) > 0
and
(
cust_active_flag = 'Y'
or LIST.custno is not NULL
)
order by
groupnum, custno
;
I'm doing Q4 (Find the titles of all movies not reviewed by Chris Jackson. ) from SQL Movie-Rating Query Exercises Extras and I don't know why this code doesn't work:
SELECT DISTINCT movie.title
FROM movie
INNER JOIN rating ON movie.mid = rating.mID
INNER JOIN reviewer ON rating.rid = reviewer.rid
WHERE rating.mid NOT IN (SELECT rating.mid FROM rating WHERE rating.rid = (SELECT reviewer.rid FROM reviewer WHERE reviewer.name = 'Chris Jackson') )
Output:
title
Gone with the Wind
Snow White
Avatar
This output doesn't include movies that ARE in movie table but ARE NOT in rating table. So I suspect maybe this has something to do with JOIN clause.
TABLES:
Movie
mID title year director
101 Gone with the Wind 1939 Victor Fleming
102 Star Wars 1977 George Lucas
103 The Sound of Music 1965 Robert Wise
104 E.T. 1982 Steven Spielberg
105 Titanic 1997 James Cameron
106 Snow White 1937 <null>
107 Avatar 2009 James Cameron
108 Raiders of the Lost Ark 1981 Steven Spielberg
Reviewer
rID name
201 Sarah Martinez
202 Daniel Lewis
203 Brittany Harris
204 Mike Anderson
205 Chris Jackson
206 Elizabeth Thomas
207 James Cameron
208 Ashley White
Rating
rID mID stars ratingDate
201 101 2 2011-01-22
201 101 4 2011-01-27
202 106 4 <null>
203 103 2 2011-01-20
203 108 4 2011-01-12
203 108 2 2011-01-30
204 101 3 2011-01-09
205 103 3 2011-01-27
205 104 2 2011-01-22
205 108 4 <null>
206 107 3 2011-01-15
206 106 5 2011-01-19
207 107 5 2011-01-20
208 104 3 2011-01-02
Firstly you must use LEFT JOIN and then then use GROUP BY movie.mid, movie.title and put the condition in the HAVING clause:
SELECT movie.title
FROM movie
LEFT JOIN rating ON movie.mid = rating.mID
LEFT JOIN reviewer ON rating.rid = reviewer.rid
GROUP BY movie.mid, movie.title
HAVING SUM(CASE WHEN reviewer.name = 'Chris Jackson' THEN 1 ELSE 0 END) = 0
See the demo.
Results:
> | title |
> | :----------------- |
> | Avatar |
> | Gone with the Wind |
> | Snow White |
> | Star Wars |
> | Titanic |
Although you can use aggregation, I would recommend NOT EXISTS for this purpose. It is close to the phrasing of the logic you want:
SELECT m.*
FROM movie m
WHERE NOT EXISTS (SELECT 1
FROM rating r JOIN
reviewer re
ON r.rid = re.rid
WHERE m.mid = r.mID AND
re.name = 'Chris Jackson'
);
There should simply be no comparison from a performance perspective. This should have much better performance.
I don't understand what this order_by clause is doing and whether I need it or not:
select c.customerid, c.firstname, c.lastname, i.order_date, i.item, i.price from
items_ordered i, customers c
where i.customerid = c.customerid
group by c.customerid, i.item, i.order_date
order by i.order_date desc;
This produces this data:
10330 Shawn Dalton 30-Jun-1999 Pogo stick 28.00
10101 John Gray 30-Jun-1999 Raft 58.00
10410 Mary Ann Howell 30-Jan-2000 Unicycle 192.50
10101 John Gray 30-Dec-1999 Hoola Hoop 14.75
10449 Isabela Moore 29-Feb-2000 Flashlight 4.50
10410 Mary Ann Howell 28-Oct-1999 Sleeping Bag 89.22
10339 Anthony Sanchez 27-Jul-1999 Umbrella 4.50
10449 Isabela Moore 22-Dec-1999 Canoe 280.00
10298 Leroy Brown 19-Sep-1999 Lantern 29.00
10449 Isabela Moore 19-Mar-2000 Canoe paddle 40.00
10413 Donald Davids 19-Jan-2000 Lawnchair 32.00
10330 Shawn Dalton 19-Apr-2000 Shovel 16.75
10439 Conrad Giles 18-Sep-1999 Tent 88.00
10298 Leroy Brown 18-Mar-2000 Pocket Knife 22.38
10299 Elroy Keller 18-Jan-2000 Inflatable Mattress 38.00
10438 Kevin Smith 18-Jan-2000 Tent 79.99
10101 John Gray 18-Aug-1999 Rain Coat 18.30
10449 Isabela Moore 15-Dec-1999 Bicycle 380.50
10439 Conrad Giles 14-Aug-1999 Ski Poles 25.50
10449 Isabela Moore 13-Aug-1999 Unicycle 180.79
10101 John Gray 08-Mar-2000 Sleeping Bag 88.70
10299 Elroy Keller 06-Jul-1999 Parachute 1250.00
10438 Kevin Smith 02-Nov-1999 Pillow 8.50
10101 John Gray 02-Jan-2000 Lantern 16.00
10315 Lisa Jones 02-Feb-2000 Compass 8.00
10449 Isabela Moore 01-Sep-1999 Snow Shoes 45.00
10438 Kevin Smith 01-Nov-1999 Umbrella 6.75
10298 Leroy Brown 01-Jul-1999 Skateboard 33.00
10101 John Gray 01-Jul-1999 Life Vest 125.00
10330 Shawn Dalton 01-Jan-2000 Flashlight 28.00
10298 Leroy Brown 01-Dec-1999 Helmet 22.00
10298 Leroy Brown 01-Apr-2000 Ear Muffs 12.50
While if I remove the order_by clause completely, as in this query:
select c.customerid, c.firstname, c.lastname, i.order_date, i.item, i.price from
items_ordered i, customers c
where i.customerid = c.customerid
group by c.customerid, i.item, i.order_date;
I get these results:
10101 John Gray 30-Dec-1999 Hoola Hoop 14.75
10101 John Gray 02-Jan-2000 Lantern 16.00
10101 John Gray 01-Jul-1999 Life Vest 125.00
10101 John Gray 30-Jun-1999 Raft 58.00
10101 John Gray 18-Aug-1999 Rain Coat 18.30
10101 John Gray 08-Mar-2000 Sleeping Bag 88.70
10298 Leroy Brown 01-Apr-2000 Ear Muffs 12.50
10298 Leroy Brown 01-Dec-1999 Helmet 22.00
10298 Leroy Brown 19-Sep-1999 Lantern 29.00
10298 Leroy Brown 18-Mar-2000 Pocket Knife 22.38
10298 Leroy Brown 01-Jul-1999 Skateboard 33.00
10299 Elroy Keller 18-Jan-2000 Inflatable Mattress 38.00
10299 Elroy Keller 06-Jul-1999 Parachute 1250.00
10315 Lisa Jones 02-Feb-2000 Compass 8.00
10330 Shawn Dalton 01-Jan-2000 Flashlight 28.00
10330 Shawn Dalton 30-Jun-1999 Pogo stick 28.00
10330 Shawn Dalton 19-Apr-2000 Shovel 16.75
10339 Anthony Sanchez 27-Jul-1999 Umbrella 4.50
10410 Mary Ann Howell 28-Oct-1999 Sleeping Bag 89.22
10410 Mary Ann Howell 30-Jan-2000 Unicycle 192.50
10413 Donald Davids 19-Jan-2000 Lawnchair 32.00
10438 Kevin Smith 02-Nov-1999 Pillow 8.50
10438 Kevin Smith 18-Jan-2000 Tent 79.99
10438 Kevin Smith 01-Nov-1999 Umbrella 6.75
10439 Conrad Giles 14-Aug-1999 Ski Poles 25.50
10439 Conrad Giles 18-Sep-1999 Tent 88.00
10449 Isabela Moore 15-Dec-1999 Bicycle 380.50
10449 Isabela Moore 22-Dec-1999 Canoe 280.00
10449 Isabela Moore 19-Mar-2000 Canoe paddle 40.00
10449 Isabela Moore 29-Feb-2000 Flashlight 4.50
10449 Isabela Moore 01-Sep-1999 Snow Shoes 45.00
10449 Isabela Moore 13-Aug-1999 Unicycle 180.79
I'm not sure what the order_by is doing here and if it's having the intended effects.
It looks like it is ordering on i.ordered_date, but using string comparison rather than date comparison, which is why 30-Jun-1999 is placed before 29-Feb-2000. As a string "30-Jun-1999" > "28-Feb-2000", but as dates, the reverse is true.
Check the type of i.ordered_date in the items_ordered table - it should be datetime or similar - if it's varchar, then you will need to either change it to a date type, or cast the value to a date in the order-by clause. E.g.
order by CAST(i.order_date AS DATE) desc
You should always use proper DATETIME datatype to store dates