比较日期,同一年或明年的月份摘录

时间:2018-01-04 19:52:26

标签: sql ms-access

此SQL语句在去年正常运行,选择当前月份大于Subquery Max个月匹配ciient_id的数据。现在,Subquery返回Max月份是去年12,是否比较今年的当前月份是1.这就是为什么SQL语句返回0记录。

我必须找出client_id,即当前月份Subquery中不存在的SELECT c.id, p.pkg_rate AS amount FROM tbl_client AS c INNER JOIN tbl_package AS p ON c.pkg_id = p.id WHERE c.status=1 AND Month(Date())>(SELECT Month(Max([due_month])) FROM tbl_payment_due WHERE c.id=client_id); 。要么我可以与日期进行比较。请帮我离开这里。

/ROOT/Customer/

-- LastName
-- FirstName
-- SSN

/ROOT/Customer/Order
-- OrderID

/Root/Customer/Order/Details
-- Item ID
-- Item QTY

1 个答案:

答案 0 :(得分:1)

一种方法是首先将日期格式化为字符串。

Format(Date(),"yyyymm") > (SELECT Format(Max([due_month]),"yyyymm") FROM tbl_payment_due WHERE c.id=client_id)

第二个选项是在WHERE子句中添加年份检查(注意:我的括号可能稍微偏离)。

(Year(Now()) > (SELECT Year(Max([due_month])) FROM tbl_payment_due WHERE c.id=client_id)) OR 
((Year(Now()) = (SELECT Year(Max([due_month])) FROM tbl_payment_due WHERE c.id=client_id)) AND (Month(Date())>(SELECT Month(Max([due_month])) FROM tbl_payment_due WHERE c.id=client_id)))