If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > sql complex query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-04-12, 06:11
preetpalkpaoor preetpalkpaoor is offline
Registered User
 
Join Date: Feb 2012
Posts: 7
Red face sql complex query

Hi All,



I have a question to be solved. Its a tricked one for me.

prod_id sale_date order_id quantity
p1 1/10/2012 1 2
p1 1/10/2012 2 5
p2 1/10/2012 1 3
p3 1/10/2012 2 6
p2 1/11/2012 6 2
p3 1/11/2012 6 3
p1 1/12/2012 9 3
p2 1/12/2012 9 6
p3 1/12/2012 9 7

Above shown is a table (name-sales_rec)

Now if i want to fetch the PROD_ID WHICH IS SOLD IN ALL THE THREE DAYS i.e. 10th ,11th,12th jan. and also the quantity also.

How can we find this?

Could some one please help me.


Thanks in advance.


Preetpal kapoor
Reply With Quote
  #2 (permalink)  
Old 02-04-12, 06:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT prod_id
     , SUM(quantity)
  FROM sales_rec
 WHERE sale_date IN ( '2012-01-10'
                    , '2012-01-11'
                    , '2012-01-12' )
GROUP
    BY prod_id  
HAVING COUNT(DISTINCT sale_date) = 3
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-05-12, 12:14
preetpalkpaoor preetpalkpaoor is offline
Registered User
 
Join Date: Feb 2012
Posts: 7
Thank you buddy.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On