January 18, 2008 at 3:18 pm
I tried to execute the following SQL statement but failed:
SELECT COUNT(*), * FROM TableA WHERE Type = X
I can do the following and it is OK
SELECT COUNT(*), Col1, Col2, Col3 ... Col20 FROM TableA WHERE Type = X GROUP BY Col1, Col2, Col3 ... Col20
There are 20 columns in TableA, and it would very messy to use the GROUP BY clause for each column.
Would appreciate suggestion from you. Thanks in advance.
sg2000
January 18, 2008 at 3:49 pm
I would be helpful if you explained what you are trying to do.
January 18, 2008 at 5:13 pm
If you have 20 columns in table A, and you try to list all 20 columns in the presence of a COUNT, then you must list all 20 columns in the SELECT and the GROUP BY. If there happens to be either a Primary Key or a unique index anywhere in the table, the count will always be "1" because you're listing/grouping by all 20 columns.
Let me rephrase what Michael said in his post (above) just a wee bit... what is it that you really want to do?
--Jeff Moden
Change is inevitable... Change for the better is not.
January 18, 2008 at 6:16 pm
Thanks for the quick response. This is what I was trying to do:
I have a table (TableA) that contains 20 columns (Col1, Col2 through Col20). I want to retrieve 5 of (Col1, Col5, Col7, Col10 and Col 20) of the 20 columns and also want to know how many of these 5 columns contain Null. Since I know the primary key of the row, I will retrieve only one row. I use the following SQL statement:
SELECT NullCount= COUNT(Col1) + COUNT(Col5) + COUNT(Col7) + COUNT(Col10) + COUNT(20), Col1, Col5, Col7, Col10, Col20 FROM TableA WHERE ID = x GROUP BY Col1, Col5, Col7, Col10, Col20
This works OK. However, if I want to count and retrieve 18 columns, the SQL statement would become very long and messy.
My question is: is there any way I can achieve this goal without the GROUP By phrase?
Thanks in advance for any suggestions.
sg2000
January 18, 2008 at 6:33 pm
My question is: is there any way I can achieve this goal without the GROUP By phrase?
I could be wrong, but I don't think there is...
--Jeff Moden
Change is inevitable... Change for the better is not.
January 18, 2008 at 7:02 pm
Actually - I'm not sure what you plan on getting with that. The numbers are going to be all over the place aren't they?
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply