I’m working with a huge database table and need to count records efficiently. My basic counting query works fine when grouping by existing columns:
select p.category,
p.status,
count(p.recordid) as total_records
from datatable as p
group by p.category, p.status
However, my real challenge involves grouping by a computed field using conditional logic. Here’s my current approach:
SELECT final.category,
final.status,
Count(recordid) AS record_total
FROM (
SELECT p.category,
CASE
WHEN p.vendor = 'toyota'
AND Left(p.serialcode, 4) <> 'XXXX' THEN 'active'
WHEN Left(p.serialcode, 5) = 'YYYYY' THEN 'pending'
WHEN Left(p.serialcode, 3) = 'ZZZ' THEN 'complete'
ELSE 'inactive'
END AS status,
Left(p.recordid, 8) AS recordid
FROM datatable AS p
) AS final
GROUP BY final.category, final.status
This query creates a subquery that processes all records first, then groups them. The problem is performance - it takes over 4 hours to process a year’s worth of data because it pulls everything into memory before counting.
Is there a way to do server-side counting without the derived table? I want to avoid pulling all records and then grouping them. The current method is extremely slow with millions of records.