Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group and Count in SQL Server 2008

OK I might be asking a stupid question, but I'm banging my head over this..

Say I have a table like so:

FullName | DownloadDate
-------- | -----------------------
Jack     | 2012-03-21 00:00:00.000
Joe      | 2012-03-21 00:00:00.000
John     | 2012-03-22 00:00:00.000

I want to return the number of downloads by date so the resulting table is:

DownloadDate            | TotalDownloaded
------------------------| ---------------
2012-03-21 00:00:00.000 | 2
2012-03-22 00:00:00.000 | 1

How can I achieve this?

Also, you can assume that in my date column in the original data, I will always have a time of '00:00:00.000'.

like image 659
AshesToAshes Avatar asked Mar 22 '12 18:03

AshesToAshes


People also ask

Can we use GROUP BY and count together in SQL?

The SQL GROUP BY Statement The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

Can we use count and GROUP BY together?

The use of COUNT() function in conjunction with GROUP BY is useful for characterizing our data under various groupings. A combination of same values (on a column) will be treated as an individual group.

Does count work without GROUP BY?

Using COUNT, without GROUP BY clause will return a total count of a number of rows present in the table. Adding GROUP BY, we can COUNT total occurrences for each unique value present in the column.

Why do we use count (*) in SQL?

COUNT(*) returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately. This includes rows that contain null values.


1 Answers

try this:

SELECT DownloadDate, Count(DownloadDate) as TotalDownloaded
FROM yourtable
GROUP BY DownloadDate
like image 155
Taryn Avatar answered Oct 15 '22 10:10

Taryn