group by month and year, count from another table

前端 未结 3 2145
北海茫月
北海茫月 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:53

    Start with

    SELECT 
      MONTHNAME(FROM_UNIXTIME(a.date_assigned)) as d_month, 
      YEAR(FROM_UNIXTIME(a.date_assigned)) as d_year, 
      SUM(IF(l.website='newsite.com',1,0) AS d_new,
      SUM(IF(l.website IS NOT NULL AND l.website!='newsite.com',1,0) 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_month, 
      d_year
    ORDER BY
        d_year asc,
        MONTH(FROM_UNIXTIME(a.date_assigned)) asc
    

    and work from here: The field id_dealership is neither in leads nor in assignments, so you need more work.

    If you edit your question to account for id_dealership we might be able to help you further.

提交回复
热议问题