| Author |
Topic |
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-05-02 : 14:49:57
|
real simple question. how would i total this query?select source,dof,count(dof) from xentel_chk group by source,dof order by source,dof comes out like: NM | 20070216 | 20NM | 20070223 | 17.... |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-05-02 : 15:14:21
|
| [code]Select Sum(Dof),Sum(CntDof)From ( select source,dof,count(dof) as CntDoffrom xentel_chk group by source,dof order by source,dof) as f [/code]Chiraghttp://chirikworld.blogspot.com/ |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-05-02 : 15:21:32
|
| after i fixed as CntDoffrom to CntDof From (type-o i assume)i got the following errorServer: Msg 1033, Level 15, State 1, Line 6The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-02 : 15:22:21
|
| You don't need an ORDER BY in the derived table.Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-02 : 15:22:59
|
| Select source, Sum(CntDof)From ( select source,dof,count(dof) as CntDoffrom xentel_chk group by source,dof) as f GROUP BY sourceorder by sourcePeter LarssonHelsingborg, Sweden |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-05-02 : 15:26:27
|
| perhaps i was mis-understood, i'm looking for a result more like this:NM | 20070216 | 20NM | 20070223 | 17total | null | 37i only need to total the CntDof fields |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-02 : 15:30:41
|
| select source, dof, count(dof) as CntDoffrom xentel_chk group by source,dof with rolluporder by source, dofPeter LarssonHelsingborg, Sweden |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-05-02 : 15:33:58
|
| i got the same thing i got from the original query. on the messages tab, i see this:(84 row(s) affected)Warning: Null value is eliminated by an aggregate or other SET operation.is that why? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-02 : 15:37:12
|
| Select 'total' as Source, null AS dof, Sum(CntDof) as cntdofFrom (select source, dof, count(dof) as CntDoffrom xentel_chkgroup by source, dof) as f Peter LarssonHelsingborg, Sweden |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-05-02 : 15:40:12
|
| Select * From ( select source,dof,count(dof) CntDof from xentel_chk group by source,dof)Order by CntDof Compute Count(CntDof) By CntDofChiraghttp://chirikworld.blogspot.com/ |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-05-02 : 15:43:44
|
| Peso, that just gives me the total, i'm looking for each count, with the total at the end.Chiragi get Server: Msg 156, Level 15, State 1, Line 4Incorrect syntax near the keyword 'Order'.when trying yours. never worked with "COMPUTE" so i'm not sure why.... |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2007-05-02 : 15:47:51
|
| Opps.. forgot to give alais to derived table.. Select * From(select source,dof,count(dof) CntDof from xentel_chkgroup by source,dof) as f Order by CntDofCompute Count(CntDof) By CntDofChiraghttp://chirikworld.blogspot.com/ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-02 : 15:50:02
|
| Please provide some sample data and expected output, so we don't have to guess anymore...Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-02 : 15:56:02
|
Here is a workable solution based on WITH ROLLUP as I suggested earlier.-- prepare sample datadeclare @sample table (source varchar(2), dof datetime)insert @sampleselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070216' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323' union allselect 'NM', '20070323'-- show the expected outputselect coalesce(source, 'total') AS source, dof, count(dof) as cntdoffrom @samplegroup by source, dofwith rolluphaving grouping(source) = grouping(dof) Peter LarssonHelsingborg, Sweden |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-05-02 : 15:58:31
|
| sorry for being so vauge before. this is what i have/what i'm looking for.current output: NH 20060505 9NH 20060512 3NH 20060519 3NH 20060526 1NH 20060602 5NM 20060505 101NM 20060512 149NM 20060519 106NM 20060526 81NM 20060602 17Xen Xen 17929what i'm looking for: NH 20060505 9NH 20060512 3NH 20060519 3NH 20060526 1NH 20060602 5NM 20060505 101NM 20060512 149NM 20060519 106NM 20060526 81NM 20060602 17Total 18404 |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-05-02 : 16:01:08
|
| Peso, that worked PERFECT. thank you very much! now i'll have to figure out a way to work this into my procedure, thank you VERY much! |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-05-02 : 16:08:25
|
| Peso, i'm fairly sure i understand most of this except:having grouping(source) = grouping(dof)could you give me an idea of the logic behind this? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-02 : 16:34:35
|
| Read Books Online about the grouping function.Peter LarssonHelsingborg, Sweden |
 |
|
|
albertkohl
Aged Yak Warrior
740 Posts |
Posted - 2007-05-02 : 16:38:18
|
| thank you sir. appreciate your help. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-02 : 16:41:57
|
| Otherwise, just do the running total at the front end application.Peter LarssonHelsingborg, Sweden |
 |
|
|
|