您好,我有以下 SQL 查询,它产生以下结果
SELECT * FROM
(
Select
catalogid, numitems, allitems - numitems ignoreditems
from
(
select
i.catalogid,
case
when Exists(select paymentodate from payentmethodvalidation q where q.paymentnoodate = o.ocardtype) AND NOT EXISTS(SELECT booked FROM bookedordersids m where CAST(m.booked AS int)=o.orderid) then sum(i.numitems)
when Exists(select paymentodate from payentmethodvalidation q where q.paymentodate = o.ocardtype) AND odate is not null AND NOT EXISTS(SELECT booked FROM bookedordersids m where CAST(m.booked AS int)=o.orderid) then sum(i.numitems)
else 0 end numitems,
sum(numitems) allitems
from
"orders o
inner join
"oitems i
on
"i.orderid=o.orderid
inner join
"products T1
on
"T1.catalogid = i.catalogid
group by
"i.catalogid, ocardtype, odate,o.orderid
) A
) B
INNER JOIN
(
SELECT
catalogId,
ProcessedSucssessfully =
STUFF((SELECT ', ' + CAST( b.orderid as varchar(10))
FROM oitems b JOIN orders o ON b.orderid = o.orderid
WHERE b.catalogId = a.catalogId
AND NOT EXISTS(SELECT booked FROM bookedordersids m where CAST(m.booked AS int)=o.orderid) AND (Exists(select paymentodate from payentmethodvalidation q where q.paymentnoodate = o.ocardtype) OR Exists(select paymentodate from payentmethodvalidation q where q.paymentodate = o.ocardtype) and o.odate is not null)
FOR XML PATH('')), 1, 2, ''),
"NotProcessed =
STUFF((SELECT ', ' + CAST( c.orderid as varchar(10))
FROM oitems c JOIN orders o ON c.orderId = o.orderid
WHERE c.catalogid = a.catalogid
AND (o.ocardtype in ('mastercard') OR o.ocardtype is null) and o.odate is null
FOR XML PATH('')), 1, 2, '')
FROM
oitems a
GROUP BY
a.catalogid
)C
ON
B.catalogid = C.catalogid
您可以在下图中看到此查询的结果
您会看到那 2 个带圆圈的行,我希望它们位于一行中,仅对 numitems 进行求和,处理成功的值和所有其他值对于共享目录 ID 的记录将始终相同,因此它们没有问题。
基本上,结果行应该具有所有具有相同目录 ID 的行中的 numitem 值的总和
那么我该如何解决这个问题呢?