SQL Server选择查询,并结合使用分组依据

时间:2018-07-19 06:24:04

标签: sql-server database

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

  • GenerateTicket-用于通过票证编号
  • 返还服装
  • GenerateTicket_Return-用于通过使用服装标识或名称(批量)来返回服装

查询

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

3 个答案:

答案 0 :(得分:0)

只使用子查询

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

答案 1 :(得分:0)

尝试一下,请说明您到底需要什么

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

答案 2 :(得分:0)

只需使用

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