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 |
|
ka365
Starting Member
3 Posts |
Posted - 2012-02-13 : 17:55:29
|
| I've read all of the "sum a count" posts and haven't found a clear answer to my question.I am making a (distinct) count of column of ID values called EO_ID, and then i need to sum the resulting count.My code:SELECT TRACTNAME, CNAME, COUNT(DISTINCT EO_ID) AS AMTFROM AU_X_NDDBGROUP BY TRACTNAME, CNAMEE.g. of my results:TRACTNAME, CNAME, AMTSmith, Hall's tarplant, 1Smith, Hall's tarplant, 1Smith, Hall's tarplant, 1Smith, Piute cypress, 2Smith, praire falcon, 2There are 3 Hall's tarplants but there are several duplicates, so first I get the DISTINCT COUNT of their EO_ID, but then I need to sum that count.Any thoughts on how I can get a single row in my output for Hall's tarplant with a total amount of 3? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-13 : 18:10:35
|
| [code]SELECT TRACTNAME, CNAME,SUM(AMT) AS SUMAMTFROM(SELECT TRACTNAME, CNAME, COUNT(DISTINCT EO_ID) AS AMTFROM AU_X_NDDBGROUP BY TRACTNAME, CNAME)tGROUP BY TRACTNAME, CNAME[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
ka365
Starting Member
3 Posts |
Posted - 2012-02-13 : 19:04:46
|
| Thank you!I saw that in other posts but when I tried it I neglected to put the little "t" at the end - I appreciate your help.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/[/quote] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-13 : 19:13:47
|
| you're welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2012-02-14 : 02:45:28
|
quote: Originally posted by ka365 Thank you!I saw that in other posts but when I tried it I neglected to put the little "t" at the end - I appreciate your help.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
[/quote]That t is a alias for a derived tableMadhivananFailing to plan is Planning to fail |
 |
|
|
ka365
Starting Member
3 Posts |
Posted - 2012-02-14 : 12:51:44
|
| thanks madhivanan. after visakh16's post, i tried searching for more information on the little t, but didn't know what it was called - derived table! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-14 : 13:46:39
|
quote: Originally posted by ka365 thanks madhivanan. after visakh16's post, i tried searching for more information on the little t, but didn't know what it was called - derived table!
yep..As you're deriving a table out of inner query and for parser to recognise it you need to give it a name which is alias t------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|