Domanda

Ho 2 query SQL che condividono una colonna chiamata catalogid

Query # 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 
.

Query # 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
.

Come faresti quelle 2 in una query o aderire a loro?

Nota I esecuzione di quelle 2 come SqlCommand da vb.net

Una cosa da notare è che ho le stesse condizioni per entrambe le query, ciò che ho provato a fare è aggiungere le seconde parti di query al caso selezionato che non ha funzionato

Ecco le tabelle coinvolte

Tabella 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 |
+---------+-----------+----------+
.

Ordini tabella

+-----------------+------------+------------+
|         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
+-----------------+------------+------------+
.

Risultato dalla query # 1:

+-----------+----------+--------------+
| catalogid | numitems | ignoreditems |
+-----------+----------+--------------+
|       353 |        4 |            0 |
|       364 |       10 |            0 |
|       882 |        2 |            0 |
|       224 |        0 |            7 |
+-----------+----------+--------------+
.

Risultato dalla query # 2:

+-----------+------------------------+--------------+
| catalogid | ProcessedSucssessfully | NotProcessed |
+-----------+------------------------+--------------+
|       353 |o737,o739               |              |
|       364 |o738,o740               |              |
|       882 |o741                    |              |
|       224 |                        |o742,o743     |
+-----------+------------------------+--------------+
.

Il risultato desiderato:

+-----------+-----------+--------------+-------------------------+---------------+
| catalogid | numitems  | ignoreditems | ProcessedSucssessfully  | NotProcessed  |
+-----------+-----------+--------------+-------------------------+---------------+
|       353 |         4 |            0 | o737,o739               |               |
|       364 |        10 |            0 | o738,o740               |               |
|       882 |         2 |            0 | o741                    |               |
|       224 |         0 |            7 |                         | o742,o743     |
+-----------+-----------+--------------+-------------------------+---------------+
.

Le condizioni per query1:

    .
  1. Se ocardtype è vuoto quindi ignorare il numitems e consideralo come 0 nella somma e somma gli elementi ignorati alla colonna ignoreditems

  2. Se OCARDTYPE per qualche ordine è MasterCard o Visa e l'Odate è vuoto, ignora i numeriti e consideralo come 0 e sommare gli elementi ignorati alla colonna ignoreditems

  3. Se ocardtype è PayPal o Sofat, quindi eseguire la somma numitems senza controllare la data perché tali tipi non richiedono odate

  4. Le condizioni per query2 che sono le stesse di query1 ma cose diverse da fare:

      .
    1. Se ocardtype è vuoto, aggiungi orderid a NotProcessed

    2. Se ocardtype per qualche ordine è MasterCard o Visa e il odate è vuoto, aggiungi orderid a NotProcessed

    3. Se ocardtype è PayPal o SoFort, quindi non controllare il controllo odate e aggiungere il orderid a ProcessedSucssessfully

    4. Le cose sopra sono eseguite in 2 query separse, ma sto cercando di arrivare a una query poiché hanno le stesse condizioni

È stato utile?

Soluzione

OK Qui andiamo, ho costruito la struttura del tavolo per convalidare lo SQL. Non ho caricato alcun dato ma viene eseguito.

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
.

Altri suggerimenti

È possibile creare una vista sulla casella SQL e quindi solo Linq a quella vista

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

Modifica:

o

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 View sembrerebbe qualcosa come questo

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
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top