Wednesday, January 6, 2010

T-SQL Discoveries #1

Given: A database table with the following structure


Problem: How many of each subject were posted on a certain date?

Solution: Issue the following query

SELECT count(queueID) AS TotalCount, dateposted, subject FROM (SELECT queueID, CONVERT(DATETIME,postdate,111) AS dateposted, subject FROM tblQueue) AS tblQ GROUP BY dateposted, subject

NOTE: The above query disregards the posting time, i.e., it focuses on the date alone.


No comments:

Post a Comment