SQL查询选择两个日期字段之间的数据
我正在尝试使用两个日期字段(开始日期和结束日期)从表中选择数据。 where 子句中的两个日期字段都没有返回结果,而取出 startdate 字段则返回结果,这是怎么回事?任何人都可以帮助解决这个困境
我的代码是:
SELECT WPP.USERID,
WPI.EMAIL,
WPI.FIRSTNAME,
WPI.LASTNAME,
WPI.INSTITUTION
FROM WEBPROGRAMPARTICIPANTS WPP
JOIN WEBPERSONALINFO WPI ON WPP.USERID = WPI.USERID
JOIN WEBPROGRAMS WP ON WPP.PROGRAMCODE = WP.PROGRAMCODE
WHERE CONFIRMED = 1
AND WP.PROGRAMTYPE IN ('1')
AND WP.PROGRAMSTARTDATE >= '2009-01-02'
AND WP.PROGRAMENDDATE < '2009-09-15'
GROUP BY WPP.USERID,
WPI.EMAIL,
WPI.FIRSTNAME,
WPI.LASTNAME,
WPI.INSTITUTION
HAVING COUNT(WPP.PROGRAMCODE) > 1
ORDER BY WPP.USERID,
WPI.EMAIL
编辑:
这是一个要查看的结果集
USERID PROGRAMSTARTDATE PROGRAMENDDATE
------ ----------------------- -----------------------
26167 2009-03-09 00:00:00.000 2009-06-12 00:00:00.000
26362 2009-03-09 00:00:00.000 2009-06-12 00:00:00.000
26411 2009-03-09 00:00:00.000 2009-06-12 00:00:00.000
26491 2009-03-09 00:00:00.000 2009-06-12 00:00:00.000
I am trying to select data from a table, using two date fields (startdate and enddate). With both date fields in the where clause no results are returned, with the startdate field taken out results are returned, what is going on? Can anyone help with this dilemna
My code is:
SELECT WPP.USERID,
WPI.EMAIL,
WPI.FIRSTNAME,
WPI.LASTNAME,
WPI.INSTITUTION
FROM WEBPROGRAMPARTICIPANTS WPP
JOIN WEBPERSONALINFO WPI ON WPP.USERID = WPI.USERID
JOIN WEBPROGRAMS WP ON WPP.PROGRAMCODE = WP.PROGRAMCODE
WHERE CONFIRMED = 1
AND WP.PROGRAMTYPE IN ('1')
AND WP.PROGRAMSTARTDATE >= '2009-01-02'
AND WP.PROGRAMENDDATE < '2009-09-15'
GROUP BY WPP.USERID,
WPI.EMAIL,
WPI.FIRSTNAME,
WPI.LASTNAME,
WPI.INSTITUTION
HAVING COUNT(WPP.PROGRAMCODE) > 1
ORDER BY WPP.USERID,
WPI.EMAIL
EDIT:
here is a result set to look at
USERID PROGRAMSTARTDATE PROGRAMENDDATE
------ ----------------------- -----------------------
26167 2009-03-09 00:00:00.000 2009-06-12 00:00:00.000
26362 2009-03-09 00:00:00.000 2009-06-12 00:00:00.000
26411 2009-03-09 00:00:00.000 2009-06-12 00:00:00.000
26491 2009-03-09 00:00:00.000 2009-06-12 00:00:00.000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为您的数据库中没有
CONFIRMED = 1 AND WP.PROGRAMTYPE IN ('1') WP.PROGRAMSTARTDATE >= '2009-01-02'
的记录?编辑:正如 @David Andres 指出的“
COUNT(WPP.PROGRAMCODE) > 1" 子句看起来像是罪魁祸首的候选者。
Because you have no records in your DB with
CONFIRMED = 1 AND WP.PROGRAMTYPE IN ('1') WP.PROGRAMSTARTDATE >= '2009-01-02'
??EDIT: As @David Andres pointed out The "
COUNT(WPP.PROGRAMCODE) > 1
" clause looks like a candidate for the culprit.嗯,说实话,我觉得还不错。我怀疑having语句过滤了你的结果,或者where和having语句的组合。
Well, to be honest, it looks fine to me. I suspect the having statement filtering your results, or a combo of the where and having statement.
先生们,非常感谢你们的帮助。通过分析您所说的所有内容并尝试不同的建议,我实际上发现代码是正确的。然而,在该时间范围内,用户没有参加超过一个节目。如果我再回去一年,预期的数据就会出现。所以基本上这只是用户对程序的逻辑参与因素的简单问题。
再次感谢大家。
Gentlemen, thank you very much for all your help. Through analysis of all what you have said and trying the different suggestions out, I have actually found out that the code is right. However, during that time frame, users did not attend more than one program during that time frame. If I go back just one more year the expected data comes up. So basically it was just a simple matter of logical attendence factoring of the user to the programs.
Thank you all again.
看来您的答案是零,例如“在该日期范围内有零个用户参与了多个计划”,
您可以做的就是稍微修改您的查询以显示每个用户参与了多少个计划来确认这一点。 参与:
这应该为您列出该日期范围内的每个用户和项目数量,首先按数量排序,然后按您最初设置的条件排序。它删除了having子句,这似乎是你痛苦的原因。
It looks like your answer is zero, as in "zero users participated in more than one program during that date range"
What you can do to confirm this is modify your query slightly to show how many programs each user did participate in:
This should list for you every user and the count of projects for that date range, ordered first by the count, then by the criteria you originally set up. It removes the having clause, which seems to be the cause of your pain.