Really simple SQL count() question

Status
Not open for further replies.

kingshaka_1977

Wise, Aged Ars Veteran
102
Hi guys,

i'm feeling brain dead today and cant seem to work this out. i ashamed as it seems to be such a basic problem.

I have an sql query: select "distinct item1, item2..." and want to find the number of rows this returns using the count() function, but i cant seem to get the syntax correct.

this is for MSSQL. i have the answer just by looking at the number of rows returned in QA, but want to know what it should be in t-sql. i thought it wouldve been something like: "select count(distinct item1,item2)..." but this just gives me an error.

thanks
Sab
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
Richard Berg":3cglwtkt said:
The other solution is Group By. If perf matters, profile both. Optimizers are weird.
Sorry for the brevity. I wrote this on my phone while walking to work. Here's what I had in mind:

Code:
SELECT COUNT(COUNT(*)) 
    FROM Table
    GROUP BY field1, field2, ...

Taking them for a spin on Sybase 12.5, on a large table where all of the fields are covered by an index, both formulations result in the same basic query plan and take about the same amount of time. The subquery does less I/O: for some reason the Group By query doesn't apply a uniqueness constraint until after the worktable is built, while the subquery does so during the index scan. That's life with SQL optimizers (10-year-old optimizers, at that) for ya.
 
Status
Not open for further replies.