0

我试图TOTAL_PRICE使用以下查询从购物车表中提取。目前它将获取TOTAL_PRICE过去 30 天的数据。但我需要修改查询以获取TOTAL_PRICE当前月份的。即,PURCHASED_DATE应该在当月的 1 到 30/31 之间。

SELECT TOTAL_PRICE
FROM CUST_CART_TABLE
WHERE USER_ID      = '"+userId+"'
AND (PURCHASED_DATE BETWEEN TO_DATE(SYSDATE-30, 'DD-MM-YY') AND TO_DATE(SYSDATE,'DD-MM-YY')
4

5 回答 5

2

您可以使用 TRUNC 和 LAST_DAY:

SELECT TOTAL_PRICE
FROM CUST_CART_TABLE
WHERE USER_ID      = '"+userId+"'
AND (TRUNC(PURCHASED_DATE) BETWEEN TRUNC(SYSDATE, 'MM') AND TRUNC(LAST_DAY(SYSDATE)));
于 2013-10-29T11:13:12.133 回答
2

你可以试试这个——

SELECT TOTAL_PRICE
    FROM CUST_CART_TABLE
    WHERE USER_ID      = '"+userId+"'
AND TRUNC(PURCHASED_DATE,'MONTH') = TRUNC(SYSDATE,'MONTH');

它将返回当前月份的记录。

于 2013-10-29T11:19:17.770 回答
1

请试试:

SELECT TOTAL_PRICE
FROM CUST_CART_TABLE
WHERE USER_ID='"+userId+"' AND 
TRUNC(PURCHASED_DATE) BETWEEN LAST_DAY(ADD_MONTHS(SYSDATE,-1))+1 AND 
                        LAST_DAY(SYSDATE)
于 2013-10-29T11:12:12.500 回答
1
    SELECT TOTAL_PRICE
    FROM CUST_CART_TABLE
    WHERE USER_ID      = '"+userId+"'
    AND (PURCHASED_DATE BETWEEN 
SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) 
AND
 SELECT DATEADD(ms, -3, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) + 1, 0))
于 2013-10-29T11:13:47.947 回答
1

简单地比较月份,即 'mm' 与 'yyyy' 的组合:

SELECT TOTAL_PRICE
FROM CUST_CART_TABLE
WHERE USER_ID = '"+userId+"'
AND TO_CHAR(PURCHASED_DATE,'yyyymm') = TO_CHAR(SYSDATE,'yyyymm');
于 2013-10-29T11:48:14.607 回答