mysql - 在 MySQL 中合并多行时出现问题

标签 mysql sql

  1. 我想根据以下条件构建查询,
  2. 我只想合并类型 1 和类型 2 的总价。
  3. 将 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;

请引用下图, enter image description here

enter image description here 预期输出:

'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/

相关文章:

mysql - 使用 Django 从数据库中选择用于聚合计算的日期格式

java - 如何在我的 Eclipse 中使用逆向工程

PHP 和 MySQL : Using $_GET ['id' ] to query matching table id

sql - 通用 MERGE 的存储过程

mysql - sql查询打印ID :Name mapping

mysql - SQL - 从多个表中进行选择,其中一个表不返回结果

php - 使用 php 连接到数据库对我不起作用。用户 'ahmadham_ahmad' 的访问被拒绝

mysql - 如何对 varchar 数字列进行排序?

sql - Angular JS/SQLite/查询

SQL Server : does an CTE update work like a transaction?