Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mar311
Starting Member
9 Posts |
Posted - 2011-03-22 : 13:17:46
|
Please look at the query I am trying to run. Can anyone tell me what is wrong with my syntax?My ultimate goal is to sum the columns yst, MTD, YTD, ystphr, MTDphr, YTDphr and then reflect the total sum of each column in a new column (ystGRANDTOTAL, MTDGRANDTOTAL, etc) as seen being attempted below?Any help would be greatly appreciated!declare @effdate datetime, @locid int, @showAll bit, @fystart datetime, @locName varchar(50)set @effdate = getdate()set @locid = 20set @showAll = 1WITH gTotal(location,name,yst,MTD,YTD,ystphr,MTDphr,YTDphr) AS (select @locname=facilityname from gwgeneral..locations where locid=@locidselect location=isnull(@locname,'All Locations')selectlocation,name,yst=ystitemcount,MTD=mtditemcount,YTD=ytditemcount,ystphr=cast(case when ystminutes > 0 then round(ystitemcount/(ystminutes/60.0),0) else 0 end as int),MTDphr=cast(case when MTDminutes > 0 then round(MTDitemcount/(MTDminutes/60.0),0) else 0 end as int),YTDphr=cast(case when YTDminutes > 0 then round(YTDitemcount/(YTDminutes/60.0),0) else 0 end as int)from (selectlocation=l.fac,name=isnull(rtrim(e.fname)+' '+left(e.lname,1),'Unknown'),s.locid,staffid=cast(p.staffid as int),p.stationid,ystitemcount=sum(case dt when dateadd(d,-1,@effdate) then itemcount else 0 end),mtditemcount=sum(case when month(dt)=month(@effdate) and year(dt)=year(@effdate) then itemcount else 0 end),ytditemcount=sum(case when dt between @fystart and @effdate then itemcount else 0 end),ystminutes=sum(case dt when dateadd(d,-1,@effdate) then datediff(n,cast(stime as datetime),cast(etime as datetime)) else 0 end),mtdminutes=sum(case when month(dt)=month(@effdate) and year(dt)=year(@effdate) then datediff(n,cast(stime as datetime),cast(etime as datetime)) else 0 end),ytdminutes=sum(case when dt between @fystart and @effdate then datediff(n,cast(stime as datetime),cast(etime as datetime)) else 0 end)from ptotals pjoin stations s on s.stationid=p.stationidjoin gen..locations l on l.locid=s.locidleft join gen..vwAllees e on ltrim(e.empno)=p.staffidwhere (@locid = 0 or s.locid=@locid)group by l.fac,s.locid,e.fname,e.lname,cast(p.staffid as int),p.stationid) xwhere ystitemcount > 0 or ytditemcount > 0and (@showAll = 1 or ystitemcount > 0))SELECT *, ystGRANDTOTAL= sum(yst) OVER (PARTITION BY location), MTDGRANDTOTAL= sum(MTD) OVER (PARTITION BY location), YTDGRANDTOTAL= sum(YTD) OVER (PARTITION BY location) FROM gTotal; |
|
X002548
Not Just a Number
15586 Posts |
|
mar311
Starting Member
9 Posts |
Posted - 2011-03-24 : 12:50:36
|
My bad...the err msg I'm getting is:"Incorrect syntax near 'gTotal'." on the line containing the WITH statement.Vague error I know...so something is wrong with my syntax. Can anyone tell me where my syntax is incorrect?Thanks for your help! |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-03-25 : 16:25:46
|
try;WITH gTotal No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-03-25 : 16:46:02
|
The semicolon tells the parser that the WITH isn't a part of set @showAll = 1in the line above. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
mar311
Starting Member
9 Posts |
Posted - 2011-03-30 : 14:54:18
|
WebFred - you da man! You answered in 2 minutes what quite a few others could not on other forums. Thank you very much!!I'm trying to contain the SUM values to just 1 row as opposed to having the SUM show for each row of the data set.EX: Right now the ystGRANDTOTAL Sum is appended to every row returned but I just want it returned as 1 row so I do not have repeating data.ystGRANDTOTAL-------------550550550550550 etc...Looking to just have:ystGRANDTOTAL-------------550Still reseaching. Thanks again for your help!! Much appreciated! |
 |
|
mar311
Starting Member
9 Posts |
Posted - 2011-04-05 : 16:02:59
|
Ok - I have not come up with an efficient way to show the "GRANDTOTAL" sums as just one row as opposed to appending it to each and every row of the data set. Can anyone advise on best way to do this? Thanks.SELECT *,ystGRANDTOTAL= sum(yst) OVER (PARTITION BY location),MTDGRANDTOTAL= sum(MTD) OVER (PARTITION BY location),YTDGRANDTOTAL= sum(YTD) OVER (PARTITION BY location)FROM gTotal; |
 |
|
mar311
Starting Member
9 Posts |
Posted - 2011-04-08 : 09:07:52
|
Am I approaching this the right way to display the value of each GRANDTOTAL in 1 row? The SUM value does not need to be repeated and appended to each row in the datatset and I'd like to have it limited to the value being appended to just 1 row. Any thoughts? |
 |
|
|
|
|
|
|