我有 2 个 SQL 查询共享一个名为catalogid
查询 #1:
Select
catalogid, numitems, allitems - numitems ignoreditems
from
(select
i.catalogid,
sum(case when (ocardtype in ('PayPal','Sofort') OR
ocardtype in ('mastercard','visa') and
odate is not null) then numitems
else 0 end) numitems,
sum(numitems) allitems
from orders o"
join oitems i on i.orderid=o.orderid"
join products T1 on T1.catalogid = i.catalogid"
group by i.catalogid) X
查询 #2:
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 (o.ocardtype in ('PayPal','Sofort') OR o.ocardtype in ('mastercard','visa') 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
您如何将这 2 个查询合并为一个查询或加入它们?
SqlCommand
注意我从 vb.net运行那些 2
需要注意的一件事是我对两个查询都有相同的条件,我尝试做的是将第二个查询部分添加到未解决的选择案例中
这是涉及的表格
项目表
+---------+-----------+----------+
| orderid | catalogid | numitems |
+---------+-----------+----------+
| o737 | 353 | 1 |
| o738 | 364 | 4 |
| o739 | 353 | 3 |
| o740 | 364 | 6 |
| o741 | 882 | 2 |
| o742 | 224 | 5 |
| o743 | 224 | 2 |
+---------+-----------+----------+
订单表
+-----------------+------------+------------+
| orderid | ocardtype | odate |
+-----------------+------------+------------+
| o737 | Paypal | | 'OK
| o738 | MasterCard | 01.02.2012 | 'OK
| o739 | MasterCard | 02.02.2012 | 'OK
| o740 | Visa | 03.02.2012 | 'OK
| o741 | Sofort | | 'OK
| o742 | | | 'ignore because ocardtype is empty
| o743 | MasterCard | | 'ignore because Mastercard no odate
+-----------------+------------+------------+
查询 #1 的结果:
+-----------+----------+--------------+
| catalogid | numitems | ignoreditems |
+-----------+----------+--------------+
| 353 | 4 | 0 |
| 364 | 10 | 0 |
| 882 | 2 | 0 |
| 224 | 0 | 7 |
+-----------+----------+--------------+
查询 #2 的结果:
+-----------+------------------------+--------------+
| catalogid | ProcessedSucssessfully | NotProcessed |
+-----------+------------------------+--------------+
| 353 |o737,o739 | |
| 364 |o738,o740 | |
| 882 |o741 | |
| 224 | |o742,o743 |
+-----------+------------------------+--------------+
想要的结果:
+-----------+-----------+--------------+-------------------------+---------------+
| catalogid | numitems | ignoreditems | ProcessedSucssessfully | NotProcessed |
+-----------+-----------+--------------+-------------------------+---------------+
| 353 | 4 | 0 | o737,o739 | |
| 364 | 10 | 0 | o738,o740 | |
| 882 | 2 | 0 | o741 | |
| 224 | 0 | 7 | | o742,o743 |
+-----------+-----------+--------------+-------------------------+---------------+
Query1 的条件:
如果
ocardtype
为空,则忽略numitems
并将其视为0
总和并将忽略的项目加到ignoreditems
列中如果某些订单的 ocardtype 是 MasterCard 或 Visa 并且 odate 为空,则忽略 numitems 并将其视为 0 并将忽略的项目加到
ignoreditems
列中如果
ocardtype
是 Paypal 或 Sofort,那么只需计算numitems
总和而不检查日期,因为这些类型不需要odate
Query2 的条件与 Query1 相同,但要做的事情不同:
如果
ocardtype
为空,则添加orderid
到NotProcessed
如果
ocardtype
某些订单是万事达卡或维萨卡并且odate
是空的,则添加orderid
到NotProcessed
如果
ocardtype
是 Paypal 或 Sofort,则不要检查odate
并将其添加orderid
到ProcessedSucssessfully
上述事情是在 2 个单独的查询中完成的,但我试图将其纳入一个查询,因为它们具有相同的条件