สวัสดี ฉันมี SQL Query ต่อไปนี้ซึ่งให้ผลลัพธ์ดังต่อไปนี้
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 แถวนั้น ฉันต้องการให้พวกมันอยู่ในแถวเดียวที่จะรวมเฉพาะตัวเลขเท่านั้น การประมวลผลค่าสำเร็จและค่าอื่น ๆ ทั้งหมดจะเหมือนกันเสมอสำหรับบันทึกในแค็ตตาล็อกที่ใช้ร่วมกัน ดังนั้นจึงไม่มีปัญหากับพวกเขา
โดยพื้นฐานแล้วแถวผลลัพธ์ควรมีผลรวมของค่า numitem ในทุกแถวที่มีแคตตาล็อกเดียวกัน
แล้วฉันจะแก้ไขปัญหานี้ได้อย่างไร?