【发布时间】:2014-11-20 13:33:36
【问题描述】:
我有这张表(tableName):
| IdPerson | Date | idPerson | idControls | Status
| 125 | 2014-11-01 | 106 | 4 | 1 |
| 126 | 2014-12-01 | 109 | 3 | 0 |
| 127 | 2014-13-01 | 112 | 2 | 1 |
| 128 | 2014-14-01 | 115 | 4 | 0 |
| 129 | 2014-14-01 | 118 | 3 | 0 |
| 130 | 2014-16-01 | 121 | 4 | 1 |
我在执行这些查询时得到了正确的结果:
QUERY1:
select Date,count(distinct idControls) from tableName where Status=0 group by Date;
+------------+-------------------------------------+
| Date | count(distinct idControls) |
+------------+-------------------------------------+
| 2014-12-01 | 1 |
| 2014-14-02 | 2 |
查询2:
select Date,count(distinct idControls) from tableName where Status=1 group by Date;
+------------+-------------------------------------+
| Date | count(distinct idControls) |
+------------+-------------------------------------+
| 2014-11-01 | 1 |
| 2014-13-02 | 1 |
| 2014-16-02 | 1 |
但是,我想做的是将两个查询都包含在一个查询中。
然后,我正在尝试做这样的事情:
select Date,sum(distinct(idControls) case when Status='1' then 1 else 0 end) fail ,sum(distinct case when Status='0' then 1 else 0 end) correct from tableName group by Date;
显然,上面的查询不起作用....
有什么想法吗?
【问题讨论】: