Sql sum of new columns - sql-server

I have a part of a query below where I have created new columns based on conditions:
select
sum(case when Overall_Time_Spent < 0 then 1 else 0 end) as Errors,
sum(case when Overall_Time_Spent between 0 and 3 then 1 else 0 end) as _0_3_days,
sum(case when Overall_Time_Spent = 4 then 1 else 0 end) as _4_days,
sum(case when Overall_Time_Spent = 5 then 1 else 0 end) as _5_days,
sum(case when Overall_Time_Spent between 6 and 8 then 1 else 0 end) as _6_8_days,
sum(case when Overall_Time_Spent >= 9 then 1 else 0 end) as more_than_9_days,
avg(case when Overall_Time_Spent < 0 then 100.0 else 0 end) as Errors_percent,
avg(case when Overall_Time_Spent between 0 and 3 then 100.0 else 0 end) as _0_3_percent,
avg(case when Overall_Time_Spent = 4 then 100.0 else 0 end) as _4_percent,
avg(case when Overall_Time_Spent = 5 then 100.0 else 0 end) as _5_percent,
avg(case when Overall_Time_Spent between 6 and 8 then 100.0 else 0 end) as _6_8_percent,
avg(case when Overall_Time_Spent >= 9 then 100.0 else 0 end) as more_than_9_days_percent,
How can I add a query within this one where I can add TWO MORE columns which give me sum of all the sums and sum of all of avg
Thanks in advance

The CASE expressions you have covers <0, between 0 and 8 and >=9, which is every possibility, provided that your data is an int. Thus, all you need to add is
COUNT(Overall_Time_Spent) AS DaysTotal
If it isn't an int, then your CASE expressions will miss values between 3 and 4, 4 and 5, 5 and 6 and 8 and 9, however, the COUNT would include them.

Try to use subquery:
SELECT a.*, (Errors + _0_3_days...) as Total FROM (
select
sum(case when Overall_Time_Spent < 0 then 1 else 0 end) as Errors,
sum(case when Overall_Time_Spent between 0 and 3 then 1 else 0 end) as _0_3_days,
sum(case when Overall_Time_Spent = 4 then 1 else 0 end) as _4_days,
sum(case when Overall_Time_Spent = 5 then 1 else 0 end) as _5_days,
sum(case when Overall_Time_Spent between 6 and 8 then 1 else 0 end) as _6_8_days,
sum(case when Overall_Time_Spent >= 9 then 1 else 0 end) as more_than_9_days
) as a

Related

Monthly and Yearly Report in one row

I have a table whose output is this
i have written my query like this
SELECT
c.DivisionNameEn,
a.LF_CompanyDivisionID,
0 as code,
SUM(a.totInvoiceAmnt) AS Sales,
'Sales' Salesperson,
sum(a.totInvoiceAmnt) as Sum ,
sum(a.totInvoiceAmnt) /sum(a.totInvoiceAmnt) *100 as MonthPercent ,
YEAR(a.TransactionDate) [Year],
MONTH(a.TransactionDate) [Month],
CASE
WHEN datepart(month,a.TransactionDate) = 1 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 1 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 2 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 2 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 3 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 4 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 5 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 6 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 7 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 8 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 9 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 10 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 10 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 11 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
WHEN datepart(month,a.TransactionDate) = 12 THEN SUM(CASE datepart(month,a.TransactionDate) WHEN 3 THEN 1 ELSE 0 END)
ELSE 0
END as MonthTotal
from POS_salesHeader a
INNER JOIN dbo.STR_CompanyDivision AS c ON c.id = a.LF_CompanyDivisionID
--WHERE YEAR(a.TransactionDate) = YEAR(GETDATE()) AND MONTH(a.TransactionDate) = MONTH(GETDATE()) /*To filter by month*/
GROUP BY YEAR(a.TransactionDate), MONTH(a.TransactionDate) , c.DivisionNameEn ,LF_CompanyDivisionID
it is giving me out put like this
I want a row which give me month Total totInvoiceAmnt and till now Year Total totInvoiceAmnt for example if today its 15th April it gives me total totInvoiceAmnt of only April with the column name MonthsTotal and in another column it give me till now yearly totInvoiceAmnt total from month of jan + Feb + March +April

I want to append year of date in alias, is it possible? and how?

My query looks like this:
SELECT
u.Title,
SUM(CASE datepart(month,VISIT_DATE) WHEN 1 THEN 1 ELSE 0 END) AS 'January',
SUM(CASE datepart(month,VISIT_DATE) WHEN 2 THEN 1 ELSE 0 END) AS 'February',
SUM(CASE datepart(month,VISIT_DATE) WHEN 3 THEN 1 ELSE 0 END) AS 'March',
SUM(CASE datepart(month,VISIT_DATE) WHEN 4 THEN 1 ELSE 0 END) AS 'April',
SUM(CASE datepart(month,VISIT_DATE) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
SUM(CASE datepart(month,VISIT_DATE) WHEN 6 THEN 1 ELSE 0 END) AS 'June',
SUM(CASE datepart(month,VISIT_DATE) WHEN 7 THEN 1 ELSE 0 END) AS 'July',
SUM(CASE datepart(month,VISIT_DATE) WHEN 8 THEN 1 ELSE 0 END) AS 'August',
SUM(CASE datepart(month,VISIT_DATE) WHEN 9 THEN 1 ELSE 0 END) AS 'September',
SUM(CASE datepart(month,VISIT_DATE) WHEN 10 THEN 1 ELSE 0 END) AS 'October',
SUM(CASE datepart(month,VISIT_DATE) WHEN 11 THEN 1 ELSE 0 END) AS 'November',
SUM(CASE datepart(month,VISIT_DATE) WHEN 12 THEN 1 ELSE 0 END) AS 'December'
FROM
pidilite_feedback_app_dev_latest.dbo.visit v, pidilite_feedback_app_dev_latest.dbo.user_master u
WHERE
v.VISIT_DATE BETWEEN '2017-01-30 12:45:43.673' and '2017-05-24 12:45:43.673'
and u.ID = v.USER_ID
and v.VISIT_STATUS = 'submitted'
and u.delete_flag = 'F'
and v.delete_flag = 'F'
and v.DIVISION_CODE = '10'
GROUP BY v.USER_ID, u.Title
where I want something like this
SUM(CASE datepart(month,VISIT_DATE) WHEN 1 THEN 1 ELSE 0 END) AS concat(YEAR(VISIT_DATE),'January'),
OR
SUM(CASE datepart(month,VISIT_DATE) WHEN 2 THEN 1 ELSE 0 END) AS 'February'+''+YEAR(VISIT_DATE),
But it is not working, need guidance, thank you

Where condition a outer applied sql query

I am using a sql query but sorry to say unable to use a where condition. I post my query below.
SELECT * , CAST( RIGHT(FileNo,3) AS numeric) as IntFileNo
FROM ExportLcs A
OUTER APPLY (
SELECT SUM(ReceivedTTUsd) AS TtTotal,
SUM(ReceivedPDCUsd) AS PdcTotal,
SUM(CASE WHEN ReceivedPDCUsd > 0 THEN 1 ELSE 0 END) AS PdcCashCount,
SUM(CASE WHEN ReceivedPDCUsd > 0 THEN ConvertRate ELSE 0 END) AS PdcRateTotal,
SUM(CASE WHEN ConvertRate > 0 THEN CEILING(ReceivedPDCUsd / ConvertRate) ELSE 0 END) AS PdcTotalUsd,
Count(ExportLcId) AS TotalPaymentCount
FROM dbo.ExportPayments G
WHERE A.ExportLcId = G.ExportLcId AND (G.PdcTotal+G.PdcTotalUsd)>0
) AS G
I want to use condition like
A.ExportLcId = G.ExportLcId AND (G.PdcTotal+G.PdcTotalUsd)>0
but and part not working.
Any one can help.
How about having clause instead of where?
SELECT * , CAST(RIGHT(FileNo, 3) AS NUMERIC) AS IntFileNo
FROM dbo.ExportLcs AS A
OUTER APPLY (
SELECT SUM(ReceivedTTUsd) AS TtTotal
, SUM(ReceivedPDCUsd) AS PdcTotal
, SUM(CASE WHEN ReceivedPDCUsd > 0 THEN 1 ELSE 0 END) AS PdcCashCount
, SUM(CASE WHEN ReceivedPDCUsd > 0 THEN ConvertRate ELSE 0 END) AS PdcRateTotal
, SUM(CASE WHEN ConvertRate > 0 THEN CEILING(ReceivedPDCUsd / ConvertRate) ELSE 0 END) AS PdcTotalUsd
, COUNT(ExportLcId) AS TotalPaymentCount
FROM dbo.ExportPayments AS G
WHERE A.ExportLcId = G.ExportLcId
HAVING (SUM(ReceivedPDCUsd) + SUM(CASE WHEN ConvertRate > 0 THEN CEILING(ReceivedPDCUsd / ConvertRate) ELSE 0 END)) > 0
) AS G;
You should consider using Outer Join instead, so try the following:
SELECT * , CAST( RIGHT(FileNo,3) AS numeric) as IntFileNo from ExportLcs A left outer join
(
SELECT SUM(ReceivedTTUsd) AS TtTotal,
SUM(ReceivedPDCUsd) AS PdcTotal,
SUM(CASE WHEN ReceivedPDCUsd > 0 THEN 1 ELSE 0 END) AS PdcCashCount,
SUM(CASE WHEN ReceivedPDCUsd > 0 THEN ConvertRate ELSE 0 END) AS PdcRateTotal,
SUM(CASE WHEN ConvertRate > 0 THEN CEILING(ReceivedPDCUsd / ConvertRate) ELSE 0 END) AS PdcTotalUsd,
Count(ExportLcId) AS TotalPaymentCount
FROM dbo.ExportPayments) G
on A.ExportLcId = G.ExportLcId where (G.PdcTotal+G.PdcTotalUsd)>0
just put your part where clause outside.
SELECT * , CAST( RIGHT(FileNo,3) AS numeric) as IntFileNo
FROM Export
where (G.PdcTotal+G.PdcTotalUsd)>0Lcs A
OUTER APPLY (
SELECT SUM(ReceivedTTUsd) AS TtTotal,
SUM(ReceivedPDCUsd) AS PdcTotal,
SUM(CASE WHEN ReceivedPDCUsd > 0 THEN 1 ELSE 0 END) AS PdcCashCount,
SUM(CASE WHEN ReceivedPDCUsd > 0 THEN ConvertRate ELSE 0 END) AS PdcRateTotal,
SUM(CASE WHEN ConvertRate > 0 THEN CEILING(ReceivedPDCUsd / ConvertRate) ELSE 0 END) AS PdcTotalUsd,
Count(ExportLcId) AS TotalPaymentCount
FROM dbo.ExportPayments G
WHERE A.ExportLcId = G.ExportLcId
) AS G

SQL - AVG call rate by time of day and day of week

I have a table with records for each telephone call.
I want to create a matrix where I have the average call rate per 30 minute time segment over the day by day of week. Each call has a start time which is a date/time
i.e.
I want to display Mon-Sun as row headers and 30 min intervals as column headers. The content is the average count of calls for each day (Mon - Sun) in each particular 30 minute segment. This is where I have got but it isn't what I want. It sums the number of calls by time segment and I cant work out how to get averages.
I hope some can point me in the right direction -
SQL is not something I use often so be gentle :-)
set dateformat dmy
SET DATEFIRST 1 -- Start with Monday
SELECT datepart(dw,starttime) as 'Day',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS 'Midnight-12:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '12:30am-1am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '1am-1:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '1:30am-2am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '2am-2:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '2:30am-3am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '3am-3:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '3:30am-4am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '4am-4:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '4:30am-5am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '5am-5:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '5:30am-6am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '6am-6:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '6:30am-7am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '7am-7:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '7:30am-8am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '8:30am-9am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '9am-9:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '10:30am-11am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '11am-11:30am',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '11:30am-Noon',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS 'Noon-12:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '1pm-1:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '1:30pm-2pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '2pm-2:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '2:30pm-3pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '3pm-3:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '4:30pm-4pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '4pm-5:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '5:30pm-6pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '6pm-6:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '6:30pm-7pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '7pm-7:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '7:30pm-8pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '8pm-8:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '8:30pm-9pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '9pm-9:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '9:30pm-10pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '10pm-10:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '10:30pm-11pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) > 30) THEN 1 ELSE 0 END) AS '11pm-11:30pm',
SUM(CASE WHEN (DATEPART(hour,StartTime) = 7 and DATEPART(Minute,StartTime) <= 30) THEN 1 ELSE 0 END) AS '11:30pm-Midnight'
FROM [dbo].[CallList]
where StartTime>'1/6/2015'
and StartTime<'30/6/2015'
GROUP BY datepart(dw,starttime)
ORDER BY datepart(dw,starttime)
Creating a WHILE loop to insert into a temp table then doing the aggregation after the fact is a viable option. See below:
CREATE TABLE #Data
(
ID UNIQUEIDENTIFIER
,DateIn DATETIME
)
INSERT INTO #Data
VALUES
(NEWID(), '2016-05-17 00:01:00'),
(NEWID(), '2016-05-17 00:21:00'),
(NEWID(), '2016-05-17 01:01:00'),
(NEWID(), '2016-05-17 05:41:00'),
(NEWID(), '2016-05-17 06:51:00'),
(NEWID(), '2016-05-17 13:21:00'),
(NEWID(), '2016-05-17 18:01:00'),
(NEWID(), '2016-05-17 21:11:00'),
(NEWID(), '2016-05-17 23:41:00'),
(NEWID(), '2016-05-17 23:51:00')
DECLARE #Start DATETIME
DECLARE #End DATETIME
SET #Start = '2016-05-17 00:00:00'
SET #End = '2016-05-17 00:30:00'
CREATE TABLE #Temp
(
ID UNIQUEIDENTIFIER
,[Time Increment] VARCHAR(100)
)
WHILE #End <= '2016-05-17 23:30:00'
BEGIN
INSERT INTO #Temp
SELECT
ID
,CAST(#Start AS VARCHAR) + ' - ' + CAST(#End AS VARCHAR) [Time Increment]
FROM #Data
WHERE DateIn >= #Start
AND DateIn < #End
SET #Start = DATEADD(Minute, 30, #Start)
SET #End = DATEADD(Minute, 30, #End)
END
SELECT
[Time Increment]
,COUNT(ID) [Count]
FROM #Temp
GROUP BY [Time Increment]
DROP TABLE #Temp
DROP TABLE #Data
In this example, the result is the following:
Time Increment |Count
May 17 2016 1:00AM - May 17 2016 1:30AM | 1
May 17 2016 1:00PM - May 17 2016 1:30PM | 1
May 17 2016 5:30AM - May 17 2016 6:00AM | 1
May 17 2016 6:00PM - May 17 2016 6:30PM | 1
May 17 2016 6:30AM - May 17 2016 7:00AM | 1
May 17 2016 9:00PM - May 17 2016 9:30PM | 1
May 17 2016 12:00AM - May 17 2016 12:30AM | 2

Grouping Records by header SQL

I have the following query;
SELECT STOCK_CODE,
dbo.manu_STOCK.DESCRIPTION,
QTY_IN_STOCK,
Quantity,
ForecastDate
FROM [FS25-w2k8\SQLEXPRESS].sagel50_46772.dbo.SalesForecastLines AS SalesForecastLines1
INNER JOIN dbo.manu_STOCK
ON SalesForecastLines1.ProductCode = dbo.manu_STOCK.STOCK_CODE
This brings up the following information;
STOCK_CODE DESCRIPTION QTY_INSTOCK Quantity ForecastDate
523 gel 12 10 01/08/2014
523 gel 12 10 08/08/2014
I want to be able to modify the query so that it displays the following formation
Stock Code Description WK1 WK2
523 gel 22 22
So it will sum qty in stock and quantity on the first date and the column will be called wk1, second week - wk2 etc.
Can you advise on this please?
IF you want to pivot the results for 52 weeks, you can use the following query. This will pivot data for 52 weeks. This is just concept, I could not test this query.
SELECT * FROM
( SELECT
dbo.manu_STOCK.STOCK_CODE AS [Stock Code],
dbo.manu_STOCK.DESCRIPTION,
DATEPART(WEEK,[Date]) Wk,
QTY_IN_STOCK + Quantity AS Stock
FROM [FS25-w2k8\SQLEXPRESS].sagel50_46772.dbo.SalesForecastLines AS SalesForecastLines1
INNER JOIN dbo.manu_STOCK
ON SalesForecastLines1.ProductCode = dbo.manu_STOCK.STOCK_CODE
) AS Source
PIVOT
(
SUM(Stock)
FOR WK IN
([0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]
,[11],[12],[13],[14],[15],[16],[17],[18],[19],[20]
,[21],[22],[23],[24],[25],[26],[27],[28],[29],[30]
,[31],[32],[33],[34],[35],[36],[37],[38],[39],[40]
,[41],[42],[43],[44],[45],[46],[47],[48],[49],[50]
,[51],[52])
) AS PVT ;
DECLARE #Stock TABLE
(
STOCK_CODE INT,
DESCRIPTION VARCHAR(50),
QTY_INSTOCK INT,
QUANTITY INT,
FORECASTDATE DATETIME
)
INSERT INTO #Stock
( STOCK_CODE, DESCRIPTION, QTY_INSTOCK, QUANTITY, FORECASTDATE )
VALUES
( 523, 'gel', 12, 10, '01/08/2014' ),
( 523, 'gel', 12, 10, '08/08/2014' )
SELECT
s.STOCK_CODE,
s.DESCRIPTION,
s.STOCKYEAR,
SUM(CASE WHEN s.STOCKWEEK = 1 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk1,
SUM(CASE WHEN s.STOCKWEEK = 2 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk2,
SUM(CASE WHEN s.STOCKWEEK = 3 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk3,
SUM(CASE WHEN s.STOCKWEEK = 4 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk4,
SUM(CASE WHEN s.STOCKWEEK = 5 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk5,
SUM(CASE WHEN s.STOCKWEEK = 6 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk6,
SUM(CASE WHEN s.STOCKWEEK = 7 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk7,
SUM(CASE WHEN s.STOCKWEEK = 8 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk8,
SUM(CASE WHEN s.STOCKWEEK = 9 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk9,
SUM(CASE WHEN s.STOCKWEEK = 10 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk10,
SUM(CASE WHEN s.STOCKWEEK = 11 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk11,
SUM(CASE WHEN s.STOCKWEEK = 12 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk12,
SUM(CASE WHEN s.STOCKWEEK = 13 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk13,
SUM(CASE WHEN s.STOCKWEEK = 14 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk14,
SUM(CASE WHEN s.STOCKWEEK = 15 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk15,
SUM(CASE WHEN s.STOCKWEEK = 16 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk16,
SUM(CASE WHEN s.STOCKWEEK = 17 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk17,
SUM(CASE WHEN s.STOCKWEEK = 18 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk18,
SUM(CASE WHEN s.STOCKWEEK = 19 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk19,
SUM(CASE WHEN s.STOCKWEEK = 20 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk20,
SUM(CASE WHEN s.STOCKWEEK = 21 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk21,
SUM(CASE WHEN s.STOCKWEEK = 22 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk22,
SUM(CASE WHEN s.STOCKWEEK = 23 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk23,
SUM(CASE WHEN s.STOCKWEEK = 24 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk24,
SUM(CASE WHEN s.STOCKWEEK = 25 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk25,
SUM(CASE WHEN s.STOCKWEEK = 26 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk26,
SUM(CASE WHEN s.STOCKWEEK = 27 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk27,
SUM(CASE WHEN s.STOCKWEEK = 28 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk28,
SUM(CASE WHEN s.STOCKWEEK = 29 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk29,
SUM(CASE WHEN s.STOCKWEEK = 30 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk30,
SUM(CASE WHEN s.STOCKWEEK = 31 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk31,
SUM(CASE WHEN s.STOCKWEEK = 32 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk32,
SUM(CASE WHEN s.STOCKWEEK = 33 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk33,
SUM(CASE WHEN s.STOCKWEEK = 34 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk34,
SUM(CASE WHEN s.STOCKWEEK = 35 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk35,
SUM(CASE WHEN s.STOCKWEEK = 36 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk36,
SUM(CASE WHEN s.STOCKWEEK = 37 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk37,
SUM(CASE WHEN s.STOCKWEEK = 38 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk38,
SUM(CASE WHEN s.STOCKWEEK = 39 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk39,
SUM(CASE WHEN s.STOCKWEEK = 40 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk40,
SUM(CASE WHEN s.STOCKWEEK = 41 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk41,
SUM(CASE WHEN s.STOCKWEEK = 42 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk42,
SUM(CASE WHEN s.STOCKWEEK = 43 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk43,
SUM(CASE WHEN s.STOCKWEEK = 44 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk44,
SUM(CASE WHEN s.STOCKWEEK = 45 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk45,
SUM(CASE WHEN s.STOCKWEEK = 46 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk46,
SUM(CASE WHEN s.STOCKWEEK = 47 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk47,
SUM(CASE WHEN s.STOCKWEEK = 48 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk48,
SUM(CASE WHEN s.STOCKWEEK = 49 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk49,
SUM(CASE WHEN s.STOCKWEEK = 50 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk50,
SUM(CASE WHEN s.STOCKWEEK = 51 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk51,
SUM(CASE WHEN s.STOCKWEEK = 52 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk52,
SUM(CASE WHEN s.STOCKWEEK = 53 THEN s.QTY_INSTOCK + s.QTY ELSE 0 END) Wk53
FROM
(
SELECT s.STOCK_CODE, s.DESCRIPTION, DATEPART(YEAR, s.FORECASTDATE) STOCKYEAR, DATEPART(WEEK, s.FORECASTDATE) STOCKWEEK, SUM(s.QTY_INSTOCK) QTY_INSTOCK, SUM(s.QUANTITY) QTY
FROM #Stock s
GROUP BY s.STOCK_CODE, s.DESCRIPTION, DATEPART(YEAR, s.FORECASTDATE), DATEPART(WEEK, s.FORECASTDATE)
) s
GROUP BY s.STOCK_CODE, s.DESCRIPTION, s.STOCKYEAR
ORDER BY s.STOCK_CODE, s.DESCRIPTION, s.STOCKYEAR
Group by the code, desc, year and week and then sum by week for each week of the year (adding extra week depending on day beginning / end of the year falls.

Resources