Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Parameter

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2011-12-09 : 10:28:40
Is there a way to have this prompt me for a month and year instead of using the current date?

SELECT     TOP (100) PERCENT CustSlsPsn, cus_no, item_no, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2) 
= 01 THEN sls_amt END), 0) AS Jan_Sales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 01 THEN qty_to_ship END), 0) AS Jan_Qty, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 02 THEN sls_amt END), 0) AS Feb_Sales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 02 THEN qty_to_ship END), 0) AS Feb_Qty, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 03 THEN sls_amt END), 0) AS Mar_Sales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 03 THEN qty_to_ship END), 0) AS Mar_Qty, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 04 THEN sls_amt END), 0) AS Apr_Sales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 04 THEN qty_to_ship END), 0) AS Apr_Qty, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 05 THEN sls_amt END), 0) AS May_Sales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 05 THEN qty_to_ship END), 0) AS May_Qty, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 06 THEN sls_amt END), 0) AS Jun_Sales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 06 THEN qty_to_ship END), 0) AS Jun_Qty, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 07 THEN sls_amt END), 0) AS Jul_Sales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 07 THEN qty_to_ship END), 0) AS Jul_Qty, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 08 THEN sls_amt END), 0) AS Aug_Sales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 08 THEN qty_to_ship END), 0) AS Aug_Qty, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 09 THEN sls_amt END), 0) AS Sep_Sales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 09 THEN qty_to_ship END), 0) AS Sep_Qty, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 10 THEN sls_amt END), 0) AS Oct_Sales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 10 THEN qty_to_ship END), 0) AS Oct_Qty, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 11 THEN sls_amt END), 0) AS Nov_Sales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 11 THEN qty_to_ship END), 0) AS Nov_Qty, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 12 THEN sls_amt END), 0) AS Dec_Sales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 12 THEN qty_to_ship END), 0) AS Dec_Qty, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
<= month(getdate()) THEN sls_amt END), 0) AS SalesYTD, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
<= month(getdate()) THEN qty_to_ship END), 0) AS UnitsYTD, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) - 1 AND RIGHT(LEFT(inv_dt, 6),
2) <= month(getdate()) THEN sls_amt END), 0) AS LYSalesYTD, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) - 1 AND RIGHT(LEFT(inv_dt,
6), 2) <= month(getdate()) THEN qty_to_ship END), 0) AS LYUnitsYTD, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) - 1 AND
RIGHT(LEFT(inv_dt, 6), 2) <= 12 THEN sls_amt END), 0) AS LYTotalSales, ISNULL(SUM(CASE WHEN LEFT(inv_dt, 4) = year(getdate()) - 1 AND
RIGHT(LEFT(inv_dt, 6), 2) <= 12 THEN qty_to_ship END), 0) AS LYTotalUnits
FROM dbo.Last3Oelinhst
GROUP BY CustSlsPsn, cus_no, item_no

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-09 : 10:33:07
instead of CASE WHEN LEFT(inv_dt, 4) = year(getdate()) AND RIGHT(LEFT(inv_dt, 6), 2)
= 01 THEN sls_amt END

you can use like

CASE WHEN inv_dt>= DATEADD(mm,@month-1,DATEADD(yy,@year-1900,0)) and inv_dt< DATEADD(mm,@month,DATEADD(yy,@year-1900,0)) THEN sls_amt END
...

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -