1

我开发了一个查询

select accountname count(transactions)
from table1 group by accountname
where date between '27-mar-2012' and '27-jan-2013'

但它给出了所有交易而不考虑和运算符之间,即它正在考虑从一月到十二月的所有值。我的表包含日期列值,例如 27-mar-2012 ...

如果我使用

select accountname count(transactions) from table1
group by accountname
where date between cast('27-mar-2012' as date) and cast( '27-jan-2013' as date)

我得到错误的结果。

我该如何解决?

4

1 回答 1

1

一种可能性是 Oracle 正在运行查询:

select accountname, count(transactions)
from table1
group by accountname

因为该where子句在group by.

我会建议:

select accountname, count(transactions)
from table1
where date between to_date('2012-03-27', 'yyyy-mm-dd') and to_date('2013-01-27', 'yyyy-mm-dd')
group by accountname

to_date()比 更安全convert()。使用 ANSI 标准日期格式是个好主意(恕我直言)。

于 2013-04-24T21:54:09.443 回答