Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Jeff writes "Is it possible to use one SQL statement to get the following?I have a table which has columns Category, Item, and DatePosted.I need to select distinct category, and the total number of items for each category, as well as the total number of items in each category which were posted within the last 5 days. The page will be displayed like this:Automotive (10) items (3) new Clothing (8) items (2) newetc..Windows,SQL Server 2000 sp4Thanks!"
RickD
Slow But Sure Yak Herding Master
3608 Posts
Posted - 2004-06-03 : 08:28:24
Something like this??
select category, sum(items) items, sum(case when dateposted between dateadd(dd,-5,dateadd(dd,datediff(dd,0,getdate()),0)) and dateadd(dd,datediff(dd,0,getdate()),0) then items else 0 end) newfrom tableAgroup by category