开发者

How to run a query between dates and times?

开发者 https://www.devze.com 2023-03-03 05:37 出处:网络
I have a query where I need to pull information from开发者_JAVA百科 two different dates and times.

I have a query where I need to pull information from开发者_JAVA百科 two different dates and times.

I want to pull everything that was date last modified between yesterday and today and between the time last modified of 18:00:00 hours from yesterday and 13:00:00 hours of today.

How can I accomplish this?

  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.CHROUT = 'UPSCA'
     AND A1.CHDLM BETWEEN 20110505 And 20110506
     AND A1.CHTLM >= '160000'
     AND A1.CHTLM <= '130000'
GROUP BY A1.CHCASN, A1.CHTRKN, A1.CHACWT, A1.CHDLM, A1.CHTLM
ORDER BY A1.CHCASN


Maybe this:

 AND ( A1.CHDLM = 20110505 
       AND A1.CHTLM >= '160000' 
     OR
       A1.CHDLM = 20110506
       AND A1.CHTLM <= '130000'
     )

For more generality (to catch the case when the two dates are not consecutive), it should be:

 AND ( A1.CHDLM = 20110505 
       AND A1.CHTLM >= '160000' 
     OR
       A1.CHDLM BETWEEN 20110505 +1
                    AND 20110506 -1
     OR
       A1.CHDLM = 20110506
       AND A1.CHTLM <= '130000'
     )
0

精彩评论

暂无评论...
验证码 换一张
取 消