简体   繁体   English

SQL 挑战

[英]An SQL chalenge

how are you today?你今天怎么样?

After burn a few neurons with part of my requirement, I'm forced to ask for your help根据我的要求烧掉几个神经元后,我不得不寻求你的帮助

I would like to solve this with pure SQL sentences in an AMDP method, I could solve this easily in ABAP, even in an AMDP method using loops, but as I said, I would like to solve this with SQL sentences, without use any kind of loops.我想在 AMDP 方法中用纯 SQL 语句解决这个问题,我可以在 ABAP 中轻松解决这个问题,即使在使用循环的 AMDP 方法中,但正如我所说,我想用 SQL 语句解决这个问题,而不使用任何类型的循环。

Please take a look on this image:请看一下这张图片:

Sample样本

I have 2 columns, the first I'll name as D and the second as E我有两列,第一列命名为D ,第二列命名为E

The D column, is a result of a SELECT SUM, but the E colunm, is a calculated column, and it shoud work as follow: D列是 SELECT SUM 的结果,但E列是计算列,它应该按如下方式工作:

Firsrt line are equals in both columns. **E1 = D1**
In the second line, the calculation for the second column is **E1 + D2**
In the third line, the calculation for the second column is **E2 + D3**
In the forth line, the calculation for the second column is **E3 + D4**
And so on.

So thats it.就是这样了。 Is it posible solve this with pure SQL sentences?是否可以用纯 SQL 语句解决这个问题?

Could someone please give me a help?有人可以帮我吗?

Best regards.此致。

Ronaldo S. Vieira罗纳尔多·S·维埃拉

Have the SUM in a subquery, then do a select from that subquery along with a row number.在子查询中使用 SUM,然后从该子查询中选择一个行号。 Insert that into a temporary table.将其插入到临时表中。 Then select from that temporary table and do a join to the same table based on the row number, looking for the previous row.然后从该临时表中选择并根据行号连接到同一个表,查找上一行。

SELECT D, ROW_NUMBER() AS ROW_NUM
INTO #TempTable
FROM (SELECT SUM() AS D FROM table) a

SELECT
    t1.D
    ,CASE
        t2.D IS NULL THEN t1.D
        ELSE t2.D + t1.D AS E
FROM #TempTable t1
    LEFT JOIN #TempTable t2 ON t1.ROW_NUM = t2.ROW_NUM + 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM