我有一个表名作为用户(id,Name,Status,joined_date)。
用户表
id | Name | status | joined_date
-------------------------------------
1 | TDS | 0 | 2014-10-16
2 | TDS | 1 | 2014-10-11
3 | VAT | 0 | 2014-10-11
4 | IT | 0 | 2014-10-11
5 | IT | 1 | 2014-10-11
6 | TDS | 0 | 2014-10-16
7 | TDS | 1 | 2014-10-30
8 | VAT | 0 | 2014-11-01
9 | IT | 0 | 2014-10-07
10 | IT | 1 | 2014-10-11 在我的结果中,我需要当前月份的名称计数和当前月份的名称计数,状态为1。对于每个名称,两者都会产生相同的行。
预期结果
Name | current_month | current_month_ststus_1
-----------------------------------------------------
VAT | 2 | 0
IT | 4 | 1
TDS | 4 | 2对于最后一个days.Please的查询,我有点困惑,如果可能的话,请帮助我。提前感谢
发布于 2014-10-16 06:14:51
选择名称,计数(状态)作为current_month,和(状态)从
usertable选择current_month_status,其中月份(Joined_date)=月份(CURDATE())按名称分组
发布于 2014-10-16 06:05:48
如果状态只允许0,1,
你可以得到这样的结果。
select count(Name) as current_month,sum(status) as current_month_status_1 from table group by currentmonth
发布于 2014-10-16 06:12:40
呃,我在想下面这样的事情。
SELECT
Name,
COUNT(Status) AS current_month,
SUM(status) AS current_month_status_1
FROM
Usertable
WHERE
MONTH(joined_date) = MONTH(NOW())
AND YEAR(joined_date) = YEAR(NOW())
GROUP BY
Name,
YEAR(joined_date),
MONTH(joined_date)这假设状态只能是1或0。
https://stackoverflow.com/questions/26397214
复制相似问题