我有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变成一个查询或加入它们?

请注意,我将这些2运行为 SqlCommand 从vb.net

有一件事要注意的是,我对两个查询都有相同的条件,我试图做的是将第二个查询部分添加到select case中,而select case没有成功

以下是涉及的表格

oitems表

+---------+-----------+----------+
| 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的条件:

  1. 如果 ocardtype 是空的,然后忽略 numitems 并将其视为 0 在总和和总和被忽略的项目到 ignoreditems 专栏

  2. 如果某些订单的ocardtype为MasterCard或Visa,并且odate为空,则忽略numitems并将其视为0,并将忽略的项目总和为 ignoreditems 专栏

  3. 如果 ocardtype 是贝宝还是索福特,那么就做 numitems 求和而不检查日期,因为这些类型不需要 odate

Query2的条件与Query1相同但要做的事情不同:

  1. 如果 ocardtype 是空的,然后添加 orderidNotProcessed

  2. 如果 ocardtype 对于一些订单是万事达卡或维萨和 odate 是空的,然后添加 orderidNotProcessed

  3. 如果 ocardtype 是Paypal还是Sofort,那就不检查了 odate 并添加 orderidProcessedSucssessfully

上面的事情是在2个单独的查询中完成的,但是我试图将其添加到一个查询中,因为它们具有相同的条件

有帮助吗?

解决方案

好了,我们开始,我构建了表结构来验证SQL。我没有加载任何数据,但它确实执行。

SELECT * FROM
(
Select  
   catalogid, numitems, allitems - numitems ignoreditems 
from 
    (
        select  
            i.catalogid, 
            case 
                when ocardtype in ('PayPal','Sofort') then sum(i.numitems)
                when ocardtype in ('mastercard','visa') and odate is not null 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
    ) 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 (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 
)C
    ON 
        B.CatalogId = C.CatalogId

其他提示

您可以在sql框上创建一个视图,然后只需Linq到该视图

select * from table_a inner join table_b on table_a.field_a = table_b.field_b

编辑:

Select * from (select * from table_a inner join table_b on table_a.field_a = table_b.field_b) AB
inner join
select * from (select * from table_c inner join table_d on table_c.field_c = table_d.field_d) CD
ON
AB.id_column = CD.id_column

SQL server视图看起来像这样

SELECT * FROM
(
    Select  
       catalogid, numitems, allitems - numitems ignoreditems 
    from 
        (
        select * 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
        inner join
            items i 
        on 
            i.orderid=o.orderid
        inner join 
            products T1 
        on 
            T1.catalogid = i.catalogid
        group by 
            i.catalogid
        ) A
    ) X 
) B
INNER JOIN
SELECT * FROM
(
    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 
) B
ON A.CatalogId = B.CatalogId
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top