按月和按年分组,从另一个表计数

时间:2012-03-14 09:53:36

标签: mysql select join count unix-timestamp

我试图让我的查询按assignments表中的月份和年份对行进行分组,并计算leads表中具有特定值的行数。它们被链接在一起,因为assignments表格有id_lead字段,这是id表格中行的leads

d_new将计算其网站为newsite.com的月份的潜在客户分配 d_subprime将计算其网站不是newsite.com的月份的潜在客户分配

这是使用的表格:

`leads`
id (int)
website (varchar)

`assignments`
id_lead (int)
date_assigned (int)

继承我的查询无效:

SELECT 
  MONTHNAME(FROM_UNIXTIME(a.date_assigned)) as d_month, 
  YEAR(FROM_UNIXTIME(a.date_assigned)) as d_year, 
  (select COUNT(*) from leads where website='newsite.com' ) as d_new,
  (select COUNT(*) from leads where 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_month, 
  d_year
ORDER BY
    d_year asc,
    MONTH(FROM_UNIXTIME(a.date_assigned)) asc

$id_dealership是一个变量,其中包含我试图查看其数量的经销商ID。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

您可以将时间戳截断为几个月,并使用获得的值进行分组,然后从中获取必要的日期部分:

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

嗯,LAST_DAY()并没有真正截断时间戳,但它确实将属于同一个月的所有值都转换为相同的值,这基本上就是我们所需要的。

我猜计数应与您实际选择的行相关,而不是您的子查询。这样的事情可能会这样:

…
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
…

以下是提及所有修改的整个查询:

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

答案 1 :(得分:0)

这应该可以解决问题:

SELECT
YEAR(FROM_UNIXTIME(a.date_assigned)) as d_year, 
MONTHNAME(FROM_UNIXTIME(a.date_assigned)) as d_month,
l.website,
COUNT(*)
FROM
assignments AS a
INNER JOIN leads AS l on (l.id = a.id_lead) /*are you sure, that you need a LEFT JOIN?*/
WHERE id_dealership='$id_dealership2'
GROUP BY
d_year, d_month, website
/*an ORDER BY is not necessary, MySQL does that automatically when grouping*/

如果确实需要LEFT JOIN,请注意COUNT()忽略NULL值。如果你想计算那些(我无法想象有意义),就这样写:

SELECT
YEAR(FROM_UNIXTIME(a.date_assigned)) as d_year, 
MONTHNAME(FROM_UNIXTIME(a.date_assigned)) as d_month,
l.website,
COUNT(COALESCE(l.id, 1))
FROM
assignments AS a
LEFT JOIN leads AS l on (l.id = a.id_lead)
WHERE id_dealership='$id_dealership2'
GROUP BY
d_year, d_month, website

答案 2 :(得分:0)

开始
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

从此处开始工作:字段id_dealership既不在leads也不在assignments,因此您需要更多工作。

如果您将问题编辑为id_dealership,我们可以为您提供进一步的帮助。