1

几天前我开始学习 SQL,我有以下查询,它在“case”附近给了我一个错误,我似乎无法弄清楚:

SELECT * , Stuff((SELECT ', ' +  CAST( orderid as varchar(10))
      FROM( oitems t2 
 case when (ocardtype in ('PayPal','Sofort') OR
        ocardtype in ('mastercard','visa') and
        odate is not null) then 
              FOR XML PATH(''), 1, 2, '') [Proccessed] else  FOR XML PATH(''), 1, 2, '') [NotProccessed] end)
                      FROM orders t1
    Group by orderid,t1.odate, t1.ocardtype

我猜你知道这段代码应该做什么,但我不确定这是否是正确的使用方式select case

oitems table

+---------+-----------+----------+
| orderid | catalogid | numitems |
+---------+-----------+----------+
| o737    |       353 |        1 |
| o738    |       364 |        4 |
| o739    |       353 |        3 |
| o740    |       364 |        6 |
| o741    |       882 |        2 |
| o742    |       224 |        5 |
| o743    |       224 |        2 |
+---------+-----------+----------+






orders table
    +-----------------+------------+------------+
    |         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
    +-----------------+------------+------------+

预期结果

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

我有以下条件,您可以在上述代码的案例陈述中找到这些条件

  1. 如果ocardtype为空,则添加orderidNotProcessed

  2. 如果ocardtype某些订单是万事达卡或维萨卡并且日期为空,则将 orderid 添加到NotProcessed

  3. 如果ocardtype是 Paypal 或 Sofort,则不要检查日期并将其添加orderidProcessedSucssessfully

4

2 回答 2

2

检查这个请求

SELECT CatalogId, ProcessedSucssessfully = 
       STUFF((SELECT ', ' + b.OrderId
              FROM oitmes b JOIN orders o ON b.OrderId = o.OrderId
              WHERE b.CatalogId = a.CatalogId
              AND (o.OcardtType in ('PayPal','Sofort') OR o.OcardtType in  ('mastercard','visa') and o.odate is not null)    
              FOR XML PATH('')), 1, 2, ''),
                  NotProcessed =
        STUFF((SELECT ', ' + c.OrderId
               FROM oitmes c JOIN orders o ON c.OrderId = o.OrderId
               WHERE c.CatalogId = a.CatalogId
               AND (o.OcardtType in ('mastercard') OR o.OcardtType is null) and o.odate is null
               FOR XML PATH('')), 1, 2, '')
FROM oitmes a
GROUP BY a.CatalogId

SQLFiddle上的演示

于 2012-10-03T13:25:28.740 回答
0

似乎您使用的是之前而不是之后的FROM子句:CASE

SELECT   ProductNumber, Category =
      CASE ProductLine
         WHEN 'R' THEN 'Road'
         WHEN 'M' THEN 'Mountain'
         WHEN 'T' THEN 'Touring'
         WHEN 'S' THEN 'Other sale items'
         ELSE 'Not for sale'
      END,
   Name
FROM Production.Product
ORDER BY ProductNumber

我能想到的另一件事是,您没有返回 true 或 false ,CASE这可能会给您带来错误。

于 2012-10-03T12:01:51.893 回答