Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

No record count in PIVOT Expand / Collapse
Author
Message
Posted Monday, November 12, 2012 3:32 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 12:50 PM
Points: 42, Visits: 130
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)
Post #1383584
Posted Monday, November 12, 2012 1:34 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 5:16 AM
Points: 946, Visits: 1,774
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

Jeremy Oursler
Post #1383856
Posted Monday, November 12, 2012 1:50 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 12:50 PM
Points: 42, Visits: 130
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 ....
Post #1383863
Posted Monday, November 12, 2012 2:40 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 1:00 PM
Points: 1,026, Visits: 3,073
Hi

You may want to check you @columns variable. It could have a comma at the beginning
Post #1383886
Posted Monday, November 12, 2012 3:10 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 12:50 PM
Points: 42, Visits: 130
There is a comma! As far as I know the syntax is correct for the columns variable. Or not?
Post #1383890
Posted Monday, November 12, 2012 3:27 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 1:00 PM
Points: 1,026, Visits: 3,073
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.
Post #1383894
Posted Monday, November 12, 2012 3:28 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 5:16 AM
Points: 946, Visits: 1,774
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

Jeremy Oursler
Post #1383895
Posted Monday, November 12, 2012 3:30 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 5:16 AM
Points: 946, Visits: 1,774
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

Jeremy Oursler
Post #1383896
Posted Monday, November 12, 2012 3:44 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 1:00 PM
Points: 1,026, Visits: 3,073
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 properly
Post #1383902
Posted Tuesday, November 13, 2012 2:21 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 12:50 PM
Points: 42, Visits: 130
Sorry!!
A bit of a beginners fault .... the field "kstplcode" in the @columns variable was not the same as the field "oms25_0" in the data-query!

Thanks for your help, anyway!
Post #1384301
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse