sql – count records per weeks using timestamp

I have the table named ‘tasaciones’ which has 2 columns ‘record’ and ‘fecha’ (fecha is timestamp).

record  | fecha

record1 | 2015-10-09 11:24:52

record1 | 2015-11-09 11:31:52

record2 | 2015-17-09 11:37:17

record3 | 2015-25-09 12:03:31

record3 | 2015-26-09 12:03:31

record4 | 2015-10-10 12:23:25

record4 | 2015-11-10 12:27:25

I need to make weekly report by counting records per weeks from the date of first record until the current time.
I found something like this but even if this is what I need, I don’t know how to write ‘while’ to echo the data.

SELECT year(fecha), datepart(week, fecha), count(*)
FROM tasaciones
GROUP BY year(fecha), datepart(week, fecha)

I want to get results like this:

week 1: 15 records

week 2: 10 records

week 3: 25 records

week 4: 25 records


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s