【发布时间】:2013-11-20 16:30:52
【问题描述】:
我对 SQL 不是特别熟悉,但是我的团队让我看一下这一系列的 sql 语句,看看是否有可能将其减少到只有 1 或 2。我看了一下,我不信,不过我对sql的技巧还不是很了解。
所以所有的语句都有几乎相同的格式
select
id, count(*)
from
table
where
x_date between to_date(start_date) and to_date(end_date)
group by
id
x_date 是唯一改变的地方。 (Start_date 和 end_date 正是我在这里输入的内容,以使其更具可读性)。总共有 10 个语句,其中 7 个正是这种格式。
在 3 个不同的部分中,其中一个看起来像这样:
select
id, count(*)
from
table
where
x_date between to_date(start_date) and to_date(end_date)
and userid not like 'AUTOPEND'
group by
id
另外两个看起来像这样:
select
id, count(*)
from
table
where
x_date between to_date(start_date) and to. _date(end_date)
group by
id, x_code
x_code 的不同之处。
他们想使用这些数据进行统计分析,但他们坚持手动使用查询并输入它。我看到的方式是我无法真正组合这些语句,因为它们都是按同一字段分组的(除了最后 2 个),所以它们都被合并到结果中,使得结果对分析毫无用处。
我的想法是否正确,或者有什么方法可以按照他们的要求做吗?我可以让 1 或 2 条 sql 语句分别输出超过 1 个表吗?
哦,我差点忘了。我相信这是使用 SQL 开发人员的 Oracle PL/SQL。
【问题讨论】: