SQL Sum and Counter

I have the following dataset.

[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 him to calculate the amount of the vote. If we look at 2012-07-24, there are 2 users who recorded the vote [1], since I can make these two users add up. Therefore he would look 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 UserID as well as VoteID.

Can anyone help?

+4
source share
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 does not matter, because you just want to know how many people voted in a certain way on a certain date; You do not need specific user data in this unit.

+7
source

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

 SELECT [Date Recorded], VoteID, COUNT(*) FROM myTable GROUP BY [Date Recorded], VoteID 
+7
source

Source: https://habr.com/ru/post/1432068/


All Articles