聚合函数中的子句

时间:2016-05-27 12:01:57

标签: sql

我想从TableTransaction(ID_Cust,CustName,TransValue,TransDate,...)中检索特定时期内的数据

在下面的SQL中,我想添加如下的子句:

Where TransDate>1/1/2016 And TransDate<2/1/2016

代码:

SELECT 
    ID_Cust, CustName, SUM(TransValue) As TotalPurchase
FROM
    TTransaction
GROUP BY
    ID_Cust, CustName
HAVING
    SUM(TransValue) < 25000000
ORDER BY
    TotalPurchase DESC

我尝试过这些尝试,但都没有奏效。

SELECT 
    ID_Cust, CustName, SUM(TransValue) As TotalPurchase
FROM 
    TTransaction
WHERE
    TransDate > 1/1/2016 AND TransDate < 2/1/2016
GROUP BY
    ID_Cust, CustName
HAVING
    SUM(TransValue) < 25000000
ORDER BY
    TotalPurchase DESC

SELECT 
    ID_Cust, CustName, SUM(TransValue) As TotalPurchase
FROM 
    TTransaction
WHERE
    TransDate > 1/1/2016 AND TransDate < 2/1/2016
HAVING
    SUM(TransValue) < 25000000
ORDER BY
    TotalPurchase DESC

1 个答案:

答案 0 :(得分:1)

首先,您需要在日期常量周围使用单引号。其次,使用ISO标准日期格式。所以,试试这个:

Select ID_Cust, CustName, SUM(TransValue) As TotalPurchase
From TTransaction
Where TransDate > '2016-01-01' AND TransDate < '2016-02-01'
Group By ID_Cust, CustName
Having SUM(TransValue)<25000000
Order By TotalPurchase DESC