5

我有以下 sql 表

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

调用的结果数据表result

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

想法是将具有相同依赖关系的产品的 numitems 列与表catalogid中的数据相加,并oitems具有以下条件

  1. 如果ocardtype为空,则忽略numitems并将其视为0总和并将忽略的项目加到ignoreditems 列中
  2. 如果ocardtype某个订单是MasterCardorVisa并且odate是空的,则忽略numitems并将其视为 0并将忽略的项目加到ignoreditems列中
  3. 如果ocardtypePaypalor Sofort,那么只做numitems 总和而不检查日期,因为这些类型不需要odate

基本上我想将结果数据表保存到临时数据表并将其加载到 vb.net 数据表

我很难弄清楚如何在 sql 查询中执行此操作!我需要这个作为 vb.net 的 sql 命令,能够使用循环使用 vb.net 数据表以编程方式执行它,并且可以选择使用 linq 进行大量检查,但我只需要从服务器获取它

4

3 回答 3

1

就像是:

SELECT
     oi.catalog_id,
     SUM(CASE
            WHEN ocardtype in ('Paypal','Sofort') THEN numitems
            WHEN ocardtype in ('Mastercard','Visa') and odate is not null THEN numitems
            ELSE 0 END) as numitems,
     SUM(CASE
            WHEN ocardtype is null then numitems
            WHEN ocardtype in ('Mastercard','Visa') and odate is null THEN numitems
            ELSE 0 END) as ignoreditems
FROM
   oitems oi
      inner join
   Orders o
      on
         oi.orderid = o.orderid
GROUP BY
   oi.catalog_id

(假设您在叙述中使用了“空”这个词,您的意思是该栏是NULL

于 2012-10-01T06:53:10.520 回答
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) 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
于 2012-10-01T06:54:39.957 回答
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 = o.ocardtype IsNot Nothing _
                     AndAlso ((odateRequired.Contains(o.ocardtype) _
                               AndAlso o.odate IsNot Nothing) _
                        OrElse odateNotRequired.Contains(o.ocardtype)) _
         Group By i.catalogid Into _
         numitem = Sum(If(check, i.numitems, 0)), _
         ignored = Sum(If(check, 0, i.numitems))

result.Dump

您对 RichardTheKiwi 的回答的评论中的额外要求(它只包括Not (From b In Bookeds Where b.ignoredoid=i.orderid).Any AndAlso在前面check):

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 _
         numitem = Sum(If(check, i.numitems, 0)), _
         ignored = Sum(If(check, 0, i.numitems))

result.Dump
于 2012-10-02T10:34:39.190 回答