如何使用SQL和Month函数计算每月订单?

时间:2019-04-24 00:00:08

标签: sql asp.net

我需要计算一年中处理了多少订单,但是我有一个名为datePrepped的字段,其中我有日期,以便我使用功能Month()来获取我所有月份都需要的月份。一年中的几个月

我不想分组我需要将输出存储在变量中,这就是为什么我使用AS Jan AS Feb等的原因。但是,我收到此错误[Microsoft] [ODBC Microsoft Access Driver]参数太少。预期为1。

    sqlString = "SELECT " & _
   "SUM(IIf(Month(r.datePrpped)='1', 1, 0)) AS Jan," & _
   "SUM(IIf(Month(r.datePrpped)='2', 0)) AS Feb," & _
   "SUM(IIf(Month(r.datePrpped)='3', 0)) AS Mar," & _
   "SUM(IIf(Month(r.datePrpped)='4', 0)) AS Apr," & _
   "SUM(IIf(Month(r.datePrpped)='5', 0)) AS May " & _
   "FROM OrderControl AS r;"

需要的输出示例

   <%=MyRecordset("Jan")%> HTML output 500
    <%=MyRecordset("Feb")%> HTML output 800

1 个答案:

答案 0 :(得分:0)

大多数'1'表达式中都缺少iif()

SELECT SUM(IIf(Month(r.datePrpped) = 1, 1, 0)) AS Jan,
       SUM(IIf(Month(r.datePrpped) = 2, 1, 0)) AS Feb,
       SUM(IIf(Month(r.datePrpped) = 3, 1, 0)) AS Mar,
       SUM(IIf(Month(r.datePrpped) = 4, 1, 0)) AS Apr,
       SUM(IIf(Month(r.datePrpped) = 5, 1, 0)) AS May
FROM OrderControl AS r;

MONTH()函数返回一个数字,因此比较值应该是数字,而不是字符串。