Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi All,

I am getting result set for the below query

SQL
SELECT CAST(SUM(CASE WHEN  S.TAXABLEAMT <=2000 THEN (S.INVOICEQTY) ELSE NULL END) AS DECIMAL(30,2)) AS QTY ,
YEAR(S.invoicedate) YEAR1,Month(S.invoicedate) MNTH  
FROM SALESDATA S
where  month(S.invoicedate) BETWEEN 1AND 4  and year(S.invoicedate) BETWEEN  2009 AND 2010  
GROUP BY YEAR(S.invoicedate),Month(S.invoicedate)
ORDER BY YEAR(S.invoicedate),Month(S.invoicedate)


as
QTY MONTH/YEAR
250 01/2010
238 02/2010
450 03/2010
238 04/2010
150 05/2010
238 05/2010
650 06/2010
238 07/2010
250 08/2010
238 09/2010
250 10/2010
238 11/2010
250 12/2010
238 01/2009
250 01/2009
238 02/2009
450 03/2009
238 04/2009

Now I want difference between QTY of First column First value(i.e.250) and First column Last value(i.e.238 ) in result set

as Separate column.(i.e. only single value ).

Is it possible?(with Separate table or within same table in the query)

Regards,
NSJ

--
Original title - How to get the difference between First column First value and First column Last value in result set of a stored procedure?
Posted
Updated 27-Jan-11 21:57pm
v3
Comments
Sunasara Imdadhusen 28-Jan-11 3:42am    
Added code formatting.
Indivara 28-Jan-11 3:58am    
While you're at it, try to shorten horribly long titles as well...

1 solution

try this

SELECT CAST(SUM(CASE WHEN  S.TAXABLEAMT <=2000 THEN (S.INVOICEQTY) ELSE NULL END) AS DECIMAL(30,2)) AS QTY ,
YEAR(S.invoicedate) YEAR1,Month(S.invoicedate) MNTH,
(
	(SELECT top 1 CAST(SUM(CASE WHEN  S1.TAXABLEAMT <=2000 THEN (S1.INVOICEQTY) ELSE NULL END) AS DECIMAL(30,2)) AS QTY
	FROM SALESDATA S1
	where  month(S1.invoicedate) BETWEEN 1AND 4  and year(S1.invoicedate) BETWEEN  2009 AND 2010  
	ORDER BY YEAR(S1.invoicedate),Month(S1.invoicedate))
	-
	(SELECT top 1 CAST(SUM(CASE WHEN  S2.TAXABLEAMT <=2000 THEN (S2.INVOICEQTY) ELSE NULL END) AS DECIMAL(30,2)) AS QTY
	FROM SALESDATA S2
	where  month(S2.invoicedate) BETWEEN 1AND 4  and year(S2.invoicedate) BETWEEN  2009 AND 2010  
	ORDER BY YEAR(S2.invoicedate) DESC,Month(S2.invoicedate) DESC)
) [Diffrence]
FROM SALESDATA S
where  month(S.invoicedate) BETWEEN 1AND 4  and year(S.invoicedate) BETWEEN  2009 AND 2010  
GROUP BY YEAR(S.invoicedate),Month(S.invoicedate)
ORDER BY YEAR(S.invoicedate),Month(S.invoicedate)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900