Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MS SQL: Group by day and count

Posted on 2007-10-16
3
Medium Priority
?
5,360 Views
Last Modified: 2008-05-27
Hi

I have the following query:
select   date_added,
      (select count(*) from redeem R where whisky_id = 1 and R.date_added = redeem.date_added) as [Item1],
      (select count(*) from redeem R where whisky_id = 2 and R.date_added = redeem.date_added) as [Item2],
      (select count(*) from redeem R where whisky_id = 6 and R.date_added = redeem.date_added) as [Item3]
from redeem
group by date_added

the data out is something like:

date_added          | Item1 | Item2 | Item3

2007-01-22 12:13.05 | 1     | 4     | 0
2007-01-22 12:15.05 | 0     | 9     | 4
2007-01-22 17:12.05 | 1     | 4     | 5
2007-01-26 00:14.05 | 0     | 1     | 4
2007-01-28 10:15.05 | 0     | 1     | 4

Now what I wanted to do was group the results by day. So the first 3 records would be combined because they are on the same day. How would I go about doing this?
Any help would be greatly appreciated.

Thanks
0
Comment
Question by:bLUE
3 Comments
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20086802
use left(convert(varchar(fieldname),111),2)
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 320 total points
ID: 20087921
SELECT CONVERT(VARCHAR(10), date_added, 120) AS [DateAdded],
    SUM(CASE WHEN whiskey_id = 1 THEN 1 ELSE 0 END) AS [Item1],
    SUM(CASE WHEN whiskey_id = 2 THEN 1 ELSE 0 END) AS [Item2],
    SUM(CASE WHEN whiskey_id = 6 THEN 1 ELSE 0 END) AS [Item3]
FROM redeem
WHERE whiskey_id IN (1, 2, 6)
GROUP BY CONVERT(VARCHAR(10), date_added, 120)
0
 
LVL 1

Author Comment

by:bLUE
ID: 20091644
Perfect
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question