I'm trying to sort some shipment data using a SQL Pivot but i can not figure it out.
I've the data sorted in this way (one row with the total items shipped for a family for each month of each year starting from 2015 to ):
TABLE A
Year | Month | ItemFamilyCode | TotalShipped
2018 | 9 | FA01 | 5
2018 | 9 | FA04 | 4
2018 | 10 | FA01 | 2
2018 | 11 | FA02 | 1
2018 | 12 | FA03 | 3
2019 | 1 | FA04 | 7
and so on. I want to achieve the following result:
ItemFamilyCode | 2018-9 | 2018-10 | 2018-11 | 2018-12 | 2019-1 | [..]
FA01 | 5 | 2 | 0 | 0 | 0 |
FA02 | 0 | 0 | 1 | 0 | 0 |
FA03 | 0 | 0 | 0 | 3 | 0 |
FA04 | 4 | 0 | 1 | 0 | 7 |
and so on ... the family code in order and all the values for each month of each year, from the older month/year to now. Is it possible? Thanks to anyone who can help.
If you want to use it as view :
SELECT * FROM
(
SELECT
Concat([Year],'-', [Month]) as [Date],
ItemFamilyCode,
TotalShipped
FROM Shipping -- Or any Table Name
) t
PIVOT(
Sum(TotalShipped)
FOR [Date] IN (
[2018-9],
[2018-10],
[2018-11],
[2018-12],
[2019-1],
[2019-2] -- You have to type all months until today
)
) AS pivot_table;
And, dynamic sql if you can use it in stored procedure :
Make a table with the content of date list to generate date list string
DECLARE
#columns NVARCHAR(MAX) = '',
#sql NVARCHAR(MAX) = '';
-- select the category names
SELECT
#columns+=QUOTENAME(Date) + ','
FROM
DateList
ORDER BY
DateList;
-- remove the last comma
SET #columns = LEFT(#columns, LEN(#columns) - 1);
-- construct dynamic SQL
SET #sql ='
SELECT * FROM
(
SELECT
Concat([Year],'-', [Month]) as [Date],
ItemFamilyCode,
TotalShipped
FROM Shipping -- Or any Table Name
) t
PIVOT(
Sum(TotalShipped)
FOR [Date] IN ('+ #columns +')
) AS pivot_table;';
-- execute the dynamic SQL
EXECUTE sp_executesql #sql;
Source : sqlservertutorial
Related
I am new to SQL and trying to write a statement similar to a 'for loop' in other languages and am stuck. I want to filter out rows of the table where for all of attribute 1, attribute2=attribute3 without using functions.
For example:
| Year | Month | Day|
| 1 | 1 | 1 |
| 1 | 2 | 2 |
| 1 | 4 | 4 |
| 2 | 3 | 4 |
| 2 | 3 | 3 |
| 2 | 4 | 4 |
| 3 | 4 | 4 |
| 3 | 4 | 4 |
| 3 | 4 | 4 |
I would only want the row
| Year | Month | Day|
|:---- |:------:| -----:|
| 3 | 4 | 4 |
because it is the only where month and day are equal for all of the values of year they share.
So far I have
select year, month, day from dates
where month=day
but unsure how to apply the constraint for all of year
-- month/day need to appear in aggregate functions (since they are not in the GROUP BY clause),
-- but the HAVING clause ensure we only have 1 month/day value (per year) here, so MIN/AVG/SUM/... would all work too
SELECT year, MAX(month), MAX(day)
FROM my_table
GROUP BY year
HAVING COUNT(DISTINCT (month, day)) = 1;
year
max
max
3
4
4
View on DB Fiddle
So one way would be
select distinct [year], [month], [day]
from [Table] t
where [month]=[day]
and not exists (
select * from [Table] x
where t.[year]=x.[year] and t.[month] <> x.[month] and t.[day] <> x.[day]
)
And another way would be
select distinct [year], [month], [day] from (
select *,
Lead([month],1) over(partition by [year] order by [month])m2,
Lead([day],1) over(partition by [year] order by [day])d2
from [table]
)x
where [month]=m2 and [day]=d2
Can I know how can I modify the SQL Query below so that the result will turn into expected result using pivot table where () in For MonthYear = Start Date and End Date selected by user ?
SQL Query : SQL Query Result (Image)
Expected Result : Table (Image)
SELECT * FROM
(
SELECT WR.Work_Type,
LEFT(DATENAME(MONTH,WR.Request_Date),3)+' '+STR(YEAR(WR.Request_Date),4) AS MonthYear,
CONVERT(INT,STR(YEAR(WR.Request_Date),4)+REPLACE(STR(MONTH(WR.Request_Date),2),' ','0') ) AS MonthYearOrder,
COUNT(ISNULL(WR.Request_ID,0)) AS Total
FROM tblWork_Request WR
INNER JOIN vWorkTypeByPropertyLevel WT ON WR.CoID=WT.CoID AND WR.Work_Type=WT.Work_Type AND WT.IsWorkRequest=1
LEFT JOIN [UBERIQ_1.5_Property].[dbo].[tblProperty] P ON WR.Property_ID=P.Property_ID
WHERE WR.CoID='59' and WR.Property_ID='MCST001'
GROUP BY WR.Work_Type,LEFT(DATENAME(MONTH,WR.Request_Date),3)+' '+STR(YEAR(WR.Request_Date),4),
CONVERT(INT,STR(YEAR(WR.Request_Date),4)+REPLACE(STR(MONTH(WR.Request_Date),2),' ','0') )
) t
pivot (
SUM(Total)
FOR MonthYear IN ()
) as pivot_table
Result :-
Work_Type | MonthYear | MonthYearOrder | Total
----------------------------------------------------------
A.1 Problem Code 1 | Jun 2020 | 202006 | 3
AHU faulty | Nov 2020 | 202011 | 6
AirCondition | May 2020 | 202005 | 1
Exterior | Jun 2020 | 202006 | 2
Expected Result :-
Work Type | Jan 2020 | Feb 2020 | Mar 2020 | Apr 2020 | May 2020 | Jun 2020 | Jun 2020 | Jul 2020 | Aug 2020 | Sep 2020 | Oct 2020 | Nov 2020 | Dec 2020 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------
A.1 Problem Code 1 | 0 | 0 | 0 | 0 | 0 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
AHU Faulty | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6 | 0 |
AirCondition | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Exterior | 0 | 0 | 0 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Please advise. Thank you.
--Variables for dynamic columns and query
DECLARE #columns NVARCHAR(MAX) = ''
DECLARE #ifnullcolumns NVARCHAR(MAX) = ''
DECLARE #query NVARCHAR(MAX)
;WITH months(YearMonth) AS
(
SELECT 202001
UNION ALL
SELECT YearMonth + 1
FROM months
WHERE YearMonth < 202012
)
SELECT
#columns += QUOTENAME(YearMonth) + ',',
#ifnullcolumns += 'ISNULL(' + QUOTENAME(YearMonth) + ', 0) AS ' + QUOTENAME(YearMonth)+','
FROM
months
-- remove last , from the column list
SET #columns = LEFT(#columns, LEN(#columns) - 1)
SET #ifnullcolumns = LEFT(#ifnullcolumns, LEN(#ifnullcolumns) - 1)
-- Building dynamic query with pivot
SET #query ='
SELECT Work_Type, '+ #ifnullcolumns +' FROM (
select Work_Type, MonthYerOrder, Total
from Sample) t
PIVOT(
SUM(Total)
FOR MonthYerOrder IN ('+ #columns +')
) AS pivot_table;'
EXECUTE sp_executesql #query
Result of this query: http://sqlfiddle.com/#!18/96333/63
From the answer from here I build a solution that is good for me but I have still one problem.
I had table:
ID | Year | Month | Multiply | Future | Current
123 | 2017 | 1 | 1.0 | 25 | null
123 | 2017 | 2 | 1.0 | 19 | 15
123 | 2017 | 3 | 1.0 | 13 | 0
123 | 2017 | 4 | 1.0 | 22 | 14
123 | 2017 | 5 | 1.0 | 13 | null
... | .... | ... | ... | .. | ..
123 | 2018 | 1 | 1.0 | 25 | 10
123 | 2018 | 2 | 1.0 | 25 | 10
... | .... | ... | ... | .. | ..
124 | 2017 | 1 | 1 | 10 | 5
124 | 2017 | 2 | 1 | 15 | 2
... | .... | ... | ... | .. | ..
124 | 2018 | 1 | 1 | 20 | 0
I build this view to concatenate Year + Month and make IF statement:
value in the new Value column I'm getting from Future and Current column - when the Current value is null get the Future value and multiply by Multiply, else get Current value and multiply by Multiply (even 0). Next to it I need to add a 'F' prefix when the value is got from Future column.
ID | Date | Value |
123 | 2017 - 1 | F25 |
123 | 2017 - 2 | 15 |
123 | 2017 - 3 | 0 |
.. | .. | .. |
Code for it:
SELECT ID = ID,
[Date] = [Date],
[Value] = [Value]
FROM ( SELECT ID,
cast([Year] as varchar(30)) + ' - ' + cast([Month]as varchar(30)) as [Date],
[Multiply],
case when [Current] IS NULL /*OR [Current] = 0*/
then 'F' + CAST([Future] * [Multiply] as varchar(30))
else CAST([Current] * [Multiply] as varchar(30))
end as Value
FROM dbo.CurrentFuture
) AS t
And from this I make this view via dynamically pivot.
ID | 2017 - 1 | 2017 - 10 | 2017 - 11 | 2017 - 12 | 2017 - 2 | ... | 2018 - 1 | ...
123 | F25 | .. | .. | .. | 15 | ... | 10 | ...
124 | 5 | 2 | .. | .. | .. | ... | 0 | ...
Code for it:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT ',' + QUOTENAME([Date])
from dbo.UpperView
group by [Date]
order by [Date]
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT [ID],' + #cols + ' from
(
select [ID], [Date],[Value]
from [dbo].[UpperView]
) x
pivot
(
max([Value])
for [Date] in (' + #cols + ')
) p '
execute(#query);
As you can see columns in the new view are not sorting in a good way.. instead of 2017 - 1, 2017 - 2, 2017 - 3 I have 2017 - 1, 2017 - 10, 2017 - 11, 2017 - 12, 2017 - 2. Can you help me how to sort it properly?
From the limited information, What you want is the ordering of the column based on the Concatenated string of Year+ Month.
What you need is to prefix the month with "0" for January - September and no prefix for October-December.
so in effect you will achieve this.
ID | 2017 - 01 | 2017 - 02 | 2017 - 03 | ..... | 2017 - 09 |2018 - 10 |2018 - 11||2018 - 12|
SELECT ID = ID,
[Date] = [Date],
[Value] = [Value]
FROM
(
SELECT ID,
CAST([Year] AS VARCHAR(30))+' - '+RIGHT('0'+CAST([Month] AS VARCHAR(30)), 2) AS [Date],
[Multiply],
CASE
WHEN [Current] IS NULL
/*OR [Current] = 0*/
THEN 'F'+CAST([Future] * [Multiply] AS VARCHAR(30))
ELSE CAST([Current] * [Multiply] AS VARCHAR(30))
END AS Value
FROM dbo.CurrentFuture
) AS t;
Add new column to UpperView for sorting like this
cast([Year] as varchar(30)) + RIGHT('0' + cast([Month] as varchar(30)), 2) as [DateOrder]
and use this column for sorting at your column query instead of [Date]
select #cols = STUFF((SELECT ',' + QUOTENAME([Date])
from dbo.UpperView
group by [Date], [DateOrder]
order by [DateOrder]
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
Hi I have a query that gets the total count of incidents per month and year
I wanted a result that shows all distinct years in columns
Could you please advise how to make this query dynamic?
Expected Result:
Month 2013 2014 2015
January 8 0 12
February 9 6 10
March 12 1 9
April 10 13 27
May 9 22 15
June 27 4 20
July 15 12 22
August 20 2 2
September 22 5 10
October 10 8 12
November 0 7 0
December 0 15 0
Query
select DATENAME(MONTH,DateOpened) as Month,
sum(case when year(DateOpened) = '2015' then 1 else 0 end) as [2015],
sum(case when year(DateOpened) = '2014' then 1 else 0 end) as [2014]
from Incidents
group by DATENAME(MONTH,DateOpened), MONTH(DateOpened)
order by MONTH(DateOpened)
Thanks for your help!
You can use the PIVOT table operator instead, something like this:
SELECT *
FROM
(
SELECT
DATENAME(MONTH,DateOpened) as Month,
DATENAME(Year,DateOpened) AS Year,
DateOpened
FROM Incidents
) AS t
PIVOT
(
COUNT(DateOpened)
FOR Year IN([2013], [2014], [2015])
) AS p;
SQL Fiddle Demo
If you don't need to write the list of years and do it dynamically for any year, you have to use dynamic SQL to run the query dynamically, like this:
DECLARE #cols AS NVARCHAR(MAX);
DECLARE #query AS NVARCHAR(MAX);
SELECT #cols = STUFF((SELECT distinct ',' +
QUOTENAME(DATENAME(Year,DateOpened))
from Incidents AS t
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '');
SELECT #query = 'SELECT * , '+ #cols + '
FROM
(
select
DATENAME(MONTH,DateOpened) as Month,
DATENAME(Year,DateOpened) AS Year,
DateOpened
FROM Incidents
) AS t
PIVOT
(
COUNT(DateOpened)
FOR Year IN(' + #cols + ')' +
') p';
execute(#query);
SQL Fiddle Demo
This will give you something like this:
| Month | 2014 | 2015 | 2014 | 2015 |
|-----------|------|------|------|------|
| April | 0 | 3 | 0 | 3 |
| August | 1 | 1 | 1 | 1 |
| December | 1 | 0 | 1 | 0 |
| February | 0 | 1 | 0 | 1 |
| July | 1 | 1 | 1 | 1 |
| September | 1 | 0 | 1 | 0 |
Note that: Both the queries won't list any month that has no dates on the original table. If you want to list any month that is not listed on the table with counts 0, you have to modify the anchor query so that it lists all the months even if not listed:
SELECT
m.Name as Month,
i.Year,
i.DateOpened
FROM
(
VALUES ('Janurary'), ('February'), ('March'),
('April'), ('May'), ('June'),
('July'), ('August'), ('September'),
('October'), ('November'), ('December')
) AS m(Name)
LEFT JOIN
(
SELECT
DATENAME(MONTH,DateOpened) as Month,
DATENAME(Year,DateOpened) AS Year,
DateOpened
FROM Incidents
) AS i ON i.Month = m.Name
and replace it in the dynamic query.
Updated SQL Fiddle
This will give you the missing months with zeros results:
| Month | 2014 | 2015 | 2014 | 2015 |
|-----------|------|------|------|------|
| Janurary | 0 | 0 | 0 | 0 | <<
| February | 0 | 1 | 0 | 1 |
| March | 0 | 0 | 0 | 0 | <<
| April | 0 | 3 | 0 | 3 |
| May | 0 | 0 | 0 | 0 | <<
| June | 0 | 0 | 0 | 0 | <<
| July | 1 | 1 | 1 | 1 |
| August | 1 | 1 | 1 | 1 |
| September | 1 | 0 | 1 | 0 |
| October | 0 | 0 | 0 | 0 | <<
| November | 0 | 0 | 0 | 0 | <<
| December | 1 | 0 | 1 | 0 |
A simple create table something like this..
CREATE TABLE #tmpIncidents
(
IncidentName NVARCHAR(50)
, DateOpened DATETIME
)
INSERT INTO #tmpIncidents (IncidentName,DateOpened) VALUES
('Test1',GETDATE()),
('Test1',DATEADD(YEAR,-1,GETDATE())),
('Test1',DATEADD(YEAR,-2,GETDATE())),
('Test1',DATEADD(YEAR,2,GETDATE())),
('Test1',DATEADD(YEAR,1,GETDATE())),
('Test1',DATEADD(YEAR,3,GETDATE()))
Then a build up query for multiple years
DECLARE #columnVar NVARCHAR(4000)
SELECT #columnVar =
(SELECT DISTINCT
'[' + CONVERT(NVARCHAR(150),DATEPART(YEAR,DateOpened)) + '],' AS [text()]
FROM #tmpIncidents
FOR XML PATH('')
)
SET #columnVar = (SELECT LEFT(#columnVar,LEN(#columnVar)-1))
--SELECT #columnVar --so you can see how it looks..
Then execute your query.
EXEC ('
SELECT
pv.*
FROM
(
SELECT DATENAME(MONTH,DateOpened) AS [DateName], DATEPART(YEAR,DateOpened) AS [YEAR], IncidentName FROM #tmpIncidents
) src
PIVOT
(
COUNT(IncidentName)
FOR [YEAR] IN (' + #columnVar + ')
) pv;
')
I have the following problem: I need to implement a calendar using SQL Server 2005. Here's an example of what my stored procedure gives me so far:
TIME | DATE | CALENDAR_ID | SUBJECT | NOTES | STATUS_ID
===================================================================
09:00 | 19/08/2013 | 1 | SUBJECT 1 | NOTES 1 | 1
10:00 | 19/08/2013 | 2 | SUBJECT 2 | NOTES 2 | 2
11:00 | 19/08/2013 | 3 | SUBJECT 3 | NOTES 3 | 3
12:00 | 19/08/2013 | 4 | SUBJECT 4 | NOTES 4 | 1
09:00 | 20/08/2013 | 5 | SUBJECT 5 | NOTES 5 | 4
10:00 | 20/08/2013 | 6 | SUBJECT 6 | NOTES 6 | 3
11:00 | 20/08/2013 | 7 | SUBJECT 7 | NOTES 7 | 1
12:00 | 20/08/2013 | 8 | SUBJECT 8 | NOTES 8 | 1
But I'd like to display it like this:
TIME | 19/08/2013 | 20/08/2013
===============================
09:00 | SUBJECT 1 | SUBJECT 5
10:00 | SUBJECT 2 | SUBJECT 6
11:00 | SUBJECT 3 | SUBJECT 7
12:00 | SUBJECT 4 | SUBJECT 8
I know about the PIVOT function in SQL Server which seems to be useful for these cases, and I searched for examples and explanations, but I still don't understand completely. Besides, so far I've seen only examples like getting the total sum of sales per month; I'm not sure my calendar could use the same logic (or even if I can do what I intend to do using PIVOT). Anyway, could someone point me in the right direction about my problem? Thanks in advance.
Yes you can use the PIVOT function to convert the rows of data into columns. You will just use the aggregate function max or min to select the subject for each date. If you have a limited number of dates that you want to convert into columns then you can hard-code the query:
select [time], [19/08/2013], [20/08/2013]
from
(
select [time], [date], subject
from yourtable
) d
pivot
(
max(subject)
for [date] in ([19/08/2013], [20/08/2013])
) piv;
See SQL Fiddle with Demo
But if you have an unknown number of values, then you can use dynamic SQL in a stored procedure to get the result:
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME(DATE)
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT [time], ' + #cols + '
from
(
select [time], [date], subject
from yourtable
) x
pivot
(
max(subject)
for [date] in (' + #cols + ')
) p '
execute sp_executesql #query;
See SQL Fiddle with Demo. Both will give a result:
| TIME | 19/08/2013 | 20/08/2013 |
-----------------------------------
| 09:00 | SUBJECT 1 | SUBJECT 5 |
| 10:00 | SUBJECT 2 | SUBJECT 6 |
| 11:00 | SUBJECT 3 | SUBJECT 7 |
| 12:00 | SUBJECT 4 | SUBJECT 8 |