gpt4 book ai didi

SQL 选择案例

转载 作者:行者123 更新时间:2023-12-04 20:49:11 24 4
gpt4 key购买 nike

我有以下 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 的产品的 numitems 列求和取决于 oitems 中的数据具有以下条件的表
  • 如果 ocardtype为空然后忽略 numitems并考虑它
    0在总和中,将忽略的项目相加到 ignoreditems 栏目
  • 如果 ocardtype对于某些订单是 MasterCardVisa
    odate为空然后忽略 numitems并将其视为
    0并将忽略的项目与 ignoreditems 相加栏目
  • 如果 ocardtypePaypalSofort ,然后只需执行 numitems
    不检查日期,因为这些类型不需要 odate

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

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

    最佳答案

    就像是:

    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 )

    关于SQL 选择案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12668630/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com