How to run a query between dates and times?

ε祈祈猫儿з 提交于 2019-12-24 00:52:18

问题


I have a query where I need to pull information from 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

回答1:


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


来源:https://stackoverflow.com/questions/5917103/how-to-run-a-query-between-dates-and-times

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!