| Author |
Topic |
|
Hommer
Aged Yak Warrior
808 Posts |
Posted - 2004-10-12 : 13:13:03
|
| I have a select sql to get me the duplicated rows as follow:select tbilled, tracking, count(*) from sept904invoice group by tracking, tbilled having count(*)>1Now I want to get a sum on the tbilled, but could not. I have tried Select INTO, Select sum(tbilled) where tracking in (select...) or Where tracking exist...The reqirement is to get a sum on a billed charge where tracking number is duplicated.Thanks!Hommer |
|
|
surefooted
Posting Yak Master
188 Posts |
Posted - 2004-10-12 : 13:20:36
|
| [code]select sum(tbilled), tracking, count(*) from sept904invoice group by tracking having count(*)>1[/code]??-JonNow a "Yak Posting Veteran". |
 |
|
|
Hommer
Aged Yak Warrior
808 Posts |
Posted - 2004-10-12 : 13:38:42
|
| They want to sum all records with duplicated tracking number. The sum is not BY tracking. That is why I tried to use sub select to get the duplicates first then try to sum. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-10-12 : 13:51:39
|
[doooh]SELECT SUM(tbilled) FROM ( SELECT tbilled, tracking, count(*) FROM sept904invoice GROUP BY tracking, tbilled HAVING count(*)>1) AS XXX [/doooh]Brett8-) |
 |
|
|
Hommer
Aged Yak Warrior
808 Posts |
Posted - 2004-10-12 : 14:01:53
|
| I have tried this at very begining:SELECT SUM(convert(money, tbilled)) FROM ( SELECT tbilled, tracking, count(*) FROM sept904invoice GROUP BY tracking, tbilled HAVING count(*)>1)I need to throw in the convert because the field is of varchar type.And I got: Line 5: Incorrect syntax near ')'.That is the best I could get. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-10-12 : 14:31:43
|
| Actually I think count needs to be label in the derived table..Brett8-) |
 |
|
|
Hommer
Aged Yak Warrior
808 Posts |
Posted - 2004-10-12 : 14:32:09
|
| I got it. two steps.SELECT tbilled, tracking, count(*) as countNo into MyTemp FROM sept904invoice GROUP BY tracking, tbilled HAVING count(*)>1select sum(convert(money, tbilled)) from mytempWithout "as countNo", the first part got tossed complaining No column was specified...Thanks! |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-10-12 : 14:42:18
|
| [code]USE NorthwindGOSET NOCOUNT ONCREATE TABLE sept904invoice99(tbilled varchar(15), tracking varchar(15))GOINSERT INTO sept904invoice99(tbilled, tracking)SELECT 1, 1 UNION ALLSELECT 2, 1 UNION ALLSELECT 3, 1 UNION ALLSELECT 4, 1 UNION ALLSELECT 4, 1 UNION ALLSELECT 5, 1 UNION ALLSELECT 5, 1 UNION ALLSELECT 5, 1 UNION ALLSELECT 6, 1 UNION ALLSELECT 7, 2 UNION ALLSELECT 7, 2GOGOSELECT CONVERT(money,tbilled) AS tbilled, tracking, count(*) AS DupCounts FROM sept904invoice99 GROUP BY tracking, tbilled HAVING count(*)>1SELECT SUM(tbilled) SUM_tbilled FROM ( SELECT CONVERT(money,tbilled) AS tbilled, tracking, count(*) AS DupCounts FROM sept904invoice99 GROUP BY tracking, tbilled HAVING count(*)>1) AS XXX GOSET NOCOUNT OFFDROP TABLE sept904invoice99GO[/code]See what I get for not testing...doesn't make much sense though...Brett8-) |
 |
|
|
Hommer
Aged Yak Warrior
808 Posts |
Posted - 2004-10-12 : 16:47:54
|
| That is all these XXX for. Without "as xxx", it complains about incorrect syntax near the ), poor scapegoat. |
 |
|
|
|