1

我有以下 sql 查询来了解它的作用请阅读下面的描述

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) AND NOT EXISTS (
                         select * from booked b
                         where b.ignoredoid = o.orderid
                       ) then numitems
                       else 0 end) numitems,
        sum(numitems) allitems
      from orders o
      join oitems i on i.orderid=o.orderid
      group by i.catalogid
    ) X

以及以下 sql 表

项目表

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

结果数据表

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

想法是根据 oitems 表中的数据对具有相同目录 ID 的产品的 numitems 列求和,并具有以下条件

  1. 如果ocardtype为空,则忽略numitems并将其视为0 总和并将忽略的项目加到ignoreditems列中
  2. 如果ocardtype某些订单是 MasterCard 或 Visa 并且odate为空 然后忽略numitems并将其视为0并将忽略的项目汇总到ignoreditems
  3. 如果 ocardtype 是 Paypal 或 Sofort,那么只需进行numitems总和而不检查,odate因为这些类型不需要odate
  4. 在另一个名为booked 的表中,我有一个名为ignoreoid 的列,此列包含orderids上表中我想忽略的列,即使满足上述3 个条件

至此,感谢@Richard aka cyberkiwi 对他在这个问题上的回答,查询运行良好


问题是,我需要结果数据表如下所示

+-----------+----------+--------------+-------------------+
| catalogid | numitems | ignoreditems | orderidcollection |
+-----------+----------+--------------+-------------------+
|       353 |        4 |            0 | O737,O739         |
|       364 |       10 |            0 | O738,O740         |
|       882 |        2 |            0 | O741              |
|       224 |        0 |            7 |                   |'O742 & O743 are ignored      
+-----------+----------+--------------+-------------------+

如您所见,唯一的变化是添加了orderidcollection列,只有在代码中没有忽略该顺序时,它才必须将 以逗号分隔的新列添加orderid到新列中,我已经搜索了几个小时没有运气!这甚至可以用 SQL 吗?

4

1 回答 1

1

我对您上一个问题的 Linq-to-Sql 回答扩展到了这个问题(LINQpad 查询):

Dim odateRequired = {"MasterCard", "Visa"}
Dim odateNotRequired = {"Paypal", "Sofort"}

Dim result = From o In Orders Join i In Oitems On o.orderid Equals i.orderid _
             Let check = Not (From b In Bookeds Where b.ignoredoid=i.orderid).Any _
                     AndAlso o.ocardtype IsNot Nothing _
                     AndAlso ((odateRequired.Contains(o.ocardtype) AndAlso o.odate IsNot Nothing) _
                        OrElse odateNotRequired.Contains(o.ocardtype)) _
         Group By i.catalogid Into _
         numitems = Sum(If(check, i.numitems, 0)), _
         ignoreditems = Sum(If(check, 0, i.numitems)), _
         group
         Select catalogid, numitems, ignoreditems, _
             orderidcollection = String.Join(",", (From g in group Where g.check Select g.i.orderid))
result.Dump

请注意,此解决方案会发出多个 SQL 查询来确定orderidsfor each catalogidwherecheckTrue要累积 eachorderidcollection的。String.Join可能有一个更有效的解决方案(或者使用一些可能令人费解的 Linq- catalogid, numitems, check, orderidto -Sql 导致生成的 SQL 执行相当于最后的积累)。

另请注意,此查询包括您的Booked表(使用 LINQpad 的复数形式)。

于 2012-10-02T11:37:25.697 回答