MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 15th, 2012, 10:16 AM
loulou loulou is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 386 loulou User rank is Corporal (100 - 500 Reputation Level)loulou User rank is Corporal (100 - 500 Reputation Level)loulou User rank is Corporal (100 - 500 Reputation Level)loulou User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 20 h 34 m 59 sec
Reputation Power: 9
Basic Query Problem

This query seems really easy but I'm out of practice

Basically, I need to find people who have ordered 4 particular products.

Table 1 - person
id
name

Table 2 - order
id
personid
name

I tried doing the following but it didn't work:

Code:
Select person.name from person inner join order on person.id = order.personid where order.name = "bike" and order.name="pencil" and order.name = "book" and order.name = "soda";


It returns nothing yet I have 2 people who have ordered all 4 of these items.

Thanks

Reply With Quote
  #2  
Old June 15th, 2012, 10:31 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,442 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 15 m 54 sec
Reputation Power: 4141
it doesn't work because the WHERE clause evaluates each row by itself, and the same column value cannot be equal to 4 different things simultaneously

what you're looking for is four different rows
Code:
SELECT person.name 
  FROM person 
INNER
  JOIN [order] 
    ON [order].personid = person.id 
   AND [order].name IN ('bike','pencil','book','soda')
GROUP
    BY person.name
HAVING COUNT(DISTINCT [order].name) = 4
two comments...

first, ORDER is a reserved word, so you have to escape it (better would be to rename the table)

second, strings are delimited by the single quote, not the doublequote

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old June 15th, 2012, 10:50 AM
loulou loulou is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 386 loulou User rank is Corporal (100 - 500 Reputation Level)loulou User rank is Corporal (100 - 500 Reputation Level)loulou User rank is Corporal (100 - 500 Reputation Level)loulou User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 20 h 34 m 59 sec
Reputation Power: 9
Thanks for the quick response. I was thinking that was my problem but I always have a difficult time when using HAVING.

I tried your query but the "AND" did not work so I replaced it with "WHERE" and the "DISTINCT" rendered an error so I removed it.

So with the revised query I have
Code:
SELECT person.name 
  FROM person 
INNER JOIN [order] 
    ON [order].personid = person.id 
WHERE  [order].name IN ('bike','pencil','book','soda')
GROUP
    BY person.name
HAVING COUNT([order].name) = 4


I received a result of one name returned which I think is right.

Many Thanks

Reply With Quote
  #4  
Old June 15th, 2012, 11:17 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,442 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 15 m 54 sec
Reputation Power: 4141
Quote:
Originally Posted by loulou
I tried your query but the "AND" did not work so I replaced it with "WHERE" and the "DISTINCT" rendered an error so I removed it.
what version of SQL Server are you running???

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Basic Query Problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap