javascript - 如何对具有多个条件的多列求和并显示在一个中
问题描述
我有一个这样的表,现在我需要获取所有数据 GROUP BY tpi
+---------------------+---------+----------------+
| trxDate | trxType | tpi |
+---------------------+---------+----------------+
| 2018-06-28 00:00:00 | Sent | SI0005 |
| 2018-07-02 00:00:00 | Sent | SI0005 |
| 2018-07-04 00:00:00 | Sent | SI0005 |
| 2018-05-25 00:00:00 | Open | SI0007 |
| 2018-06-26 00:00:00 | Open | SI0007 |
| 2018-05-25 00:00:00 | Sent | SI0007 |
| 2018-06-23 00:00:00 | Sent | SI0007 |
+---------------------+---------+----------------+
我需要记录来汇总打开和发送
+---------------------+---------+----------------+
| tpi | open | sent |
+---------------------+---------+----------------+
| SI0005 | 0 | 3 |
| SI0007 | 2 | 2 |
+---------------------+---------+----------------+
我尝试了一些子查询,但没有得到想要的响应
SELECT tblcount.tpi, tblcount.qType, count(tblcount.id) total FROM (SELECT id, tpi, 'Open' qType FROM `tbl`WHERE trxType = 'Open' UNION SELECT id, tpi, 'Sent' qType FROM `tbl` WHERE trxType = 'Sent') tblcount GROUP BY tblcount.third_party_id, tblcount.qType
解决方案
您可以进行条件聚合:
select tpi,
sum(trxType = 'Open') as trxType_open,
sum(trxType = 'Sent') as trxType_Sent
from tbl t
group by tpi;
推荐阅读
- unity3d - Unity - 使用 BinaryFormatter 保存列表的问题
- swift - 错误。无法将类型“[UInt32]”的值分配给类型“UInt32”
- kubernetes - 如何使用 Istio 创建服务别名?
- sql - Microsoft Access DB 的 SQL 表语法说明
- ios - 可观察地图 Rxswift 中的切换案例
- java - 在 Java Spring 中使用外部 API 服务器进行压力测试 - 模拟响应?
- c - 运行程序 3 秒
- powershell - 一次重命名多个包含不同字符的文件
- html - HTML 电子邮件中的粗体内联文本被推送到 Outlook 中的新行
- xml - XPath 用于在 Google 表格中使用 ImportXML 标记