0
你好,我已經產生以下結果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
下面的SQL查詢的這個查詢,你可以在下面的圖片中看到的結果
你看那些2盤旋的行,我希望他們在一個行中,只會將numitems,處理成功的價值和所有其他值將始終相同的記錄共享catalogid所以沒有問題與他們。
basicly結果行應該在具有相同採用catalogId
那麼,如何解決這個問題的所有行的numitem valuse的總和?
上心謝謝:) – user1570048