查询日期范围和具体时间范围?

时间:2011-05-12 22:55:54

标签: sql database db2 ibm-midrange

我有一个DB2 DB的查询,我需要在日期和特定时间之间提取数据。我想出了下面的查询并且它正在运行但是没有获得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

4 个答案:

答案 0 :(得分:0)

看起来你缺少WHERE子句中的一些括号

试试这个: -

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 = 20110511)
    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 

答案 1 :(得分:0)

示例: -

(两个日期之间)

`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 BETWEEN 20110510 AND 20110512)      
    AND A1.CHROUT = 'UPSPA' GROUP BY A1.CHCASN, A1.CHTRKN, A1.CHACWT,
    A1.CHDLM, A1.CHTLM  
ORDER BY A1.CHDLM, A1.CHTLM` 

(同一日期)

`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 = 20110511)     
    AND A1.CHROUT = 'UPSPA' GROUP BY A1.CHCASN, A1.CHTRKN, A1.CHACWT,
    A1.CHDLM, A1.CHTLM  
ORDER BY A1.CHDLM, A1.CHTLM `

答案 2 :(得分:0)

一种解决方案是将日期和时间列组合成时间戳或类似结构,这种结构更易于在多天内进行远程搜索。使用什么数据类型并不重要;只需确保它遵循某种YYYYMMDDHHMMSS格式,并以相同的方式转换输入参数,以便将它们与列数据进行比较。

假设:

  • 表CHCART00中的列CHDLM列为INTEGER,并以YYYYMMDD格式存储日期
  • 表CHCART00中的列CHTLM列为CHAR(6)并以24小时HHMMSS格式存储时间
  • 输入参数指定的窗口是连续的,没有间隙,例如“仅营业时间”

    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'))

答案 3 :(得分:-1)

提取查询的一种简单方法是:

SELECT
  num_executions,
  num_exec_with_metrics,
  SUBSTR(stmt_text,1,2000000) AS stmt_text
FROM TABLE (MON_GET_PKG_CACHE_STMT('d', null, null, -1)) AS tf
WHERE
  (VARCHAR_FORMAT(INSERT_TIMESTAMP,'YYYY-MM-DD HH24:MI:SS') > '2016-01-27 07:00:00'
  AND VARCHAR_FORMAT(INSERT_TIMESTAMP,'YYYY-MM-DD HH24:MI:SS') <= '2016-01-27 08:59:59')
ORDER BY INSERT_TIMESTAMP DESC;