NULL Values in Dynamic Pivot Query - sql

I am looking to display the NULL values as blank spaces or dashes to make the resulting data set easier to read as the date range can vary. For example, for a date range of 10 days I could receive NULL values for 5 out of the 10 days for a person's hours. I just want those NULL values to be blank if a person has no hours for that day.
I have seen examples of ISNULL being used on pivot tables, but not a dynamic pivot table. I'm wondering how to specify the NULL replacement for my dynamic list of dates filled into #PivotColumns.
In my query example below, I have omitted non-relevant code such as joins and other informational columns, etc.
SELECT
#PivotColumns = COALESCE(#PivotColumns + ',','') + QUOTENAME(CONVERT(varchar(20),eff_date,110))
FROM
(SELECT DISTINCT
eff_date
FROM
timept
WHERE
eff_date
BETWEEN
#StartDate AND #EndDate) AS TimePivot
ORDER BY
eff_date
SET
#TimeQuery =
'WITH Hours AS (
SELECT
,tp.person_id
,tp.hours
,tp.task_code
,tp.eff_date
,SUM(tp.hours) OVER(PARTITION BY tp.person_id, edr.name, tp.task_code, tp.comments ORDER BY tp.task_code) AS sum_hours
FROM
tables
WHERE
tp.eff_date BETWEEN ''' + #StartDate + ''' AND ''' + #EndDate + '''
)
SELECT
*
FROM
Hours
PIVOT
(SUM(hours)
FOR
eff_date
IN
('+#PivotColumns+')) as p'
EXEC sp_executesql #TimeQuery

Try this:
#TimeQuery =
' WITH Hours AS (
SELECT
,tp.person_id
,ISNULL(tp.hours, '''')
,tp.task_code
,ISNULL(tp.eff_date, '''')
,SUM(tp.hours) OVER(PARTITION BY tp.person_id, edr.name, tp.task_code, tp.comments ORDER BY tp.task_code) AS sum_hours
FROM
tables
WHERE
tp.eff_date BETWEEN ''' + #StartDate + ''' AND ''' + #EndDate + '''
)
SELECT
*
FROM
Hours
PIVOT
(SUM(hours)
FOR
eff_date
IN
('+ #PivotColumns +')) as p'

Related

Dynamic pivot in SQL Server not working as expected

I have the following table:
enter image description here
I'm doing a dynamic PIVOT in SQL_Server and I got the following query:
declare #colunas_pivot as nvarchar(max), #comando_sql as nvarchar(max)
set #colunas_pivot =
stuff((
select
distinct ',' + quotename(datename(year,PLD_Date) + '' + datename(month, PLD_Date))
from TB_Planned
/* where PLD_Date > getdate() */
order by 1
for xml path('')
), 1, 1, '')
print #colunas_pivot
set #comando_sql = '
SELECT * FROM (
SELECT
[PLD_ProjectSapCode],
[PLD_Date],
[PLD_Value]
FROM TB_Planned
) result_pivot
pivot (max(PLD_Value) for PLD_Date in (' + #colunas_pivot + ')) result_pivot
'
print #comando_sql
execute(#comando_sql)
This query results in the following table:
enter image description here
As you can see, I want to PIVOT the column "PLD_Date" and group my columns by month/year, and put the sum of the values ​​corresponding to each month, from the column "PLD_Value.
However, in this result above it is only returning the value of the first day of each month.
How would I group and correctly add the values ​​of the entire month?

SQL Pivot - take a changing date from column and make it a row header

I have a basic query that looks like this.
SELECT Database_Name,
FilingDate,
SUM(ISNULL([column1], 0) + ISNULL(column2], 0) +
ISNULL([column3], 0) + ISNULL([column4], 0)) AS Total
FROM SomeTable(NOLOCK)
GROUP BY Database_Name,
FilingDate
ORDER BY Database_Name,
FilingDate DESC
This query outputs results that look like this.
I would like to take the dates returned in the FilingDate column and use them as new column headers with the totals for each database and date being used as the row content. The end result should look like this:
My research suggests that a pivot is the best option but I'm struggling to find the right way to execute it as my dates change each day Any assistance would be appreciated.
If this is MS SQL, you can use a dynamic pivot table. Here is a solution using your query (should work, but I don't have the base data to test it).
SELECT Database_Name,
FilingDate,
SUM( ISNULL(column1 ,0) +
ISNULL(column2],0) +
ISNULL([column3],0) +
ISNULL([column4],0)
) AS Total
INTO #T1
FROM SomeTable(NOLOCK)
GROUP BY Database_Name,
FilingDate
DECLARE #PivotColumnHeaders varchar(MAX)
SELECT #PivotColumnHeaders =
COALESCE(
#PivotColumnHeaders + ',[' + CAST(UC.FilingDate AS NVARCHAR(10)) + ']',
'[' + CAST(UC.FilingDate AS NVARCHAR(10)) + ']'
)
FROM (SELECT FilingDate FROM #T1 GROUP BY FilingDate) UC
DECLARE #PQuery varchar(MAX) = '
SELECT * FROM (SELECT Database_Name, FilingDate, Total FROM #T1 T0) T1
PIVOT (SUM([Total]) FOR FilingDate IN (' + #PivotColumnHeaders + ') ) AS P'
EXECUTE (#PQuery)
DROP TABLE #T1

SQL Server SORT Dynamic Pivot Column Names

I have an dynamic SQL query which is returning the result as desired but the only problem is the resultant pivot columns are not getting selected in correct order.
I am using SQL Server 2012.
Here is the query:
DECLARE #columns NVARCHAR(MAX), #columns_pivot NVARCHAR(MAX), #sql NVARCHAR(MAX);
SELECT #columns_pivot = COALESCE(#columns_pivot + ', ', '') + QUOTENAME(Week_No)
,#columns = COALESCE(#columns + ', ', '') + 'ISNULL(' + QUOTENAME(Week_No) + ',0) AS ' + QUOTENAME(Week_No) + ''
FROM (SELECT DISTINCT TOP 100 PERCENT DATEPART(wk,T_Date) As Week_No
FROM [VISIT].[dbo].[Report]
WHERE DATEPART(m,T_Date) = 5
ORDER BY Week_No DESC) x; // This query returns Column 'Week_No' in order 22,21,20,19,18 (As Desired)
Edit: //But when Order By Clause is removed it returns 21,18,19,22,20 which is the actual output( Not Desired)
SET #sql = '
SELECT ABC, ' + #columns + '
FROM
(Select TOP 100 PERCENT
ABC
,SUM(CASE WHEN Type = A THEN Sum ELSE 0 END) AS Revenue
,DATEPART(wk,T_Date) As Week_No
FROM [VISIT].[dbo].[Report]
GROUP BY ABC
,DATEPART(wk,T_Date)
ORDER BY Week_No DESC) As j
PIVOT(
max(Revenue)
FOR Week_No in (' + #columns_pivot + ')) As p '
Final result returns columns in the order ABC, 21, 18, 19, 22, 20
But I want the result As ABC, 22, 21, 20, 19, 18.
I saw few posts but was not able to figure out what is wrong with my query. Can someone please point what I have to change in the Query to get the desired output. Where exactly do I have to put the ORDER BY clause
In short when the Select Distinct statement returns the column Week_No in Desc Order why the variables #columns and #columns_pivot don't get it in Desc order
Thanks.
I have solved the issue by storing Week_No in a temp table and apply clustered indexing to it.
Here is the code I added:
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
DROP TABLE #temp
SET NOCOUNT ON;
DECLARE #params nvarchar(max) = :month
SELECT DISTINCT TOP 100 PERCENT DATEPART(wk,Tour_Datum) As Week_No into #temp
FROM [VISITOUR].[dbo].[BI_001_ADM_Report]
WHERE DATEPART(m,Tour_Datum) = #params
--Clustered index is created on week_no in acending order, so that data is physically stored in that way
CREATE CLUSTERED INDEX cix_wekno ON #temp(Week_No ASC)

transpose rows to columns in sql

I have problem in getting the desired output with the SQL query.
My sql data is as follows:
TOTAL Charge PAYMNET A B C D E MonthYear
------- ----------- ----------- --------- -------- ---------- ------- ------- ----------
661 157832.24 82967.80 700.00 10.70 58329.33 0.00 0.00 Oct-2013
612 95030.52 17824.28 850.00 66.10 53971.41 0.00 0.00 Nov-2013
584 90256.35 16732.91 700.00 66.10 52219.87 0.00 0.00 Dec-2013
511 72217.32 12336.12 285.00 53.17 42951.12 0.00 0.00 Jan-2014
I need the output as follows,
Data Jan-2013 Feb-2013 Mar-2013
TOTALCOUNT 761 647 671
Charge 126888 119995 151737.5
Payment 25705.4 26235.47 28704.41
A 1089.08 1020 745
B 2100.4 1947.25 1868.22
C 94246.55 84202.15 115673.7
D 0 0 0
E 0 0 0
I have seen the examples of pivot and unpivot, in pivot I don't get the column headers as row data, and in unpivot I didn't found an example where I can transpose multiple columns. I have another option to get this result in the code. But I want to know is it possible to get this kind of result in sql?
Edit
The result will give only for 3 or 4 months, not more than that.
Update : The first sample data is the actual data which I will get as a result of multiple joins and grouping on multiple tables, which I will store into a temp table. I tried to get the required result by modifying the query which is not possible because of the table structure. I managed to get the result as in the first sample data, but this is not what the client want to see!!! So I need to process the temp table data which will be only 3 to 4 rows into required output. The query to get the first result is select * from temp. The processing needs to be done on temp table result.
Update-2
I have tried the following query
declare #cols varchar(max)
select #cols = STUFF((select ', ' + MonthYear
from #tmp for xml path('')),1,1,'')
declare #query varchar(max)
set #query =
'select ''TOTAL'' as Data,' +#cols+' from
(select MonthYear,TOTALCLAIMS from #tmp)st
pivot
(
MAX(TOTAL) for MonthYear in (' + #cols + ')
)pt;'
Which gave me the first row correctly!!! But I tried to use union as
set #query =
'select ''TOTAL'' as Data,' +#cols+' from
(select MonthYear,TOTALCLAIMS from #tmp)st
pivot
(
MAX(TOTAL) for MonthYear in (' + #cols + ')
)pt;
union
select ''CHARGES'' as Data,' +#cols+' from
(select MonthYear,TOTALCLAIMS from #tmp)st
pivot
(
MAX(CHARGES) for MonthYear in (' + #cols + ')
)pt;'
Which gives an error as incorrect syntax near union. Any one know how to union pivot results? Or is there any better way to do this?
Thank You.
I have tried this code. Please check and let me know if it works
I know that it doesnt look so good. Also not sure how it will be performance wise.
--Can have more columns like A,B,...
DECLARE #tbl TABLE
(
TOTAL INT,
CHARGE FLOAT,
PAYMENT FLOAT,
MONTHYEAR VARCHAR(50)
)
--Test data
INSERT INTO #tbl SELECT 661, 157832.24, 82967.80, 'Oct2013'
INSERT INTO #tbl SELECT 612, 95030.52, 17824.28, 'Nov2013'
INSERT INTO #tbl SELECT 584 ,90256.35, 16732.91, 'Dec2013'
--Can be a physical table
CREATE TABLE #FinalTbl
(
DATA VARCHAR(100)
)
--inserted hardcode records in data column. To add it dynamically you would need to loop through information_schema.columns
--SELECT *
--FROM information_schema.columns
--WHERE table_name = 'tbl_name'
INSERT INTO #FinalTbl
VALUES ('TOTAL')
INSERT INTO #FinalTbl
VALUES ('CHARGE')
INSERT INTO #FinalTbl
VALUES ('PAYMENT')
DECLARE #StartCount INT, #TotalCount INT, #Query VARCHAR(5000), #TOTAL INT,#CHARGE FLOAT,#PAYMENT FLOAT,#MONTHYEAR VARCHAR(50)
SELECT #TotalCount = COUNT(*) FROM #tbl;
SET #StartCount = 1;
WHILE(#StartCount <= #TotalCount)
BEGIN
SELECT #TOTAL = TOTAL,
#CHARGE = CHARGE,
#PAYMENT = PAYMENT,
#MONTHYEAR = MONTHYEAR
FROM
(SELECT ROW_NUMBER() over(ORDER BY MONTHYEAR) AS ROWNUM, * FROM #tbl) as tbl
WHERE ROWNUM = #StartCount
SELECT #Query = 'ALTER TABLE #FinalTbl ADD ' + #MONTHYEAR + ' VARCHAR(1000)'
EXEC (#Query)
SELECT #Query = 'UPDATE #FinalTbl SET ' + #MONTHYEAR + ' = ''' + CONVERT(VARCHAR(50), #TOTAL) + ''' WHERE DATA = ''TOTAL'''
EXEC (#Query)
SELECT #Query = 'UPDATE #FinalTbl SET ' + #MONTHYEAR + ' = ''' + CONVERT(VARCHAR(50), #CHARGE) + ''' WHERE DATA = ''CHARGE'''
EXEC (#Query)
SELECT #Query = 'UPDATE #FinalTbl SET ' + #MONTHYEAR + ' = ''' + CONVERT(VARCHAR(50), #PAYMENT) + ''' WHERE DATA = ''PAYMENT'''
EXEC (#Query)
SELECT #StartCount = #StartCount + 1
END
SELECT * FROM #FinalTbl
DROP TABLE #FinalTbl
Hope this helps
I would imagine the reason you are only getting 3 or 4 months is because you don't have data for the missing months? If you want to display columns for missing months you will need to either:
Create a Table datatype with all the months you want to display
and left join the remainder of the tables to it in your query. You
could then use the PIVOT function as normal.
If you know how many columns up front i.e. one for each month in a particular year and it won't change, you can simply use CASE
Statements (one for each month) to transpose the data without the
PIVOT operator.
I can provide examples if needed.
Select Month(Mdate)md,'A' AS Col,sum(A) as a from Product group by Month(MDate)
union all
Select Month(Mdate)md,'B',sum(b) as a from Product group by Month(MDate)
union all
Select Month(Mdate)md,'C',sum(c) as a from Product group by Month(MDate)
union all
Select Month(Mdate)md,'D',Count(A) as a from Product group by Month(MDate)
Try Pivot with the above query you may to get required result....

change the column name while selecting from dyanamic table

Hi I have attendence query which will generate the attendence report with using PIVOT function
Here's the procedure :
declare #in_date DATETIME
/*Select all the stagign entries related to promotion id and investment type id */
/* also only those staging daat related interface status tracking*/
-- Getting all distinct dates into a temporary table #Dates
SELECT a.date as full_date_of_attendence INTO #Dates
FROM dbo.getFullmonth(#in_date) a
ORDER BY a.date
-- The number of days will be dynamic. So building
-- a comma seperated value string from the dates in #Dates
SELECT #cols = COALESCE(#cols + ',[' + CONVERT(varchar, full_date_of_attendence, 106)
+ ']','[' + CONVERT(varchar, full_date_of_attendence, 106) + ']')
FROM #Dates
ORDER BY full_date_of_attendence
--select #cols
---- Building the query with dynamic dates
SET #qry =
'SELECT * FROM
(SELECT admission_id, attendence_status , date_of_attendence
FROM dbo.tblattendence)emp
PIVOT (MAX(attendence_status) FOR date_of_attendence IN (' + #cols + ')) AS stat'
-- Executing the query
EXEC(#qry)
-- Dropping temporary tables
DROP TABLE #Dates
here is the output of the above query::
admission_id 01 May 2013 02 May 2013 03 May 2013
2 NULL 1 0
3 NULL 1 1
4 NULL 0 0
5 NULL 0 1
Here I want to change the names of the columns as 01,02,03......
and I want the values 1 as 'P' and 0 as 'A'
can anyone would help me to achieve this ??
I would suggest the following changes to your code. If you want a list of the days (1, 2, 3, etc), then you can use the DAY function.
Typically when I get a list of columns dynamically, my preference is using STUFF and FOR XML PATH, I would alter that code to the following:
select #colsPiv = STUFF((SELECT ',' + QUOTENAME(cast(day(full_date_of_attendence) as varchar(2)))
from #Dates
GROUP BY full_date_of_attendence
ORDER BY full_date_of_attendence
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
Then if you want to replace the 0 with an A and a 1 with a P, you will want to create a query to get a list of columns to replace the values:
select #colsSel = STUFF((SELECT ', case when ' + QUOTENAME(cast(day(full_date_of_attendence) as varchar(2)))+'= 1 then ''P'' else ''A'' end as '+QUOTENAME(cast(day(full_date_of_attendence) as varchar(2)))
from #Dates
GROUP BY full_date_of_attendence
ORDER BY full_date_of_attendence
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
Basically, this is creating a select list similar to this:
select case when [1] = 1 then 'P' else 'A' end as [1], ...
Then your final query will be:
SET #qry =
'SELECT admission_id, '+#colsSel +'
FROM
(
SELECT admission_id,
attendence_status ,
day(date_of_attendence) date_of_attendence
FROM dbo.tblattendence
)emp
PIVOT
(
MAX(attendence_status)
FOR date_of_attendence IN (' + #colsPiv + ')
) AS stat'
See SQL Fiddle with Demo
Let's change just the two things you wanted to, i.e.
CONVERT(CHAR(2), full_date_of_attendence, 106) -- use CHAR(2) instead of varchar
CASE attendence_status when 1 then 'P' else 'A' END in the SELECT...
The code with minimal changes. Hope this helps you see how you can make similar changes in future to other code.
declare #in_date DATETIME
/*Select all the stagign entries related to promotion id and investment type id */
/* also only those staging daat related interface status tracking*/
-- Getting all distinct dates into a temporary table #Dates
SELECT a.date as full_date_of_attendence INTO #Dates
FROM dbo.getFullmonth(#in_date) a
ORDER BY a.date
-- The number of days will be dynamic. So building
-- a comma seperated value string from the dates in #Dates
SELECT #cols = COALESCE(#cols + ',', '') + [' +
CONVERT(CHAR(2), full_date_of_attendence, 106) + ']'
FROM #Dates
ORDER BY full_date_of_attendence
--select #cols
---- Building the query with dynamic dates
SET #qry =
'SELECT * FROM
(SELECT admission_id, CASE attendence_status when 1 then 'P' else 'A' END, date_of_attendence
FROM dbo.tblattendence)emp
PIVOT (MAX(attendence_status) FOR date_of_attendence IN (' + #cols + ')) AS stat'
-- Executing the query
EXEC(#qry)
-- Dropping temporary tables
DROP TABLE #Dates