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 |
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2011-05-02 : 08:42:31
|
Hi, i have 2 count in my query RecordsClose and RecordAllI need the sum of RecordsClose and RecordAll is there an easy way of doing this?Ex: RecordsClose RecordAll1 12 52 5So i need sumRecordsClose and sumRecordAll5 11SELECT tt.datDateTempete, te.strNom, reg.strNom,count(*) OVER(partition by reg.strNom) as RecordsClose, (SELECT DISTINCT count(*) OVER(partition by reg2.strNom) as Records FROM tblEcole as te2 INNER JOIN tblRegion as reg2 ON reg2.intRegionId = te2.intRegionId WHERE reg2.intRegionId = reg.intRegionId) as RecordAllFROM tblTempete as ttINNER JOIN tblRegionEcole as re ON tt.intTempeteId = re.intTempeteIdINNER JOIN tblEcole as te ON re.intEcoleId = te.intEcoleIdINNER JOIN tblRegion as reg ON reg.intRegionId = te.intRegionIdINNER JOIN tblRaisonTempete as rt ON rt.intTempeteId = tt.intTempeteIdINNER JOIN tblRaison as tra ON tra.intRaisonId = rt.intRaisonIdWHERE tt.datDateTempete = '2011-05-2'ORDER BY reg.strNom,te.strNom Tks |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-05-02 : 08:48:39
|
trySELECT tt.datDateTempete, te.strNom, reg.strNom, count(*) OVER(partition by reg.strNom) as RecordsClose, RecordAll, sum(count(*)) OVER(partition by reg.strNom) as sumRecordsClose, sum(RecordAll) OVER()FROM tblTempete as ttINNER JOIN tblRegionEcole as re ON tt.intTempeteId = re.intTempeteIdINNER JOIN tblEcole as te ON re.intEcoleId = te.intEcoleIdINNER JOIN tblRegion as reg ON reg.intRegionId = te.intRegionIdINNER JOIN tblRaisonTempete as rt ON rt.intTempeteId = tt.intTempeteIdINNER JOIN tblRaison as tra ON tra.intRaisonId = rt.intRaisonIdCROSS APPLY ( SELECT DISTINCT count(*) OVER(partition by reg2.strNom) as RecordAll FROM tblEcole as te2 INNER JOIN tblRegion as reg2 ON reg2.intRegionId = te2.intRegionId WHERE reg2.intRegionId = reg.intRegionId) rWHERE tt.datDateTempete = '2011-05-02'ORDER BY reg.strNom,te.strNom KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2011-05-02 : 08:56:12
|
It seem to work i put group by reg.strNom,te.strNom,tt.datDateTempete,RecordAllTks for your helptks for the replys it giving me this error Column 'tblTempete.datDateTempete' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clausequote: Originally posted by khtan trySELECT tt.datDateTempete, te.strNom, reg.strNom, count(*) OVER(partition by reg.strNom) as RecordsClose, RecordAll, sum(count(*)) OVER(partition by reg.strNom) as sumRecordsClose, sum(RecordAll) OVER()FROM tblTempete as ttINNER JOIN tblRegionEcole as re ON tt.intTempeteId = re.intTempeteIdINNER JOIN tblEcole as te ON re.intEcoleId = te.intEcoleIdINNER JOIN tblRegion as reg ON reg.intRegionId = te.intRegionIdINNER JOIN tblRaisonTempete as rt ON rt.intTempeteId = tt.intTempeteIdINNER JOIN tblRaison as tra ON tra.intRaisonId = rt.intRaisonIdCROSS APPLY ( SELECT DISTINCT count(*) OVER(partition by reg2.strNom) as RecordAll FROM tblEcole as te2 INNER JOIN tblRegion as reg2 ON reg2.intRegionId = te2.intRegionId WHERE reg2.intRegionId = reg.intRegionId) rWHERE tt.datDateTempete = '2011-05-02'ORDER BY reg.strNom,te.strNom KH[spoiler]Time is always against us[/spoiler]
|
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2011-05-02 : 09:12:09
|
| tks for your help it working perfect!Many tks |
 |
|
|
|
|
|
|
|