SQL查询访问-显示记录到9月底

时间:2018-10-10 04:50:33

标签: sql ms-access

我不确定如何显示直到9月底的记录。

到目前为止,这是我的代码,似乎由于错误“条件表达式中的数据类型不匹配”而失败。

SELECT Month(InvoiceDate) AS IncomeMonth, Sum(TotalPrice) AS Income
FROM INVOICE
WHERE InvoiceDate <= '30/9/2018'
GROUP BY Month(InvoiceDate);

样本数据:

InvoiceDate | TotalPrice
  05/11/18  |    $200
  05/04/18  |    $100
  05/05/18  |    $150
  05/03/18  |    $120
  05/07/18  |    $100

预期的输出查询结果:

IncomeMonth | Income
         4  |    $100 
         6  |    $150
         3  |    $120
         7  |    $100
         3  |    $120
         7  |    $100

3 个答案:

答案 0 :(得分:1)

最好使用年,月,日格式作为日期

SELECT Month(InvoiceDate) AS IncomeMonth, Sum(TotalPrice) AS Income
FROM INVOICE
WHERE InvoiceDate <= #2018/09/30#
GROUP BY Month(InvoiceDate);

答案 1 :(得分:0)

如果您有9月的状况月,也许可以使用stortime格式

答案 2 :(得分:0)

由于您可能希望在下个月运行类似的查询,因此使其动态化:

SELECT 
    Month(InvoiceDate) AS IncomeMonth, Sum(TotalPrice) AS Income
FROM 
    INVOICE
WHERE 
    InvoiceDate < DateSerial(Year(Date()), Month(Date()), 1)
GROUP BY 
    Month(InvoiceDate);