﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / No record count in PIVOT / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 01:48:48 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>Thanks for your reply!I do something wrong but not sure what ...I think the @SumColumns variable is not correct.Hope you can help me out? ThanksHere is my query:DECLARE @Columns VARCHAR(MAX)DECLARE @SumColumns VARCHAR(MAX)DECLARE @Query nVARCHAR(MAX)	SELECT @Columns = ISNULL(@Columns + ',[' + kstplcode + ']', '[' + kstplcode + ']')FROM kstplWHERE Enabled = 1SELECT @SumColumns = ISNULL(@Columns + ',[' + @Columns + ']', '[' + @Columns + ']')FROM kstplWHERE Enabled = 1SET @Query =N'DECLARE @DaysMinus INTSET @DaysMinus = 18;--WITH BasePivot as(SELECT	''No of employees per '' + convert(VARCHAR(10), PeriodEnd, 105) as KPI,		' + @Columns + 'FROM(SELECT	h.res_id as Resource,		pd.eddatum as PeriodEnd,		h.costcenter as Costcenter		--kp.oms25_0 as CComsFROM	humres hLEFT OUTER JOIN perdat pd ON convert(VARCHAR(10), GETDATE()- @DaysMinus, 105) = convert(VARCHAR(10), pd.eddatum, 105)LEFT OUTER JOIN kstpl kp ON h.costcenter = kp.kstplcodeWHERE	ISNULL(h.ldatuitdienst, getdate()+1) &amp;gt; pd.eddatum - @DaysMinus /* Job runs on the last day of period */		AND h.res_id &amp;gt; 5000GROUP BY pd.eddatum, h.costcenter, h.res_id) as dataPIVOT	(count(data.Resource) FOR data.Costcenter IN (' + @Columns + ')) as pivottable)SELECT --KPI, ' + @Columns + ' FROM BasePivotKPI, ' + @Columns + ', ' + @SumColumns + ' FROM pivottable'EXEC (@Query)</description><pubDate>Thu, 22 Nov 2012 04:06:01 GMT</pubDate><dc:creator>michielbijnen</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>same concept as the total rows except instead of sum and you would use a second column variable to dynamically create column1 + column2 ... then instead of using a CTE you would instead use the working pivot table and just add that calculated column to the end of your select.</description><pubDate>Mon, 19 Nov 2012 06:07:16 GMT</pubDate><dc:creator>CapnHector</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>Thanks a lot, but this adds a total row to the pivot.What I would like to have is a total column at the end of the row that contains the total for that row.I'm a bit confused about the results that I come across in Google. It didn't work out so far with my query.Could you help me out with this one?Thanks!</description><pubDate>Mon, 19 Nov 2012 03:08:18 GMT</pubDate><dc:creator>michielbijnen</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>I would Suggest using a CTE to hold the pivot table then select your data and union it to a totals query.This will get you close, i changed your dynamic sql a bit to use my tally table to give me some output i could use so you will have to modify this slightly.[code="sql"]declare @Columns varchar(max)declare @Query nvarchar(max)DECLARE @SumColumns NVARCHAR(MAX)select @Columns = isnull(@Columns + ',[' + CAST(N AS VARCHAR) + ']', '[' + CAST(N AS VARCHAR) + ']')from Tallywhere N &amp;lt; 9select @SumColumns = isnull(@SumColumns + ',SUM([' + CAST(N AS VARCHAR) + '])', 'SUM([' + CAST(N AS VARCHAR) + '])')from Tallywhere N &amp;lt; 9SELECT @Query =N'declare @DaysMinus intset @DaysMinus = 8;WITH BasePvt AS (select	''No of employees per '' + convert(varchar(10), PeriodEnd, 105) as KPI,' + @Columns + 'from(select	h.res_id as Resource,pd.eddatum as PeriodEnd,kp.oms25_0 as Costcenterfrom	humres hleft outer join perdat pd on convert(varchar(10), GETDATE()- @DaysMinus, 105) = convert(varchar(10), pd.eddatum, 105)left outer join kstpl kp on h.costcenter = kp.kstplcodewhere	isnull(h.ldatuitdienst, getdate()+1) &amp;gt; pd.eddatum - @DaysMinus -	 and h.res_id &amp;gt; 5000) as datapivot	(count(data.Resource) for data.Costcenter in ('+ @Columns +')) as pivottable)SELECT KPI,' + @Columns + ' FROM BasePivotUNIONSELECT ''Totals'', ' + @SumColumns + 'FROM BasePivot'SELECT @Query[/code]</description><pubDate>Wed, 14 Nov 2012 16:20:02 GMT</pubDate><dc:creator>CapnHector</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>What's now the best way to add a grand total column at the end of the cross tab?</description><pubDate>Wed, 14 Nov 2012 15:20:14 GMT</pubDate><dc:creator>michielbijnen</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>Sorry!!A bit of a beginners fault :blush: .... the field "kstplcode" in the @columns variable was not the same as the field "oms25_0" in the data-query! :doze:Thanks for your help, anyway!</description><pubDate>Tue, 13 Nov 2012 14:21:29 GMT</pubDate><dc:creator>michielbijnen</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>[quote][b]capnhector (11/12/2012)[/b][hr]nope he handles the leading "," with the initial ISNULL.  the OP does not initialize the @columns variable so NULL + ',SOMETHING' is NULL and the ISNULL function returns the second value (which does not have the leading ",").[/quote]My bad sorry ... didn't read it properly:ermm:</description><pubDate>Mon, 12 Nov 2012 15:44:56 GMT</pubDate><dc:creator>mickyT</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>[quote][b]mickyT (11/12/2012)[/b][hr][quote][b]michielbijnen (11/12/2012)[/b][hr]There is a comma! As far as I know the syntax is correct for the columns variable. Or not?[/quote]As an example[code="sql"]select 'Count IsPrimary', [0], [1]from (select isprimary, blockid from block) b	pivot (	count(blockid)	for isprimary in ([0],[1])	) p[/code]Will workBased on a quick glance at you procedure I'm guessing you producing something like[code="sql"]select 'Count IsPrimary',[b],[/b] [0], [1]from (select isprimary, blockid from block) b	pivot (	count(blockid)	for isprimary in ([b],[/b][0],[1])	) p[/code]Which will generate a syntax error.If you trim the leading comma off the columns you query should start working.  But that is a guess as I can't test it.  You may want to change exec(@query) to select @query and post the actual query that is being generated.[/quote]nope he handles the leading "," with the initial ISNULL.  the OP does not initialize the @columns variable so NULL + ',SOMETHING' is NULL and the ISNULL function returns the second value (which does not have the leading ",").</description><pubDate>Mon, 12 Nov 2012 15:30:19 GMT</pubDate><dc:creator>CapnHector</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>[quote][b]michielbijnen (11/12/2012)[/b][hr]There is a comma! As far as I know the syntax is correct for the columns variable. Or not?[/quote]every thing in the dynamic sql is correct since the extra "-" is from a comment (you deal with the leading comma a little differently than i would but it works and that is what matters).  so now in order to figure out what is going on we are going to need sample data.  try running all the queries and make sure you are getting the expected results.  Try running SELECT @Columns after you build it and running the base select of your pivot.  those are the areas where a problem can occur since the syntax of your dynamic pivot looks fine.</description><pubDate>Mon, 12 Nov 2012 15:28:46 GMT</pubDate><dc:creator>CapnHector</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>[quote][b]michielbijnen (11/12/2012)[/b][hr]There is a comma! As far as I know the syntax is correct for the columns variable. Or not?[/quote]As an example[code="sql"]select 'Count IsPrimary', [0], [1]from (select isprimary, blockid from block) b	pivot (	count(blockid)	for isprimary in ([0],[1])	) p[/code]Will workBased on a quick glance at you procedure I'm guessing you producing something like[code="sql"]select 'Count IsPrimary',[b],[/b] [0], [1]from (select isprimary, blockid from block) b	pivot (	count(blockid)	for isprimary in ([b],[/b][0],[1])	) p[/code]Which will generate a syntax error.If you trim the leading comma off the columns you query should start working.  But that is a guess as I can't test it.  You may want to change exec(@query) to select @query and post the actual query that is being generated.</description><pubDate>Mon, 12 Nov 2012 15:27:59 GMT</pubDate><dc:creator>mickyT</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>There is a comma! As far as I know the syntax is correct for the columns variable. Or not?</description><pubDate>Mon, 12 Nov 2012 15:10:38 GMT</pubDate><dc:creator>michielbijnen</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>Hi You may want to check you @columns variable.  It could have a comma at the beginning</description><pubDate>Mon, 12 Nov 2012 14:40:59 GMT</pubDate><dc:creator>mickyT</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>Sorry, I omitted a remark at the end of the line, but I left one minus-sign there. My fault! My original query is correct and does not have this minus-sign.I'm still wondering what it is ....</description><pubDate>Mon, 12 Nov 2012 13:50:00 GMT</pubDate><dc:creator>michielbijnen</dc:creator></item><item><title>RE: No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>it looks like there is an extra minus sign in your query at where	[code="sql"]isnull(h.ldatuitdienst, getdate()+1) &amp;gt; pd.eddatum - @DaysMinus -[/code]  Other than that with out some sample data it will be hard to dianose what the issue is.The other issue is that i may look at hard coding the @DaysMinus or concantenate that value in to the dynamic sql query instead of declaring a variable in the dynamic SQL</description><pubDate>Mon, 12 Nov 2012 13:34:57 GMT</pubDate><dc:creator>CapnHector</dc:creator></item><item><title>No record count in PIVOT</title><link>http://www.sqlservercentral.com/Forums/Topic1383584-392-1.aspx</link><description>Hi,I have the following query to count the number of employees per costcenter using the pivot command.The @Columns parameter declares these costcenters.The query shows results but the problem is that the count is zero for every costcenter! When I do "count(*)", it has the same problem.Even a night of proper sleep didn't brought up a light.What's wrong with the count in the pivot command?I think it's simple quite but I haven't figured it out yet ...Thanks for your help!declare @Columns varchar(max)declare @Query nvarchar(max)	select @Columns = isnull(@Columns + ',[' + kstplcode + ']', '[' + kstplcode + ']')from kstplwhere LEFT(kstplcode, 1) in ('B', 'S')set @Query =N'declare @DaysMinus intset @DaysMinus = 8select	''No of employees per '' + convert(varchar(10), PeriodEnd, 105) as KPI,	' + @Columns + 'from(select	h.res_id as Resource,	pd.eddatum as PeriodEnd,	kp.oms25_0 as Costcenterfrom	humres hleft outer join perdat pd on convert(varchar(10), GETDATE()- @DaysMinus, 105) = convert(varchar(10), pd.eddatum, 105)left outer join kstpl kp on h.costcenter = kp.kstplcodewhere	isnull(h.ldatuitdienst, getdate()+1) &amp;gt; pd.eddatum - @DaysMinus -		and h.res_id &amp;gt; 5000) as datapivot	(count(data.Resource) for data.Costcenter in ('+ @Columns +')) as pivottable'exec(@Query)</description><pubDate>Mon, 12 Nov 2012 03:32:28 GMT</pubDate><dc:creator>michielbijnen</dc:creator></item></channel></rss>