sql-server - SQL Server 使用 Group by 和 union 选择查询

标签 sql-server database

我在 SQL Server 中有两个表,它们都有相同的列:

  • GenerateTicket - 用于按票号归还服装
  • GenerateTicket_Return - 用于通过使用 costumeid 或名称(批量)返回服装

查询

select 
    Temp.CostumeName, temp.costumetotalprice, 
    Temp.Quantity, Temp.RQty, 
    Temp.Quantity - temp.rqty  as PendingQty,
    Temp.refund * Temp.Quantity as refund,
    Temp.REFUNDED, 
    temp.costumetotalprice - temp.REFUNDED  as TotalAmount 
from 
    (select 
         sum(costumetotalprice) as costumetotalprice,  
         sum(CostumeQuantity) as Quantity,
         c.refundamount as refund,
         sum(ISNULL(b.RefundAmount, 0)) as REFUNDED,
         (select sum(ISNULL(CostumeQuantity, 0)) 
          from GenerateTicket 
          where CostumeCode = b.CostumeCode 
            and IsRefund = 0 
            and RefundAmount <> 0.00) as RQty,
         (select sum(ISNULL(CostumeQuantity, 0)) 
          from GenerateTicket 
          where CostumeCode = b.CostumeCode 
            and IsRefund = 1 
            and RefundAmount = 0.00) as PendingQty,
          sum(ISNULL(CostumeTotalPrice, 0) - ISNULL(b.RefundAmount, 0)) as TotalAmount,
          c.CostumeName 
      from 
          GenerateTicket as b 
      inner join 
          MasterCostume as c on b.costumecode = c.ID
      where 
          1 = 1  
      group by 
          c.CostumeName, b.CostumeCode, c.refundamount 

     union  

     select 
         sum(costumetotalprice) as costumetotalprice,  
         sum(CostumeQuantity) As Quantity,
         c.refundamount as refund,
         sum(ISNULL(b.RefundAmount,0)) as REFUNDED,
         (select sum(ISNULL(CostumeQuantity, 0)) 
          from GenerateTicket 
          where CostumeCode = b.CostumeCode 
            and IsRefund = 0 
            and RefundAmount<>0.00) as RQty,
         (select sum(ISNULL(CostumeQuantity, 0)) 
          from GenerateTicket 
          where CostumeCode = b.CostumeCode 
            and IsRefund = 1 
            and RefundAmount = 0.00) as PendingQty ,
         sum(ISNULL(CostumeTotalPrice, 0) - ISNULL(b.RefundAmount, 0)) as TotalAmount,
         c.CostumeName 
     from 
         GenerateTicket_Return as b 
     inner join 
         MasterCostume as c on b.costumecode = c.ID
     where 
         1 = 1  
     group by 
         c.CostumeName,b.CostumeCode,c.refundamount) Temp

但问题是我得到了一些像这样的重复数据:enter image description here

最佳答案

只使用子查询

select CostumeName,costumetotalprice,Quantity,PendingQty,sum(refund) as refund,sum(REFUNDED) as REFUNDED,sum(TotalAmount) as TotalAmount from
    (
    select 
        Temp.CostumeName, temp.costumetotalprice, 
        Temp.Quantity, Temp.RQty, 
        Temp.Quantity - temp.rqty  as PendingQty,
        Temp.refund * Temp.Quantity as refund,
        Temp.REFUNDED, 
        temp.costumetotalprice - temp.REFUNDED  as TotalAmount 
    from 
        (select 
             sum(costumetotalprice) as costumetotalprice,  
             sum(CostumeQuantity) as Quantity,
             c.refundamount as refund,
             sum(ISNULL(b.RefundAmount, 0)) as REFUNDED,
             (select sum(ISNULL(CostumeQuantity, 0)) 
              from GenerateTicket 
              where CostumeCode = b.CostumeCode 
                and IsRefund = 0 
                and RefundAmount <> 0.00) as RQty,
             (select sum(ISNULL(CostumeQuantity, 0)) 
              from GenerateTicket 
              where CostumeCode = b.CostumeCode 
                and IsRefund = 1 
                and RefundAmount = 0.00) as PendingQty,
              sum(ISNULL(CostumeTotalPrice, 0) - ISNULL(b.RefundAmount, 0)) as TotalAmount,
              c.CostumeName 
          from 
              GenerateTicket as b 
          inner join 
              MasterCostume as c on b.costumecode = c.ID
          where 
              1 = 1  
          group by 
              c.CostumeName, b.CostumeCode, c.refundamount 

         union  

         select 
             sum(costumetotalprice) as costumetotalprice,  
             sum(CostumeQuantity) As Quantity,
             c.refundamount as refund,
             sum(ISNULL(b.RefundAmount,0)) as REFUNDED,
             (select sum(ISNULL(CostumeQuantity, 0)) 
              from GenerateTicket 
              where CostumeCode = b.CostumeCode 
                and IsRefund = 0 
                and RefundAmount<>0.00) as RQty,
             (select sum(ISNULL(CostumeQuantity, 0)) 
              from GenerateTicket 
              where CostumeCode = b.CostumeCode 
                and IsRefund = 1 
                and RefundAmount = 0.00) as PendingQty ,
             sum(ISNULL(CostumeTotalPrice, 0) - ISNULL(b.RefundAmount, 0)) as TotalAmount,
             c.CostumeName 
         from 
             GenerateTicket_Return as b 
         inner join 
             MasterCostume as c on b.costumecode = c.ID
         where 
             1 = 1  
         group by 
             c.CostumeName,b.CostumeCode,c.refundamount) Temp
    ) as T
    group by CostumeName,costumetotalprice,Quantity,PendingQty

关于sql-server - SQL Server 使用 Group by 和 union 选择查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51415697/

相关文章:

sql - 查找 'break'记录

database - 为 Netezza 数据库 (nzSQL) 配置 Django

android - 应用程序返回空 toast

java - 我的数据库中有两个表。如果其他表不是该记录,我想从一个表中选择记录

database - 从两个不同的表中得到不同的计数?

c# - 使用 SqlCommand 从多个表中删除

c# - 更快的 SQL 插入?

node.js - 如何从数据库中清除过期的 token ?

sql-server - .NET SqlClient 错误号枚举类型

sql-server - 如何列出给定 sql 查询的所有列