Sql:在MS Query的switch中的datediff

时间:2021-03-23 19:23:59

I am using MS Query in Excel 2010. I have two tables and I am appending them together. colG is then added. ColA has dates and in colG I want to diplay a message about how long ago the date is from the current date. I am having issues using DateDiff within a switch statement. Any ideas what is wrong? Here is what I have so far:

我在Excel 2010中使用MS Query。我有两个表,我将它们一起添加。然后添加colG。 ColA有日期和colG我想要显示一个关于多久以前该日期是从当前日期开始的消息。我在switch语句中使用DateDiff时遇到问题。有什么想法有什么不对?这是我到目前为止:

SELECT `Sheet1$`.colA, `Sheet1$`.colB, 
Switch(

1<DateDiff('m', #colA#, CURRENT_TIMESTAMP) < 2, '1-2 months',

2<DateDiff('m', #colA#, CURRENT_TIMESTAMP) < 4, '2-4 months',

4<DateDiff('m', #colA#, CURRENT_TIMESTAMP)< 6, '4-6 months',

6<DateDiff('m', #colA#, CURRENT_TIMESTAMP)< 9, '6-9 months',

DateDiff('m', #colA#, CURRENT_TIMESTAMP)> 9, '+9 months'


)AS colG


FROM (

SELECT `Sheet1$`.colA, `Sheet1$`.colB
FROM `Sheet1$` `Sheet1$`
UNION ALL
SELECT `Sheet2$`.colC, `Sheet2$`.colD
FROM `Sheet2$` `Sheet2$`
) t;

1 个解决方案

#1


0  

Thanks for your help and advice. Here is the solution that ended up working for me:

感谢您的帮助和建议。这是最终为我工作的解决方案:

SELECT `Sheet1$`.colA, `Sheet1$`.colB, 

Switch (

DateDiff ('m', colA, NOW()) < 3 AND DateDiff ('m', colA, NOW()) > 0 , '1 - 2 months',

DateDiff ('m', colA, NOW()) < 5 AND DateDiff ('m', colA, NOW()) > 1 , '2 - 4 months',

DateDiff ('m', colA, NOW()) < 7 AND DateDiff ('m', colA, NOW()) > 3 , '4 - 6 months',

DateDiff ('m', colA, NOW()) < 10 AND DateDiff ('m', colA, NOW()) > 5 , '6 - 9 months',

DateDiff ('m', colA, NOW()) > 9, '+9 months'

)AS colG 

FROM (

SELECT `Sheet1$`.colA, `Sheet1$`.colB
FROM `Sheet1$` `Sheet1$`
UNION ALL
SELECT `Sheet2$`.colC, `Sheet2$`.colD
FROM `Sheet2$` `Sheet2$`
) t;

#1


0  

Thanks for your help and advice. Here is the solution that ended up working for me:

感谢您的帮助和建议。这是最终为我工作的解决方案:

SELECT `Sheet1$`.colA, `Sheet1$`.colB, 

Switch (

DateDiff ('m', colA, NOW()) < 3 AND DateDiff ('m', colA, NOW()) > 0 , '1 - 2 months',

DateDiff ('m', colA, NOW()) < 5 AND DateDiff ('m', colA, NOW()) > 1 , '2 - 4 months',

DateDiff ('m', colA, NOW()) < 7 AND DateDiff ('m', colA, NOW()) > 3 , '4 - 6 months',

DateDiff ('m', colA, NOW()) < 10 AND DateDiff ('m', colA, NOW()) > 5 , '6 - 9 months',

DateDiff ('m', colA, NOW()) > 9, '+9 months'

)AS colG 

FROM (

SELECT `Sheet1$`.colA, `Sheet1$`.colB
FROM `Sheet1$` `Sheet1$`
UNION ALL
SELECT `Sheet2$`.colC, `Sheet2$`.colD
FROM `Sheet2$` `Sheet2$`
) t;