Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


help me write sql


help me write sql

Author
Message
tirthadip
tirthadip
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 14
I have a table (with data) like the following :


Site-id product_id mode quantity date_of_issue_return

3 14 Issue 75 02/10/2014
6 14 Issue 100.5 02/15/2014
7 14 Return 120 01/14/2014
1 12 Issue 400 01/04/2014
1 12 Issue 27.5 03/05/2014
4 9 Return 159 03/28/2014
4 9 Return 20 03/19/2014
3 14 Return 30 01/25/2014
4 9 Issue 101 01/01/2014

I want a report like the following within a given period

site_id ---product_id---total issue qty---total return qty---total consumption(total issue- total return)

please help me how can I write a sql query to achieve this?
spaghettidba
spaghettidba
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5732 Visits: 13305

SET DATEFORMAT mdy

DECLARE @test TABLE (
Site_id int,
product_id int,
mode varchar(10),
quantity decimal(9,3),
date_of_issue_return date
)



INSERT INTO @test VALUES
(3, 14 ,'Issue' ,75 ,'02/10/2014')
,(6, 14 ,'Issue' ,100.5 ,'02/15/2014')
,(7, 14 ,'Return' ,120 ,'01/14/2014')
,(1, 12 ,'Issue' ,400 ,'01/04/2014')
,(1, 12 ,'Issue' ,27.5 ,'03/05/2014')
,(4, 9 ,'Return' ,159 ,'03/28/2014')
,(4, 9 ,'Return' ,20 ,'03/19/2014')
,(3, 14 ,'Return' ,30 ,'01/25/2014')
,(4, 9 ,'Issue' ,101 ,'01/01/2014')


SELECT site_id,
product_id,
SUM(CASE mode WHEN 'Issue' THEN quantity ELSE 0 END) AS total_issue_qty,
SUM(CASE mode WHEN 'Return' THEN quantity ELSE 0 END) AS total_return_qty,
SUM(CASE mode WHEN 'Issue' THEN quantity ELSE 0 END)
- SUM(CASE mode WHEN 'Return' THEN quantity ELSE 0 END) AS total_consumption
FROM @test
WHERE date_of_issue_return >= '01/01/2014'
AND date_of_issue_return < '02/01/2014'
GROUP BY site_id,
product_id



Hope this helps

--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
tirthadip
tirthadip
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 14
You are a genious spaghettidba....thanks a ton for the help.....people like u make the world a better place to live.....
spaghettidba
spaghettidba
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5732 Visits: 13305
You're welcome.
Thanks for the kind words.

--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search