【发布时间】: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