JaedenRuiner
Well-known member
- Joined
- Aug 13, 2007
- Messages
- 340
- Programming Experience
- 10+
I'm trying to think how I would have done it elsewhere, but I can't seem to resolve a little distinct issue in my T-SQL.
Given the nature of the DISTINCT keyword, when selecting rows, it acts somewhat like a "unique" key for all columns specified in the Selection. Now I know someone is going to come up and say "Distinct is not a key" so please ignore them and pay attention. To Explain what I mean: Each row in a Select Distinct must be completely unique from all other rows. Therefore, in a 3 column table:
So in truth it DOES behave in a similar manner to a Unique key constraint around ALL columns specified just for the immediate purpose of selection.
Now that that is out of the way...how do you count it?
I can do:
and that works just fine. but for the example above that would return a value of 1 and my count would be off.
returns an error about the "," being present.
Thus, I come to this: How can I get the Distinct Count for a DISTINCT subset of columns out of a table. (I ask, because this table has 16 columns, and I only need 6, but those 6 together must be distinct or else it throws my insert out of whack. I noticed the same issue with Oracle, that even though I tell it to Insert Into one table from another, it can often insert duplicates of the same row for no reason. I need to guarantee that the rows inserted from this table are Distinct, and I'd REALLY like to have the count of those rows before hand.)
Thanks
Given the nature of the DISTINCT keyword, when selecting rows, it acts somewhat like a "unique" key for all columns specified in the Selection. Now I know someone is going to come up and say "Distinct is not a key" so please ignore them and pay attention. To Explain what I mean: Each row in a Select Distinct must be completely unique from all other rows. Therefore, in a 3 column table:
VB.NET:
Col1 Col2 Col3
1 2 3
1 2 2
1 2 1
1 2 3 <- not distinct
Now that that is out of the way...how do you count it?
I can do:
VB.NET:
Select Count(Distinct col1)
VB.NET:
Select Count(distinct col1, col2, col3)
Thus, I come to this: How can I get the Distinct Count for a DISTINCT subset of columns out of a table. (I ask, because this table has 16 columns, and I only need 6, but those 6 together must be distinct or else it throws my insert out of whack. I noticed the same issue with Oracle, that even though I tell it to Insert Into one table from another, it can often insert duplicates of the same row for no reason. I need to guarantee that the rows inserted from this table are Distinct, and I'd REALLY like to have the count of those rows before hand.)
Thanks