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»»

How to do SUM on Columns from subquery aggregate functions Expand / Collapse
Author
Message
Posted Sunday, March 13, 2011 10:16 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 16, 2011 2:05 PM
Points: 12, Visits: 34
Am wanting to know the most efficient way to show SUM of a column derived from a subquery aggregate function. If you look at the query I provide below, how can I sum the columns yst, MTD, YTD, ystphr, MTDphr, YTDphr and then reflect the total sum of each column?

Would really appreciate any example queries anyone can provide. Thanks for your time!



declare @effdate datetime, @locid int, @showAll bit, @fystart datetime, @locName varchar(50)

set @effdate = getdate()
set @locid = 20
set @showAll = 1

select @locname=facilityname from gwgeneral..locations where locid=@locid
select location=isnull(@locname,'All Locations')
select
location,
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 (
select
location=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 p
join stations s on s.stationid=p.stationid
join gen..locations l on l.locid=s.locid
left join gen..vwAllees e on ltrim(e.empno)=p.staffid
where (@locid = 0 or s.locid=@locid)
group by l.fac,s.locid,e.fname,e.lname,cast(p.staffid as int),p.stationid
) x
where ystitemcount > 0 or ytditemcount > 0
and (@showAll = 1 or ystitemcount > 0)
order by location,name
Post #1077540
Posted Sunday, March 13, 2011 11:04 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, May 16, 2013 9:52 AM
Points: 1,277, Visits: 1,608
If I understand you correctly, you need an aggregation, and then an aggregation of all aggregates (or more commonly, a summary row). You may want to try exploring the possibility of using the GROUPING() Aggregate function - http://msdn.microsoft.com/en-us/library/ms178544.aspx

Thanks & Regards,
Nakul Vachhrajani.
http://beyondrelational.com/modules/2/blogs/77/nakuls-blog.aspx
Be courteous. Drive responsibly.

Follow me on
Twitter: @nakulv_sql
Google Plus: +Nakul
Post #1077542
Posted Monday, March 14, 2011 12:00 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:39 PM
Points: 5,661, Visits: 6,102
I believe you're looking for the ROLLUP operator.

http://msdn.microsoft.com/en-us/library/ms189305(v=sql.90).aspx



- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1077550
Posted Monday, March 14, 2011 8:43 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 16, 2011 2:05 PM
Points: 12, Visits: 34
Thanks for the feedback. What I'm ultimately looking for is to create new columns for the "Grand Totals" and then pull the single value from that column via SP to reflect on the front end of the web app.

See my changes in bold below to see what I'm trying to achieve. If someone can give me an example query of how to do this, I would be extremely appreciative.


select @locname=facilityname from gwgeneral..locations where locid=@locid
select location=isnull(@locname,'All Locations')
select
location,
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),
ystGRANDTOTAL= sum(yst),
MTDGRANDTOTAL= sum(MTD),
YTDGRANDTOTAL= sum(YTD)
Post #1077751
Posted Monday, March 14, 2011 8:54 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:49 AM
Points: 6,367, Visits: 8,229
mar311 (3/14/2011)
Thanks for the feedback. What I'm ultimately looking for is to create new columns for the "Grand Totals" and then pull the single value from that column via SP to reflect on the front end of the web app.

See my changes in bold below to see what I'm trying to achieve. If someone can give me an example query of how to do this, I would be extremely appreciative.


select @locname=facilityname from gwgeneral..locations where locid=@locid
select location=isnull(@locname,'All Locations')
select
location,
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),
ystGRANDTOTAL= sum(yst),
MTDGRANDTOTAL= sum(MTD),
YTDGRANDTOTAL= sum(YTD)


Does this work?

WITH CTE AS (
select
location,
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)
)
SELECT *,
ystGRANDTOTAL= sum(yst) OVER (PARTITION BY location),
MTDGRANDTOTAL= sum(MTD) OVER (PARTITION BY location),
YTDGRANDTOTAL= sum(YTD) OVER (PARTITION BY location)
FROM CTE;



Wayne
Microsoft Certified Master: SQL Server 2008
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings
Post #1077759
Posted Monday, March 14, 2011 2:25 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 16, 2011 2:05 PM
Points: 12, Visits: 34
Unfortunately Wayne that did not work. If you look at the query in my original post, how could we structure the syntax in a way to get your suggestion working properly? Or anyone else that wants to chime in? I haven't figured out a way to do this successfully yet.

Thanks for your input!
Post #1077987
Posted Monday, March 14, 2011 2:59 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:39 PM
Points: 5,661, Visits: 6,102
mar311 (3/14/2011)
Unfortunately Wayne that did not work. If you look at the query in my original post, how could we structure the syntax in a way to get your suggestion working properly? Or anyone else that wants to chime in? I haven't figured out a way to do this successfully yet.

Thanks for your input!


Can you provide some sample data, ddl, and expected result from that sample data? It would help tremendously towards us understanding your final expected goal. You can check the first link in my sig if you need assistance with that.



- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1077998
Posted Wednesday, March 16, 2011 10:32 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 16, 2011 2:05 PM
Points: 12, Visits: 34
Absolulety..and I should have done this to begin with. Apologies.

Here's the result set I'm looking to achieve.

yst MTD YTD ystphr MTDphr YTDphr *ystGRANDTOTAL* *MTDGRANDTOTAL* *YTDGRANDTOTAL*
--- --- --- ------ ------ ------ --------------- --------------- ---------------
70 1200 6200 200 180 100 590 5800 58200
520 4600 52000 120 100 90
Post #1079103
Posted Wednesday, March 16, 2011 10:47 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 16, 2011 2:05 PM
Points: 12, Visits: 34
I attached an img for ease of reading.

  Post Attachments 
SampleData.GIF (18 views, 9.11 KB)
Post #1079111
Posted Monday, March 21, 2011 7:18 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 16, 2011 2:05 PM
Points: 12, Visits: 34
Happy Monday to everyone! Was hoping there was someone out there that can review this thread and provide a solution. Any help would be greatly appreciated!
Post #1081271
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse