多个查询日期范围

时间:2015-11-25 13:37:14

标签: sql oracle oracle11g

我每天都运行多个查询,有没有办法为多个选择设置日期范围,而不是在每个select中输入日期。我使用oracle sql developer来运行这些查询。

这里只有三个例子,但有大约十几个查询:

select count(*)"Tall ord"
from cr_ordpar
inner join cr_palhis on cr_palhis.pikref = cr_ordpar.pikref
inner join CR_LODHED_DESP on cr_lodhed_desp.ilodno = cr_ordpar.ILODNO
where cr_palhis.hstdat between '24-nov-15 07:00' and '25-nov-15 07:00:00'
and hststs_str = 'Pallet Output From Racking'
and cr_palhis.palhgt = '2700'
order by cr_lodhed_desp.lodref;

select count(*)"Tall del"
from cr_palhis
where cr_palhis.hstdat between '24-nov-15 07:00' and '25-nov-15 07:00:00'
and hststs_str = 'Pallet Deleted'
and cr_palhis.palhgt = 2700
and cr_palhis.rakblk <> 510; 

select  count(*)"Tall ind"
from cr_palhis
where cr_palhis.hstdat between '24-nov-15 07:00' and '25-nov-15 07:00:00'
and hststs_str = 'Pallet Indexed'
and cr_palhis.PALHGT = '2700';

1 个答案:

答案 0 :(得分:0)

您的DATE存在多个问题。

  • 首先,'24-nov-15 07:00'不是 DATE ,而是字符串。您必须始终使用 TO_DATE 将文字明确转换为DATE。另外,如果您没有提及 NLS_DATE_LANGUAGE ,请记住 TO_DATE 取决于NLS。 NOV与其他语言不同。

  • YY格式很久以前被认定为 Y2K 错误,世界已经看到了摆脱它的努力。要么提到完整的YYYY,要么使用RR格式,但要在实施之前了解它。

TO_DATE('24-nov-2015 07:00','dd-mon-yyyy hh24:mi','nls_date_language=ENGLISH')

您可以使用 GROUP BY 子句在单个查询中获取不同的计数。鉴于所有条件都保持不变。

SELECT hststs_str,
  COUNT(*) cnt
FROM cr_ordpar
INNER JOIN cr_palhis
ON cr_palhis.pikref = cr_ordpar.pikref
INNER JOIN CR_LODHED_DESP
ON cr_lodhed_desp.ilodno = cr_ordpar.ILODNO
WHERE cr_palhis.hstdat 
BETWEEN TO_DATE('24-nov-2015 07:00','dd-mon-yyyy hh24:mi','nls_date_language=ENGLISH') 
AND     TO_DATE('25-nov-2015 07:00','dd-mon-yyyy hh24:mi','nls_date_language=ENGLISH')
AND hststs_str       IN( 'Pallet Output From Racking', 
                         'Pallet Deleted', 
                         'Pallet Indexed'
                       )
AND cr_palhis.palhgt  = '2700'
AND cr_palhis.rakblk <> 510
GROUP BY hststs_str
ORDER BY cnt;