【问题标题】:Add sub-condition in the where clause在 where 子句中添加子条件
【发布时间】:2016-12-16 20:06:44
【问题描述】:

我不确定在 where 子句中给这个条件取什么合适的名字。我正在使用 Microsoft SQL Server 2008。

基本上,我有一个包含以下列的订单表。

我的订单表:

OrderAmount Paid    OrderDate
100         Y       2016-08-10 21:15:52.000
120         N       2016-08-10 20:15:12.000
300         Y       2016-08-09 11:10:22.000
500         Y       2016-06-09 10:10:54.000
125         N       2016-06-09 09:15:13.000 
325         N       2015-11-09 09:15:13.000

目前,我有以下 SQL 查询:

SELECT DATEPART(Year, OrderDate) TheYear, DATEPART(Month, OrderDate) TheMonth,
       SUM(ISNULL(OrderAmount, 0)) as SumOrderAmount
FROM MyOrderTable
WHERE YEAR(OrderDate) = 2016
GROUP BY DATEPART(Year, OrderDate), DATEPART(Month, OrderDate)
ORDER BY TheYear, TheMonth

上述SQL返回的结果如下:

TheYear     TheMonth    SumOrderAmount
2016        6           625
2016        8           520

我怎样才能查询 SumOrderAmount 行是 Paid='Y' 的地方?我想从 SQL 中返回以下内容:

TheYear     TheMonth    SumOrderAmount  PaidSumOrderAmount
2016        6           625             500
2016        8           520             400

我该怎么做?任何帮助是极大的赞赏。谢谢。

【问题讨论】:

    标签: sql sql-server sql-server-2008


    【解决方案1】:

    使用case 表达式来执行条件SUM()

    SELECT DATEPART(Year, OrderDate) TheYear, DATEPART(Month, OrderDate) TheMonth,
           SUM(ISNULL(OrderAmount, 0)) as SumOrderAmount,
           SUM(case when Paid='Y' then OrderAmount end)
    FROM MyOrderTable
    WHERE YEAR(OrderDate) = 2016
    GROUP BY DATEPART(Year, OrderDate), DATEPART(Month, OrderDate)
    ORDER BY TheYear, TheMonth
    

    【讨论】:

      【解决方案2】:

      使用条件聚合:

      SELECT DATEPART(Year, OrderDate) TheYear, DATEPART(Month, OrderDate) TheMonth,
             SUM(ISNULL(OrderAmount, 0)) as SumOrderAmount,
             SUM(CASE WHEN Paid = 'Y' THEN OrderAmount ELSE 0 END) as PaidOrderAmount
      FROM MyOrderTable
      WHERE YEAR(OrderDate) = 2016
      GROUP BY DATEPART(Year, OrderDate), DATEPART(Month, OrderDate)
      ORDER BY TheYear, TheMonth;
      

      您可能不需要SUM() 中的ISNULL()。在 SQL 中,NULL 值在 SUM() 聚合函数中被忽略。

      【讨论】:

      • 这么说之后,他也不需要ELSE 0 :)
      • @sagi 。 . .相反。当没有匹配项时,需要防止NULL 值,因为假设一个月没有支付金额是合理的。另一方面,如果一个月的所有支付金额都是NULL,那么总金额也需要它。鉴于提供的数据,这似乎不太可能。
      • 如果 OP 有几个月没有付费销售,这可能是比 NULL 值而不是 0 更大的问题。
      • 正是我想要的。非常感谢您的帮助。无论如何,为什么我的问题在那里得到-1?
      【解决方案3】:

      在 select 上添加这个子句:

      SUM(CASE WHEN Paid='Y' THEN OrderAmount ELSE 0 END) AS PaidSumOrderAmount
      

      【讨论】:

        【解决方案4】:
        SELECT DATEPART(Year, OrderDate) TheYear, DATEPART(Month, OrderDate) TheMonth,
               SUM(ISNULL(OrderAmount, 0)) as SumOrderAmount,Paid
        FROM MyOrderTable
        WHERE (YEAR(OrderDate) = 2016) and (Paid = 'Y')
        GROUP BY DATEPART(Year, OrderDate), DATEPART(Month, OrderDate)
        ORDER BY TheYear, TheMonth
        

        【讨论】:

        • 请注意,OP 希望像以前一样保留 SumOrderAmount 列。
        • @jarlh 对不起,我不太懂英语
        • @jarlh 你的回答和 Gordon Linoff 是一样的
        • 我也注意到了,但这次我打字快了一点!
        • @jarlh 对不起兄弟,这件事发生在我身上很多次。我很生气,因为我的母语,有些人不尊重我,给我带来了麻烦,我没有任何朋友和不要和一个人说话。
        猜你喜欢
        • 2013-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-09
        • 2012-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多