简体   繁体   English

我想仅使用 select 语句(无包或函数)在 SQL 中实现以下结果。 我该怎么做?

[英]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 

Demo 演示

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.

相关问题 如何使用sql语句获得想要的结果? - How can I achieve the result I want using sql statement? 如何使用 SQL select 语句实现此结果 - How can I achieve this result using SQL select statement 如何在mysql中使用JOIN语句实现以下目标? - How do I achieve the following using JOIN statement in mysql? 如何在 select 语句中使用用户定义的函数作为游标来实现结果? - How can I use user defined function in select statement as cursor to achieve the results? 如何使用sql函数或查询达到以下要求 - How will I achieve the following requirement using sql function or query SQL:如何循环SELECT语句的结果? - SQL: How do I loop through the results of a SELECT statement? Clojure.contrib.sql:如何将select语句的结果检索到可以随时访问的向量中? - Clojure.contrib.sql: How do I retrieve the results of a select statement into a vector that can be accessed anytime? 使用SQL FOR XML PATH,如何实现以下格式化XML输出? - Using SQL FOR XML PATH, how can I achieve the following formatted XML output? 当我的列中的字符串不可用时,我想获得 NULL 如何使用 SQL 实现此目的? - I want to get NULL when the string in my column is not available How can I achieve this using SQL? 如何从LINQ实现SQL Pivot语句 - How can I achieve SQL Pivot statement from LINQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM