我在 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
最佳答案
只使用子查询
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/