group by month and year, count from another table

前端 未结 3 2149
北海茫月
北海茫月 2021-01-26 11:23

im trying to get my query to group rows by month and year from the assignments table, and count the number of rows that has a certain value from the leads

3条回答
  •  攒了一身酷
    2021-01-26 11:43

    You can sort of truncate your timestamps to months and use the obtained values for grouping, then derive the necessary date parts from them:

    SELECT
      YEAR(d_yearmonth) AS d_year,
      MONTHNAME(d_yearmonth) AS d_month,
      …
    FROM (
      SELECT
        LAST_DAY(FROM_UNIXTIME(a.date_assigned)) as d_yearmonth,
        …
      FROM assignments AS a
        LEFT JOIN leads AS l ON (l.id = a.id_lead)
      WHERE id_dealership = '$id_dealership2'
      GROUP BY
        d_yearmonth
    ) AS s
    ORDER BY
      d_year            ASC,
      MONTH(d_yearmonth) ASC
    

    Well, LAST_DAY() doesn't really truncate a timestamp, but it does turn all the values belonging to the same month into the same value, which is basically what we need.

    And I guess the counts should be related to the rows you are actually selecting, which is not what your subqueries are. Something like this might do:

    …
    COUNT(d.website = 'newsite.com' OR NULL) AS d_new,
    /* or: COUNT(d.website) - COUNT(NULLIF(d.website, 'newsite.com')) AS d_new */
    COUNT(NULLIF(d.website, 'newsite.com'))  AS d_subprime
    …
    

    Here's the entire query with all the modifications mentioned:

    SELECT
      YEAR(d_yearmonth) AS d_year,
      MONTHNAME(d_yearmonth) AS d_month,
      d_new,
      d_subprime
    FROM (
      SELECT
        LAST_DAY(FROM_UNIXTIME(a.date_assigned)) as d_yearmonth,
        COUNT(d.website = 'newsite.com' OR NULL) AS d_new,
        COUNT(NULLIF(d.website, 'newsite.com'))  AS d_subprime
      FROM assignments AS a
        LEFT JOIN leads AS l ON (l.id = a.id_lead)
      WHERE id_dealership = '$id_dealership2'
      GROUP BY
        d_yearmonth
    ) AS s
    ORDER BY
      d_year            ASC,
      MONTH(d_yearmonth) ASC
    

提交回复
热议问题