MySQL using Sum and Case

前端 未结 1 1026
抹茶落季
抹茶落季 2020-12-18 19:37

I\'m trying to create a GridView with ASP.NET connecting to a MySQL database. The data appears like below.

BusinessUnit    OrderDate      Canceled
UnitA             


        
1条回答
  •  隐瞒了意图╮
    2020-12-18 20:17

    The error is caused by the space between function name and parenthesis

    SUM (CASE WHEN ...
       ^^
    

    Read more Function Name Parsing and Resolution

    Try

    SELECT BusinessUnit,
           SUM(CASE WHEN OrderDate = CURDATE() THEN 1 ELSE 0 END) TodaysOrders,
           SUM(CASE WHEN DATE_FORMAT(OrderDate, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') THEN 1 ELSE 0 END) ThisMonthsOrders,
           SUM(CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) THEN 1 ELSE 0 END) ThisYearsOrders
      FROM OrderTable
     WHERE Canceled <> 'Y'
     GROUP BY BusinessUnit
    

    Here is SQLFiddle demo

    0 讨论(0)
提交回复
热议问题