[英]I want to achieve following results in SQL using just select statement (no package or function). How can I do it?
I want to achieve following results in SQL using just select statement (no package or function).我想仅使用 select 语句(无包或函数)在 SQL 中实现以下结果。 How can I do it?
我该怎么做? C is a calculation column ie C=A*B and it is dependent on A(where type = X)
C 是一个计算列,即 C=A*B 并且它依赖于 A(其中类型 = X)
OP Typ A B C
10 X 0.5 2 1
10 X 2 4 8
10 X 1.5 2 3
10 Y 12 2 24 <----If Typ = Y then A = (1+8+3)
20 X 2 3 6
20 X 3 1.5 4.5
20 Y 46.6 1 46.5 <----If Typ = Y then A = (1+8+3+24+6+4.5)
30 X 1 1 1
30 X 2 1.5 3
30 Y 96 0.5 48 <----If Typ = Y then A = (1+8+3+24+6+4.5+46.5+1+3)
If Typ = X then A will be from database
Data is sorted by Op,Type ascending
You need SUM() OVER ()
Analytic function with conditional for the column A, assuming you have an ID column(Opr_seq) mentioned as operation sequence :您需要
SUM() OVER ()
带有条件的 A 列分析函数,假设您有一个 ID 列(Opr_seq) 作为操作序列提及:
SELECT Op, Typ,
CASE WHEN Typ='Y'
THEN
SUM(C) OVER (ORDER BY Opr_seq) - C
ELSE
A
END AS A,
B, C
FROM t
ORDER BY Opr_seq
SQL statements where rows reference rows, which reference rows, can be solved with either a recursive common table expression, a CONNECT BY
statement, or a MODEL
expression.行引用行的 SQL 语句可以用递归公用表表达式、
CONNECT BY
语句或MODEL
表达式解决。 A CONNECT BY
solution would probably be smaller, and a MODEL
solution would probably be faster, but both would also probably be even more cryptic, so below is a solution using recursive common table expressions. CONNECT BY
解决方案可能会更小,而MODEL
解决方案可能会更快,但两者也可能更加神秘,因此下面是使用递归公用表表达式的解决方案。
with results(op, typ, a, b, c, runningTotal, rownumber) as
(
select op, typ, a, b, c,
case when typ = 'X' then c else 0 end runningTotal,
rownumber
from
(
select op, typ, a, b,
case when typ = 'X' then a*b else null end c,
row_number() over (order by op, typ) rownumber
from sample_data
)
where rownumber = 1
union all
select
next_row.op,
next_row.typ,
case when next_row.typ = 'Y' then runningTotal else next_row.a end a,
next_row.b,
case when next_row.typ = 'Y' then runningTotal else next_row.a end * next_row.b c,
runningTotal + case when next_row.typ = 'Y' then runningTotal else next_row.a end * next_row.b runningTotal,
next_row.rownumber
from results
join
(
select op, typ, a, b,
case when typ = 'X' then a*b else null end c,
row_number() over (order by op, typ) rownumber
from sample_data
) next_row
on results.rownumber+1 = next_row.rownumber
)
select op, typ, A, B, C from results;
I used t his sample table:我使用了他的示例表:
-- drop table sample_data;
create table sample_data as
select 10 op, 'X' Typ, 0.5 A, 2 B from dual union all
select 10 op, 'X' Typ, 2 A, 4 B from dual union all
select 10 op, 'X' Typ, 1.5 A, 2 B from dual union all
select 10 op, 'Y' Typ, null A, 2 B from dual union all
select 20 op, 'X' Typ, 2 A, 3 B from dual union all
select 20 op, 'X' Typ, 3 A, 1.5 B from dual union all
select 20 op, 'Y' Typ, null A, 1 B from dual union all
select 30 op, 'X' Typ, 1 A, 1 B from dual union all
select 30 op, 'X' Typ, 2 A, 1.5 B from dual union all
select 30 op, 'Y' Typ, null A, 0.5 B from dual;
My results are slightly different than yours, but I think that's only because your sample has a few minor addition errors.我的结果与你的略有不同,但我认为这只是因为你的样本有一些小的加法错误。 Here's a SQL Fiddle .
这是一个SQL Fiddle 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.