[英]Joining Two Query results on the basis of one column
我有这样的表结构。 表名是z_notes
v_resident_fname
(基本上是名称)
i_communication_log
(布尔值,包含0或1,0表示常规注释,1表示关键注释)
dt_note_created
(日期)
因此,我需要做的是,首先我需要所有名称及其计数,其中i_communication_log = 0
且dt_note_created < 1332792382
和dt_note_create > 1332792382
。
因此,此查询为:
SELECT v_resident_fname as Name, COUNT( * ) AS Critical_Notes
FROM z_notes
WHERE dt_note_created >1332792382
AND dt_note_created <1332892382
AND `i_communication_log` = 1
GROUP BY v_resident_fname
返回:
______________________
|Name |Critical_Notes|
| abc | 7 |
| xyz | 4 |
另一个查询是相同的,但i_communcation_log = 0
SELECT v_resident_fname as Name, COUNT( * ) AS Routine_Notes
FROM z_notes
WHERE dt_note_created >1332792382
AND dt_note_created <1332892382
AND `i_communication_log` = 0
GROUP BY v_resident_fname
_____________________
|Name |Routine_Notes |
| abc | 3 |
| xyz | 4 |
我想将这两个查询合并为一个基于Name的查询,以使最终结果类似于:
____________________________________
|Name |Critical_Notes|Routine_Notes|
| abc | 7 | 3 |
| xyz | 4 | 4 |
尝试这个:
SELECT
v_resident_fname as Name,
SUM(CASE i_communication_log WHEN 1 THEN 1 ELSE 0 END) AS Critical_Notes,
SUM(CASE i_communication_log WHEN 0 THEN 1 ELSE 0 END) AS Routine_Notes
FROM z_notes
WHERE dt_note_created >1332792382
AND dt_note_created <1332892382
GROUP BY v_resident_fname
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.