SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


No record count in PIVOT


No record count in PIVOT

Author
Message
michielbijnen
michielbijnen
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 145
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 kstpl
where LEFT(kstplcode, 1) in ('B', 'S')

set @Query =

N'
declare @DaysMinus int
set @DaysMinus = 8

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 Costcenter

from humres h
left 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.kstplcode

where isnull(h.ldatuitdienst, getdate()+1) > pd.eddatum - @DaysMinus - and h.res_id > 5000
) as data

pivot (count(data.Resource) for data.Costcenter in ('+ @Columns +')) as pivottable
'

exec(@Query)
CapnHector
CapnHector
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1327 Visits: 1789
it looks like there is an extra minus sign in your query at where
isnull(h.ldatuitdienst, getdate()+1) > pd.eddatum - @DaysMinus -


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


For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2
michielbijnen
michielbijnen
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 145
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 ....
mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1558 Visits: 3317
Hi

You may want to check you @columns variable. It could have a comma at the beginning
michielbijnen
michielbijnen
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 145
There is a comma! As far as I know the syntax is correct for the columns variable. Or not?
mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1558 Visits: 3317
michielbijnen (11/12/2012)
There is a comma! As far as I know the syntax is correct for the columns variable. Or not?


As an example
select 'Count IsPrimary', [0], [1]
from (select isprimary, blockid from block) b
pivot (
count(blockid)
for isprimary in ([0],[1])
) p

Will work

Based on a quick glance at you procedure I'm guessing you producing something like
select 'Count IsPrimary',, [0], [1]
from (select isprimary, blockid from block) b
pivot (
count(blockid)
for isprimary in (,[0],[1])
) p

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.
CapnHector
CapnHector
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1327 Visits: 1789
michielbijnen (11/12/2012)
There is a comma! As far as I know the syntax is correct for the columns variable. Or not?


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.


For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2
CapnHector
CapnHector
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1327 Visits: 1789
mickyT (11/12/2012)
michielbijnen (11/12/2012)
There is a comma! As far as I know the syntax is correct for the columns variable. Or not?


As an example
select 'Count IsPrimary', [0], [1]
from (select isprimary, blockid from block) b
pivot (
count(blockid)
for isprimary in ([0],[1])
) p

Will work

Based on a quick glance at you procedure I'm guessing you producing something like
select 'Count IsPrimary',, [0], [1]
from (select isprimary, blockid from block) b
pivot (
count(blockid)
for isprimary in (,[0],[1])
) p

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.


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 ",").


For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2
mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1558 Visits: 3317
capnhector (11/12/2012)


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 ",").

My bad sorry ... didn't read it properlyErmm
michielbijnen
michielbijnen
SSC Journeyman
SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)SSC Journeyman (89 reputation)

Group: General Forum Members
Points: 89 Visits: 145
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!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search