【问题标题】:SQL Server Select Query Using Group by with unionSQL Server 使用 Group by 和 union 选择查询
【发布时间】:2018-07-19 06:24:03
【问题描述】:

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

  • GenerateTicket - 用于按票号返回服装
  • GenerateTicket_Return - 用于返回服装,使用服装 ID 或名称(批量)

查询

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

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

【问题讨论】:

  • 你认为这些是重复的,因为我可以看到你标记的每个案例的总金额都不同
  • 请同时显示预期输出
  • @ZaynulAbadinTuhin ya 实际上这是主要问题,totalAmount 不同,因为我将 groupby 与 totalAmount 一起使用,但我想将服装名称显示的所有详细信息合并在单行中,但我不能
  • @Ravi 我想代表服装名称在单行中显示所有服装细节(当总金额为差异时)
  • @aparna totalAmount 和 custumetotalprice 应该是 sum

标签: sql-server database


【解决方案1】:

只使用子查询

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

【讨论】:

  • @aparna rai 有帮助吗?
【解决方案2】:

试试这个,请说明你到底需要什么

select 
    Temp.CostumeName, usm(temp.costumetotalprice) costumetotalprice, 
    sum(Temp.Quantity) Quantity, sum(isnull(Temp.RQty,0) RQty, 
    sum(Temp.Quantity) - sum(isnull(Temp.RQty,0)  PendingQty,
    sum(Temp.refund * Temp.Quantity) as refund,
    sum(Temp.REFUNDED) as Refunded, 
    sum(temp.costumetotalprice) - sum(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
group by CostumeName

【讨论】:

    【解决方案3】:

    只需使用这个

    SELECT CostumeName,SUM( ISNULL( IssQuantity,0) ) AS IssQuantity , SUM(ISNULL( costumetotalprice,0)) as CostumeTotalPrice ,SUM(ISNULL( RetQuantity,0)) as RQty,
         SUM(ISNULL(refund,0)) as Refund ,SUM(ISNULL( IssQuantity,0)) -  SUM(ISNULL( RetQuantity,0)) AS PendingQty,SUM(ISNULL( costumetotalprice,0)) + SUM(ISNULL( refund,0)) AS TotalRent  FROM (
        select SUM(ISNULL( costumetotalprice,0))  as costumetotalprice,  sum(ISNULL( CostumeQuantity,0)) As IssQuantity,CASE WHEN RefundAmount>0 THEN SUM(CostumeQuantity) ELSE 0 END As RetQuantity,
        SUM(A.refundamount) as refund,
        CostumeName from GenerateTicket as A
        group by A.CostumeName,A.CostumeCode,RefundAmount 
        union all
        select 0  as costumetotalprice,  0 As IssQuantity,SUM(CostumeQuantity) As RetQuantity,
        SUM(-A.costumetotalprice) as refund,
        CostumeName from GenerateTicket_Return as A
        group by A.CostumeName,A.CostumeCode
        )TMP 
        GROUP BY CostumeName
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-30
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      相关资源
      最近更新 更多