SQL选择案例

时间:2012-10-01 06:47:48

标签: sql vb.net

我有以下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 |
+-----------+----------+--------------+

想法是对具有以下条件的catalogid表中数据具有相同oitems描述的产品的numitems列进行求和

  1. 如果ocardtype为空,则忽略numitems并考虑它     汇总为0,并将忽略的项目汇总到ignoreditems     柱
  2. 如果某个订单的ocardtypeMasterCardVisa而且     odate为空,然后忽略numitems并将其视为     0并将忽略的项目汇总到ignoreditems
  3. 如果ocardtypePaypalSofort,则执行numitems 和     没有检查日期,因为这些类型不需要odate
  4. 基本上我想将结果数据表保存到临时数据表并将其加载到vb.net数据表

    我很难搞清楚如何在SQL查询中执行此操作!我需要这个作为vb.net的sql命令,能够使用循环和很多检查使用vb.net数据表以编程方式执行此操作 使用linq是一个选项,但我只需要从服务器获取它

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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