I have the following SQL code that creates a very useful pivot table:
Use [A1_20132014]
DECLARE #cols NVARCHAR (MAX)
SELECT #cols = COALESCE (#cols + ',[' + Link_ID + ']', '[' + Link_ID + ']')
FROM (SELECT DISTINCT Link_ID FROM A1) PV
ORDER BY Link_ID
DECLARE #query NVARCHAR(MAX)
SET #query = 'SELECT * FROM
(
-- We will select the data that has to be shown for pivoting
SELECT date_1, StartHour,Cost, Link_ID
FROM A1
WHERE Section = (''1AB'')
) x
PIVOT
(
-- Values in each dynamic column
SUM(Cost)
-- Select columns from #cols
FOR Link_ID IN (' + #cols + ')
) p;'
EXEC SP_EXECUTESQL #query
from these headings
link_id Section date_1 StartHour Cost data_source
4000000027866016A 8NB 2013-09-02 6 5871 1
4000000027866017B 5EB 2013-10-09 9 8965 2
4000000027856512B 4TB 2013-05-06 15 6754 1
4000000027866015A 6HB 2013-06-08 8 5354 1
4000000027866011A 1AB 2013-06-09 11 2 1
with these source types;
link_Id nvarchar(50)
Section nvarchar(50)
Date_1 smalldatetime
StartHour int
Cost float
data_source int
However despite WHERE clause that specifies a certain section unfortunately ALL sections still appear in the pivot table but populated with NULL values all the way down.
Is there a way of completely excluding the columns that do not meet the WHERE clause?
Thanks for any help.
Henry.
Put the where clause in the subquery. That way you'll only get columns that apply to 1AB
SELECT #cols = COALESCE (#cols + ',[' + Link_ID + ']', '[' + Link_ID + ']')
FROM (SELECT DISTINCT Link_ID FROM A1 WHERE Section = '1AB') PV
ORDER BY Link_ID
Related
I have a table that looks like this:
Agent_id
break_id
time
1
1
15
1
2
12
1
2
12
I used pivot to get this structure:
Agent_id
1
2
1
15
24
The problem is that I need to get the count for the pivoted columns, in the example I need to have a structure like this:
Agent_id
1
2
count1
count2
1
15
24
1
2
And I'm not sure on how to do it ... this is the query so far.
DECLARE #COLUMNS VARCHAR(MAX)
DECLARE #QUERY nVARCHAR(MAX)
SELECT #COLUMNS = COALESCE(#COLUMNS + ', ','') + QUOTENAME([break_id])
FROM
(SELECT DISTINCT [break_id] FROM test) AS B
ORDER BY B.[break_id]
SET #QUERY = '
SELECT agent_id,
'+#COLUMNS+'
FROM (
SELECT TOP (1000)
agent_id,break_id,time_inbreak
FROM test
) as pivotData
PIVOT (
SUM(time_inbreak)
FOR break_id IN ('+#COLUMNS+')
) as pivotResult
'
EXEC sp_executesql #QUERY
Any help is greatly appreciated
Unfortunately, PIVOT can only pivot a single column. But we can do multiple columns using conditional aggregation SUM(CASE WHEN... and COUNT(CASE WHEN...:
DECLARE #COLUMNS VARCHAR(MAX)
DECLARE #QUERY nVARCHAR(MAX)
SELECT #COLUMNS = COALESCE(#COLUMNS + ', ','') +
'Sum' + QUOTENAME([break_id]) +
' = SUM(CASE WHEN break_id = ' + break_id + ' THEN time_inbreak END), Count' +
QUOTENAME([break_id]) + ' = COUNT(CASE WHEN break_id = ' + break_id + ' THEN 1 END)'
FROM
(SELECT DISTINCT [break_id] FROM test) AS B
ORDER BY B.[break_id]
OPTION (MAXDOP 1);
SET #QUERY = '
SELECT agent_id,
'+#COLUMNS+'
FROM (
SELECT TOP (1000)
agent_id,break_id,time_inbreak
FROM test
) as pivotData
GROUP BY agent_id;
';
PRINT #QUERY
EXEC sp_executesql #QUERY
I must say, I'm not sure how safe it is to aggregate the columns like that, especially in the face of parallelism. Preferably use STRING_AGG or FOR XML PATH(''), TYPE. At the very least I have added OPTION (MAXDOP 1) to prevent parallelism
I have a table with around 10 rows. I want to pivot on all values in one column to a one-row multi column result. It looks as though there is no way to get around the "For ContactTypeID in ([1],[2])" syntax.
ContactTypeID int
ContactType varchar(20)
Sample data:
1 Customer
2 Vendor
...
5 BillTo
I want to return a single row with
Customer Vendor BillTo, etc
1 2 5
But like I said, I don't want to have to specify each ContactTypeID by number. Is there way to specify "for all"?
Thank you.
You need a dynamic pivot.
Here's the code, for your reference. Hope it helps.
CREATE TABLE tablename (ContactTypeID int, ContactType varchar(20));
INSERT INTO tablename VALUES (1, 'Customer'), (2, 'Vendor'), (5, 'BillTo');
DECLARE #cols NVARCHAR (MAX);
SELECT #cols = COALESCE (#cols + ',[' + ContactType + ']',
'[' + ContactType + ']')
FROM (SELECT DISTINCT [ContactType] FROM tablename) PV
ORDER BY [ContactType]
DECLARE #query NVARCHAR(MAX)
SET #query = '
SELECT * FROM
(
SELECT * FROM tablename
) x
PIVOT
(
MIN(ContactTypeID)
FOR [ContactType] IN (' + #cols + ')
) p
'
EXEC SP_EXECUTESQL #query;
This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 6 years ago.
I'm struggling with an error while making a dynamic pivot table
The source data is
JobID | SalesForMonth | YearMonth
7734 | 400 | 2016-12
7734 | 350 | 2017-01
8540 | 444 | 2016-12
8540 | 300 | 2017-01
and aiming for
JobID | 2016-12 | 2017-01
7734 | 400 | 350
8540 | 444 | 300
and I've tried to use a query I found on here to create the column headers. But must admit I don't really understand the 'For XML' line and getting a syntax error there on line 6
DECLARE
#cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
SELECT #cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(YearMonth)
FROM v_JobSalesByMonth
FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'')
SELECT #query =
'SELECT * FROM
(SELECT JobID, YearMonth, SalesForMonth
FROM v_JobSalesByMonth) X
PIVOT
(
(JobID, SalesForMonth)
for [YearMonth] in (' + #cols + ')
) P'
I'd also like to stick in a 'total sales' for the jobID column
Any help would be much appreciated
Declare #SQL varchar(max) = Stuff((Select Distinct ',' + QuoteName(YearMonth) From v_JobSalesByMonth Order by 1 For XML Path('')),1,1,'')
Select #SQL = '
Select [JobID],[TotalSales],' + #SQL + '
From (
Select JobID
,TotalSales = sum(SalesForMonth) over (Partition By JobID)
,YearMonth
,SalesForMonth
From v_JobSalesByMonth A
) A
Pivot (sum(SalesForMonth) For [YearMonth] in (' + #SQL + ') ) p'
Exec(#SQL);
Returns
EDIT - Dynamically Create View
Since you can't have dynamic SQL in a view, you could have job scheduled (daily or monthly) to drop and re-create the view.
if object_id('vw_SalesByMonth','v') is not null
drop view vw_SalesByMonth;
Declare #SQL varchar(max) = Stuff((Select Distinct ',' + QuoteName(YearMonth) From Yourtable Order by 1 For XML Path('')),1,1,'')
Select #SQL = '
Create View vw_SalesByMonth
AS
Select [JobID],[TotalSales],' + #SQL + '
From (
Select JobID
,TotalSales = sum(SalesForMonth) over (Partition By JobID)
,YearMonth
,SalesForMonth
From YourTable A
) A
Pivot (sum(SalesForMonth) For [YearMonth] in (' + #SQL + ') ) p'
Exec(#SQL);
Select * from vw_SalesByMonth
I have a table with the following structure where there are different types of results for a "SymbolQuoteID".
AnalysisResultID SymbolQuoteID ResultTypeID Result Updated
11 368 1 48.6750775191538 2015-10-08 22:09:15.680
12 368 2 47.7401046493826 2015-10-08 22:09:15.680
13 368 3 47.3978529584944 2015-10-08 22:09:15.680
14 368 4 43.4227483517635 2015-10-08 22:09:15.680
15 369 1 44.9316592945153 2015-10-08 22:09:15.680
16 369 2 48.8348167760945 2015-10-08 22:09:15.680
17 369 3 51.6463393199821 2015-10-08 22:09:15.680
18 369 4 51.7885923247485 2015-10-08 22:09:15.680
I need to SELECT the data so it is formatted like this where ResultTypeID is the column header and the corresponding Result column value is the column data. There is no aggregation, just the Result value. Also, the number of values for ResultTypeID will be consistent w/in a single query but may vary from query to query. For example, one time they might be 1 through 4 and another time 1 through 7, etc. Therefore, the number of columns in the pivoted table will change based upon the data.
SymbolQuoteID 1 2 3 4
368 48.6750775191538 47.7401046493826 47.3978529584944 43.4227483517635
369 44.9316592945153 48.8348167760945 51.6463393199821 51.7885923247485
It seems PIVOT might work but I've not used it before. Any help appreciated.
You can use dynamic PIVOT:
SQL Fiddle
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX);
SET #cols =
STUFF((
SELECT DISTINCT ',' + QUOTENAME(ResultTypeID)
FROM tbl
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)')
, 1, 1, '')
SET #query =
'SELECT SymbolQuoteID, ' + #cols + '
FROM (
SELECT
SymbolQuoteID, ResultTypeID, Result
FROM tbl
)t
PIVOT
(
MAX(Result)
FOR ResultTypeID IN (' + #cols + ')
) p '
PRINT (#query)
EXEC (#query)
You can also do this using dynamic crosstab:
SQL Fiddle
DECLARE #maxResultTypeId INT
SELECT TOP 1 #maxResultTypeId = ResultTypeId FROM tbl ORDER BY ResultTypeID DESC
DECLARE #sql NVARCHAR(MAX) = ''
SELECT #sql =
'SELECT
SymbolQuoteID' + CHAR(10)
SELECT #sql = #sql +
' , MAX(CASE WHEN ResultTypeId = ' + CONVERT(VARCHAR(3), rn) + ' THEN Result END) AS ' + QUOTENAME(rn) + CHAR(10)
FROM(
SELECT TOP(#maxResultTypeId)
ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rn
FROM sys.columns
)t
SELECT #sql = #sql +
'FROM tbl
GROUP BY SymbolQuoteID'
PRINT (#sql)
EXEC (#sql)
SELECT * FROM
(
SELECT SymbolQuoteID
,ResultTypeID
,Result
FROM TableName ) t
PIVOT (SUM(Result)
FOR ResultTypeID
IN ([1],[2],[3],[4])
)p
I have a table in SQL Server 2012 which has 5 million rows.
Table view is like this :
CustomerID ProdID FavouriteProduct
1 A A
1 A A
1 A A
1 B A
1 A A
1 A A
1 A A
1 B A
2 A C
2 AN C
2 G C
2 C C
2 C C
2 F C
2 D C
2 C C
As you can see there are so many different products.
I already wrote a query for that :
DECLARE #DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE #ColumnName AS NVARCHAR(MAX)
--Get distinct values of the PIVOT Column
SELECT
#ColumnName = ISNULL(#ColumnName + ',','') + QUOTENAME(prodID)
FROM
(SELECT DISTINCT ProdID FROM Table) AS Prods
--Prepare the PIVOT query using the dynamic
SET #DynamicPivotQuery =
N'SELECT CustomerID, ' + #ColumnName + '
FROM table
PIVOT(count(CustomerID)
FOR ProdID IN (' + #ColumnName + ')) AS PVTTable'
--Execute the Dynamic Pivot Query
EXEC sp_executesql #DynamicPivotQuery
Normally, I expect count(*) or Count(SubID) would count the number of each products bought by each customer, but it doesn't. It says
Invalid Column name CustomerID
instead.
You can't count the CustomerId while selecting it, try this instead:
SET #DynamicPivotQuery =
N'SELECT CustomerID, ' + #ColumnName + '
FROM table
PIVOT(count(FavouriteProduct)
FOR ProdID IN (' + #ColumnName + ')) AS PVTTable'
You need a column to count, and CustomerId is not allowed since you are selecting it. If FavouriteProduct fails, I suggest you fake a column or find another column.
Since you have more columns than described, I modified query. Try this instead:
SET #DynamicPivotQuery =
N'SELECT CustomerID, ' + #ColumnName + '
FROM
(SELECT CustomerID, FavouriteProduct, ProdID FROM table) x
PIVOT(count(FavouriteProduct)
FOR ProdID IN (' + #ColumnName + ')) AS PVTTable'