Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL:列和_Sql_Select_Aggregate - Fatal编程技术网

SQL:列和

SQL:列和,sql,select,aggregate,Sql,Select,Aggregate,让我们有以下示例表: Person Quantity A 1 B 2 C 3 D 4 E 5 结果应该是: PersonAggregate 1 (0+Quantity of PersonA)=sumA 3 (sumA+Quantity of PersonB)=sumB 6 (sumB+Quantity of PersonC)=sumC 10 (sumC+Quantity of PersonD)=sumD 15

让我们有以下示例表:

Person Quantity
A      1
B      2
C      3
D      4
E      5
结果应该是:

PersonAggregate
1     (0+Quantity of PersonA)=sumA
3     (sumA+Quantity of PersonB)=sumB
6     (sumB+Quantity of PersonC)=sumC
10    (sumC+Quantity of PersonD)=sumD
15    (sumD+Quantity of PersonE)

有可能在onq SQL查询中得到这个结果吗?

这显然会得到单独的和

select person, sum(quantity)
from sample
group by person
order by person
我不认为你想要的效果可以用固定的方式来实现。带有游标的过程语言,如T-SQL或PLSQL,可以很容易地实现这一点


我会编写一个存储过程并调用它。

这显然会得到单独的总和

select person, sum(quantity)
from sample
group by person
order by person
我不认为你想要的效果可以用固定的方式来实现。带有游标的过程语言,如T-SQL或PLSQL,可以很容易地实现这一点


我会编写一个存储过程并调用它。

大多数版本的SQL都支持将累积和作为窗口函数:

select person, sum(quantity) over (order by person) as cumesum
from sample;
也可以使用相关子查询执行此操作:

select s.person,
       (select sum(s2.quantity)
        from samples s2
        where s2.person <= s.person
       ) as cumesum
from sample s;

大多数SQL版本都支持将累计和作为窗口函数:

select person, sum(quantity) over (order by person) as cumesum
from sample;
也可以使用相关子查询执行此操作:

select s.person,
       (select sum(s2.quantity)
        from samples s2
        where s2.person <= s.person
       ) as cumesum
from sample s;
如果示例表中每人有多行,且有多个数量需要求和,则可以使用:

select curr.person, curr.sum_person + case when prev.person <> curr.person
                                           then prev.sum_person
                                           else 0 end as person_sum
  from (select person, sum(quantity) as sum_person
          from sample
         group by person) curr
 cross join (select person, sum(quantity) as sum_person
               from sample
              group by person) prev
 where prev.person =
       (select max(x.person) from sample x where x.person < curr.person)
    or curr.person = (select min(person) from sample)
 group by curr.person
如果示例表中每人只有一行,则可以更简单地使用:

select curr.person, curr.quantity +   case when prev.person <> curr.person
                                           then prev.quantity
                                           else 0 end as person_sum
  from sample curr
 cross join sample prev
 where prev.person =
       (select max(x.person) from sample x where x.person < curr.person)
    or curr.person = (select min(person) from sample)
 group by curr.person
小提琴:

如果样本每人有2+行:

select person,
       sum(quantity) over(order by person rows between 1 preceding and current row) as your_sum
  from sample
 order by person
select person,
       sum(sum_person) over(order by person rows between 1 preceding and current row) as your_sum
  from (select person, sum(quantity) as sum_person
          from sample
         group by person) x
 order by person
Fiddle:

如果样本表中每人有多行,且有多个数量需要求和,则可以使用:

select curr.person, curr.sum_person + case when prev.person <> curr.person
                                           then prev.sum_person
                                           else 0 end as person_sum
  from (select person, sum(quantity) as sum_person
          from sample
         group by person) curr
 cross join (select person, sum(quantity) as sum_person
               from sample
              group by person) prev
 where prev.person =
       (select max(x.person) from sample x where x.person < curr.person)
    or curr.person = (select min(person) from sample)
 group by curr.person
如果示例表中每人只有一行,则可以更简单地使用:

select curr.person, curr.quantity +   case when prev.person <> curr.person
                                           then prev.quantity
                                           else 0 end as person_sum
  from sample curr
 cross join sample prev
 where prev.person =
       (select max(x.person) from sample x where x.person < curr.person)
    or curr.person = (select min(person) from sample)
 group by curr.person
小提琴:

如果样本每人有2+行:

select person,
       sum(quantity) over(order by person rows between 1 preceding and current row) as your_sum
  from sample
 order by person
select person,
       sum(sum_person) over(order by person rows between 1 preceding and current row) as your_sum
  from (select person, sum(quantity) as sum_person
          from sample
         group by person) x
 order by person

Fiddle:

一个人是否有2+行的多个数量需要求和?我这样问是因为,如果不是,您的查询实际上不会涉及求和函数。相反,您只是水平添加两个值。您使用的是哪种DBMS?博士后?Oracle?一个人是否有2行以上的多个数量需要求和?我这样问是因为,如果不是,您的查询实际上不会涉及求和函数。相反,您只是水平添加两个值。您使用的是哪种DBMS?博士后?神谕