Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Sum and Count

I have the following data set.

[Date Recorded]             [VoteID]  [User]
[2012-07-20 00:00:00.000]   [2]       [A]
[2012-07-20 00:00:00.000]   [3]       [B]
[2012-07-20 00:00:00.000]   [4]       [C]
[2012-07-24 00:00:00.000]   [1]       [A]
[2012-07-24 00:00:00.000]   [1]       [B]

How can I get it to count the vote sum. If we look at 2012-07-24 there are 2 users who recorded a vote of [1] so how can I get these two users to add up. So it would look something like

[Date Recorded]             [VoteID]  [Count]
[2012-07-20 00:00:00.000]   [2]       [1]
[2012-07-20 00:00:00.000]   [3]       [1]
[2012-07-20 00:00:00.000]   [4]       [1]
[2012-07-24 00:00:00.000]   [1]       [2]

I have a Guid for the UserID as well as the VoteID.

Can someone help?

like image 372
TheMonkeyMan Avatar asked Dec 08 '22 21:12

TheMonkeyMan


2 Answers

Group on [Date Recorded] and VoteID:

SELECT [Date Recorded], VoteID, COUNT(*) [Count]
FROM YourTable
GROUP BY [Date Recorded], VoteID

The GUID for your user who voted is irrelevant since you just want to know how many people voted a certain way on a given date; you don't need specific user details in this aggregate.

like image 128
Yuck Avatar answered Dec 24 '22 13:12

Yuck


Use GROUP BY to group by both the date and id column, then COUNT up the results in each group:

SELECT [Date Recorded], VoteID, COUNT(*)
FROM myTable
GROUP BY [Date Recorded], VoteID
like image 34
lc. Avatar answered Dec 24 '22 14:12

lc.