sql-server - 根据列中的值对行组求和
解决方案
尝试如下查询。
select
[Account Group] =AG.Name,
[Ledger Name] =AL.Name,
DR =SUM(CASE WHEN TT.Name='DR' THEN Amount ELSE NULL END),-- case based SUM expression
CR =SUM(CASE WHEN TT.Name='CR' THEN Amount ELSE NULL END)
from Account_Group AG
join Account_ledgers AL -- NOTE we have used INNER JOIN throughout
on AG.Id=AL.AccountGroupId
join Transactions T
on T.AccountLedgerId=AL.Id
join Transaction_Type TT
on TT.Id= T.TransactionTypeId
group by AG.Name,AL.Name -- Group by contains all the things we need in SELECT list
推荐阅读
- powershell - 用于扫描 OU 中所有服务器的过期 SSL 证书的 Powershell 脚本不起作用
- elasticsearch - 在 Elasticsearch 中使用预定义的 geo_shapes 创建文档
- android - 按下后退按钮时保存包含按钮的 Activity 状态 android
- php - 将文件时间转换为时间(从变量)PHP
- kotlin - 如何将粗体、斜体、下划线等格式添加到 Kotlin 文档 (KDoc)
- html - 使用 CSS 调整小部件的大小
- sql - 使用来自一个查询的(多个)结果作为另一个查询的输入
- docker - 将 ca 证书添加到使用指定用户运行的 Tomcat Docker 容器
- python - Python pandas 删除具有列值“NaN”的重复行
- php - 来自mysql的所有数据都没有按php条件显示