Subtract months from current date sql

北城以北 提交于 2021-02-17 05:22:05

问题


I am trying to substract dates from today (Get a 1 month ago till forever report). So far I've tried

DATE_SUB(NOW(), INTERVAL 1 MONTH) 

Here is the context:

SELECT contracts.currency , ROUND(SUM( 
    CASE
        WHEN contracts.currency = 'USD' THEN contracts.value*550
        WHEN contracts.currency = 'UF' THEN contracts.value*22000
        ELSE contracts.value
    END),2)
    AS real_value
FROM contracts
WHERE currency IN ('USD','UF','CLP') AND date >=DATE_SUB(NOW(), INTERVAL 1 MONTH)
GROUP BY currency 
ORDER BY currency ASC

回答1:


See if this helps :

SELECT contracts.currency , ROUND(SUM( 
CASE contracts.currency
    WHEN 'USD' THEN contracts.value*550
    WHEN 'UF'  THEN contracts.value*22000
    ELSE contracts.value
END),2)
AS real_value
FROM contracts
WHERE currency IN ('USD','UF','CLP') AND 
      date >=DATE_SUB(curdate(), INTERVAL 1 MONTH) AND
      date <=curdate()
GROUP BY currency 
ORDER BY currency ASC

If not, it would be nice to check the type of the column "date" in table. Sometimes it is varchar instead of date. This is in case you are not the one who has created the table.



来源:https://stackoverflow.com/questions/23620109/subtract-months-from-current-date-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!