| Author |
Topic |
|
NeilC
Yak Posting Veteran
55 Posts |
Posted - 2007-12-06 : 08:54:19
|
| I have the following query:select L1.LookupTitle as ReferralType, sum(ReferralTotal) as ReferralTotalfrom Client_Survey_Totals Tinner join Lookup L1 on T.ReferralLookupID = L1.lookupIDinner join Client_Survey S on T.ClientSurveyID = S.ClientSurveyIDwhere DateRecieved Between '1/1/2006' and '1/12/2007'--@start and @endGroup By L1.LookupTitleWhich produces:ReferralType ReferralTotal-------------------------------------Audiologist 9Friend / Family 301Presentation 2Website 2Hearing Aid Specialist 7example 89Newspaper 2Other 11Physician 2Television 2Test 2My question is, how can I sum them up? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-06 : 09:02:59
|
| Do you want total ReferralTotal?Select sum(ReferralTotal) as ReferralTotal from(your above query) as tMadhivananFailing to plan is Planning to fail |
 |
|
|
jordanam
Yak Posting Veteran
62 Posts |
Posted - 2007-12-06 : 11:14:51
|
| Hmm... if you saw my posts, sorry. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-06 : 11:20:42
|
[code]declare @t table (i int, j int)insert @tselect 1, 1 union allselect 2, 1 union allselect 1, 2select distinct i, sum(j) over (partition by i) as groupsum, sum(j) over () as totalfrom @t[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-07 : 00:16:01
|
| You can use WITH CUBE clause to get total sum as a record select L1.LookupTitle as ReferralType, sum(ReferralTotal) as ReferralTotalfrom Client_Survey_Totals Tinner join Lookup L1 on T.ReferralLookupID = L1.lookupIDinner join Client_Survey S on T.ClientSurveyID = S.ClientSurveyIDwhere DateRecieved Between '1/1/2006' and '1/12/2007'--@start and @endGroup By L1.LookupTitleWITH CUBE |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2007-12-07 : 01:09:51
|
quote: Originally posted by Peso
declare @t table (i int, j int)insert @tselect 1, 1 union allselect 2, 1 union allselect 1, 2select distinct i, sum(j) over (partition by i) as groupsum, sum(j) over () as totalfrom @t E 12°55'05.25"N 56°04'39.16"
For the folks who cannot understand the new SQL 2005 syntax this is the equivalent of Peso solution with the SQL 2000 syntax if I am not wrong somewhere declare @t table (i int, j int)insert @tselect 1, 1 union allselect 2, 1 union allselect 1, 2select i,sum(j)as groups,total=(select sum(j) from @t)from @t group by iRegards |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-07 : 01:10:48
|
quote: Originally posted by jordanam Hmm... if you saw my posts, sorry.
Subtotal and Grandtotals are something that should be done in your Reports. If you want to do them in sql, then you would have both detailed and summary data togetherYes. I saw your post and forgot to ask "where do you want to show your data?" MadhivananFailing to plan is Planning to fail |
 |
|
|
jordanam
Yak Posting Veteran
62 Posts |
Posted - 2007-12-07 : 13:05:09
|
| I read the initial post incorrectly, and then proceeded to "correct" your first post, which was so elegantly correct.This thread has become the perfect example of classic "Hello world" being a little overdeveloped :) That's not to say there is nothing to learn. As a matter of fact, I think I am going to grab Peso's code and play with it. |
 |
|
|
|