查询日期范围和特定时间范围?
我有一个 DB2 数据库查询,我需要在日期和特定时间之间提取数据。我提出了下面的查询,它正在工作,但没有获取 20110510 和 20110512 之间当天的数据,在本例中为 20110511,该数据应该是一整天的数据。
日期和时间是用户的参数,因此会根据他们的需要而改变。如果可能的话,我还希望能够使用相同的查询提取同一天的数据,例如:
Date last modify >= 20110512 AND
Date last modify <= 20110512 AND
Time last modify >= 090000 AND
Time last modify <= 230000 AND
任何帮助都会提前非常感激。谢谢。
SELECT A1.CHCASN, A1.CHTRKN,
SUM(A2.CDPAKU) AS UNITS, A1.CHACWT, SUM(A2.CDPRC * A2.CDPAKU) AS COST, SUM(A3.STRPRC * A2.CDPAKU) AS RETAIL, A1.CHDLM, A1.CHTLM
FROM CHCART00 A1, CDCART00 A2, STSTYL00 A3
WHERE A1.CHCASN = A2.CDCASN
AND A2.CDSTYL = A3.STSTYL
AND A2.CDCOLR = A3.STCOLR
AND A2.CDSDIM = A3.STSDIM
AND A1.CHSTAT = '25'
AND (A1.CHDLM = 20110510 AND A1.CHTLM >= '200000' OR A1.CHDLM = 20110512 AND A1.CHTLM <= '092000') AND A1.CHROUT = 'UPSPA'
GROUP BY A1.CHCASN, A1.CHTRKN, A1.CHACWT, A1.CHDLM, A1.CHTLM
ORDER BY A1.CHDLM, A1.CHTLM
I have this query for a DB2 DB where I need to pull data between dates and specific times. I came up with the query below and it is working but is not getting the data for the day in between 20110510 and 20110512 in this case 20110511, this data should be for the entire day.
The dates and times are user's parameters so this will change depending on their needs. I also want to be able to pull data for the same day with the same query if possible, for example:
Date last modify >= 20110512 AND
Date last modify <= 20110512 AND
Time last modify >= 090000 AND
Time last modify <= 230000 AND
Any help will be really appreciate it in advance. Thank you.
SELECT A1.CHCASN, A1.CHTRKN,
SUM(A2.CDPAKU) AS UNITS, A1.CHACWT, SUM(A2.CDPRC * A2.CDPAKU) AS COST, SUM(A3.STRPRC * A2.CDPAKU) AS RETAIL, A1.CHDLM, A1.CHTLM
FROM CHCART00 A1, CDCART00 A2, STSTYL00 A3
WHERE A1.CHCASN = A2.CDCASN
AND A2.CDSTYL = A3.STSTYL
AND A2.CDCOLR = A3.STCOLR
AND A2.CDSDIM = A3.STSDIM
AND A1.CHSTAT = '25'
AND (A1.CHDLM = 20110510 AND A1.CHTLM >= '200000' OR A1.CHDLM = 20110512 AND A1.CHTLM <= '092000') AND A1.CHROUT = 'UPSPA'
GROUP BY A1.CHCASN, A1.CHTRKN, A1.CHACWT, A1.CHDLM, A1.CHTLM
ORDER BY A1.CHDLM, A1.CHTLM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
示例:-(
两个日期之间)
(同一日期)
Examples:-
(Between two dates)
(Same date)
看起来您的 WHERE 子句中缺少一些括号
试试这个:-
It looks like you are missing some parentheses from your WHERE clause
Try this:-
一种解决方案是将日期和时间列组合成时间戳或类似的结构,这样更容易用于多天的范围搜索。使用什么数据类型并不重要;重要的是。只需确保它遵循某种 YYYYMMDDHHMMSS 格式并以相同的方式转换您的输入参数,以便它们可以与列数据进行比较。
假设:
输入参数指定的窗口是连续的,没有间隙例如“仅限营业时间”
AND (BIGINT(A1.CHDLM) * 1000000 + INTEGER(A1.CHTLM))
>= (BIGINT(20110510) * 1000000 + INTEGER('230000'))
AND (BIGINT(A1.CHDLM) * 1000000 + INTEGER(A1.CHTLM))
<= ( BIGINT( 20110512 ) * 1000000 + INTEGER('090000'))
One solution is to combine the date and time columns into a timestamp or a similar structure that is much easier to use for ranged searches across multiple days. It doesn't really matter what data type is used; just be sure it follows some sort of YYYYMMDDHHMMSS format and convert your input parameter(s) the same way so they can be compared with the column data.
Assumptions:
The window specified by the input parameters is continuous, with no gaps such as "business hours only"
AND ( BIGINT( A1.CHDLM ) * 1000000 + INTEGER( A1.CHTLM ))
>= ( BIGINT( 20110510 ) * 1000000 + INTEGER('230000'))
AND ( BIGINT( A1.CHDLM ) * 1000000 + INTEGER( A1.CHTLM ))
<= ( BIGINT( 20110512 ) * 1000000 + INTEGER('090000'))
拉取查询的一个简单方法是这样的:
A simple way to pull the query is this :