How do I group a date field to get quarterly results in MySQL?

前端 未结 4 1585
孤独总比滥情好
孤独总比滥情好 2020-12-18 19:47

I have a job table that holds jobs and leaddate is the field for the job entry.

The result I want to get is the number of jobs I have in each quarter. My query count

4条回答
  •  太阳男子
    2020-12-18 20:09

    I usually combine quarter and year into a single YEAR-QUARTER field:

    select jobid, count(jobid) as jobcount, 
    CONCAT( YEAR(leaddate),'-',QUARTER(leaddate) ) as year_qtr
    from jobs
    where contactid='19249'
    group by year_qtr
    

    RETURNS data like:

    19249, 324, 2011-3
    19249,   4, 2011-2
    19249,  34, 2011-1
    

提交回复
热议问题