- 我想根据以下条件构建查询,
- 我只想合并类型 1 和类型 2 的总价。
- 将 2 行合并为 1 行并避免在该行中出现空值。
查询,
select B.ID as BillId,
I.Id as ItemID,
t.Type,
SUM(I.TotalPrice) as TotalPrice,
Case when t.Type = 1 then SUM(T.Amount) end as TaxAmountType1,
Case when t.Type = 2 then SUM(T.Amount) end as TaxAmountType2
from Bill B
inner join ItemDetails I
on I.BillNoID = B.ID
inner join TaxDetails T
on T.ItemDetailId = I.Id
group by
B.ID,
I.Id,
I.TotalPrice,
t.Type;
请引用下图,
预期输出:
'1', '1', NULL, '40.00', '3.00', '5.00'
注意: 1.我不想使用子查询。 2. 预期输出“NULL”表示我不想单独显示该列。
最佳答案
听起来这样可以解决您的问题。如果不是,请提供更多信息。
SELECT B.ID as BillId, I.Id as ItemId,
t.type, SUM(TotalPrice), SUM(TaxAmountType1),
SUM(TaxAmountType2)
FROM Bill B
GROUP BY BillId, ItemId, t.type
关于mysql - 在 MySQL 中合并多行时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22557106/