Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query for Figuring counts by month

Tags:

sql

sql-server

whats a good way to join 1-12 in a column to a bunch of counts by month?... in SQL

SELECT
    months???,
    count(whatever1) count1,
    count(whatever2) count2
FROM
    months????
    LEFT JOIN whatever1 ON
        month(whatever1.Date) = months???.monthid
    LEFT JOIN whatever2 ON
        month(whatever2.Date) = months???.monthid
GROUP BY
    months???

something that would end up like

"month","whatever1count","whatever2count"
1,null,5
2,null,3
3,null,null
4,2,3
5,36,73
6,2,null
7,45,944
8,null,12
9,1467,3
10,null,2
11,3,25
12,4,null

edit- basically where is a slick way to get my months list/table/whatever

like image 218
spaghetticowboy Avatar asked May 24 '11 19:05

spaghetticowboy


People also ask

How can I get month wise data count in SQL?

Calculate Monthly Sales Report in MySQL If you only want a total count of sales every month, then you can use COUNT function instead. mysql> select year(order_date),month(order_date),sum(sale) from sales WHERE condition group by year(order_date),month(order_date) order by year(order_date),month(order_date);

How do I select month data in SQL?

To get a month from a date field in SQL Server, use the MONTH() function. This function takes only one argument – the date. This can be a date or date and time data type.

How do I count counts in SQL query?

In SQL, you can make a database query and use the COUNT function to get the number of rows for a particular group in the table. Here is the basic syntax: SELECT COUNT(column_name) FROM table_name; COUNT(column_name) will not include NULL values as part of the count.

How do I get month order in SQL?

To order by month, create a date with this month. To do this, use the STR_TO_DATE() function. If you have a date stored as a string in the ' Year Month Day ' format, you can cast it to a date using STR_TO_DATE(date_string, '%Y %M %d') . The CONCAT() function combines all the arguments into one string.


2 Answers

Many ways... one that worked well for me across many applications at a previous job was to build a table of timeframes.

id - Year - Month   - StartStamp            - End Stamp  
1  - 2008 - January - 1/1/2008 00:00:00.000 - 1/31/2008 23:59:59.999

Then you can just join against the timeframes table where your date field is between startstamp and endstamp.

This makes it easy to pull a certain time period, or all time periods...

Yours could be simpler, with just 12 records (i.e. 1 - January) and joining on DATEPART(m,DateColumn)

select mon.monthNumber, mon.monthName, 
       count(a.*) as count1, count(b.*) as count2
from months mon
left join whatever1 a on DATEPART(m,a.Date) = mon.monthNumber
left join whatever2 b on DATEPART(m,b.Date) = mon.monthNumber
group by mon.monthNumber, mon.monthName

You could also do the month thing ad-hoc, like so:

select mon.monthNumber, mon.monthName, 
       count(a.*) as count1, count(b.*) as count2
from (
   select 1 as monthNumber, 'January' as monthName
   union
   select 2 as monthNumber, 'February' as monthName
   union
   select 3 as monthNumber, 'March' as monthName
   union
   ......etc.....
) mon
left join whatever1 a on DATEPART(m,a.Date) = mon.monthNumber
left join whatever2 b on DATEPART(m,b.Date) = mon.monthNumber
group by mon.monthNumber, mon.monthName
like image 126
Fosco Avatar answered Nov 04 '22 12:11

Fosco


basically where is a slick way to get my months list/table/whatever

You can use a recursive cte to build a list of months.

;with Months(MonthNum) as
(
  select 1 MonthNum
  union all
  select MonthNum+1
  from Months
  where MonthNum < 12 
)

-- Your query goes here
select * 
from Months
like image 4
Mikael Eriksson Avatar answered Nov 04 '22 12:11

Mikael Eriksson