andylin 发表于 2013-5-30 08:48:41

求SQL查询语句

求SQL查询语句:
表A
月份科目   本月数   
1   6001      10      
1   6002      15
1   6003      20
2   6001       6      
2   6002       5
3   6002      10
3   6003       8
要求显示结果
月份   科目   本月数    累计数
1   6001      10         10      
1   6002      15         15
1   6003      20         20
2   6001       6         16      
2   6002       5         20
2   6003       0         20
3   6001       0         16      
3   6002       5         25
3   6003       8         28

tyzk 发表于 2013-5-30 09:02:51

我只是路过打酱油的。

790496690 发表于 2013-5-30 09:17:13

找百度啊www.whgongsizhuce.com

颂歌 发表于 2013-6-4 07:22:34

你那累计数是怎么出来的?

乱闯 发表于 2013-6-4 19:03:04

求SQL查询语句:
表A
月份科目   本月数   
1   6001      10      
1   6002      15
1   6003      20
2   6001       6      
2   6002       5
3   6002      10
3   6003       8
要求显示结果

我先给字段取个名

month objectcou   num
月份   科目   本月数    累计数
1   6001      10         10      
1   6002      15         15
1   6003      20         20
2   6001       6         16      
2   6002       5         20
2   6003       0         20
3   6001       0         16      
3   6002       5         25
3   6003       8         28

那么正确的语句是

select month,object,cou,count(cou) '累计数' from 表名;

leewon 发表于 2013-8-16 16:48:19

oracle 好像有啥啥rank的包,不知道能实现不,但是可以用存储过程实现楼主需求。以下是本人写的匿名存储过程。

create table tmp_test
(   monvarchar2(30),
    item varchar2(30),
    cnt varchar2(30)
);
select * from tmp_test;
--------------------------------------
1        6001        1
1        6002        2
1        6003        3
2        6001        1
2        6002        2
2        6003        3
3        6001        1
----------------------------------------------

declare
v_acc number(10);
cursor c1 is
    select * from tmp_test order by mon, item;
begin
for rec in c1 loop
    select sum(cnt)
      into v_acc
      from tmp_test
   where mon <= rec.mon
       and item = rec.item;
    DBMS_OUTPUT.put_line(rec.mon ||',' ||rec.item || ',' ||rec.cnt ||',' ||v_acc);
end loop;
end;

leewon 发表于 2013-8-16 16:49:45

忘记贴出最后打印结果了
1,6001,1,1
1,6002,2,2
1,6003,3,3
2,6001,1,2
2,6002,2,4
2,6003,3,6
3,6001,1,3

qzluotaotao 发表于 2013-8-22 00:18:07

我想问你的是你的累计数是怎么算出来的哦

独自面对℡ 发表于 2013-9-22 09:32:42

围观中:lol::lol::lol:

www5956 发表于 2014-5-16 20:55:27

你这个显示的结果都有问题。

joaboo 发表于 2014-6-1 14:31:36

这么简单的题都没人答出来啊.....


以mysql为例:
(沿用上面仁兄的建表语句)
create table tmp_test
(   monint(2),
    item varchar(10),
    num int(11)
)

insert into tmp_test
select 1, '6001', 10 from dual union all
select 1, '6002', 15 from dual union all
select 1, '6003', 20 from dual union all
select 2, '6001', 6from dual union all
select 2, '6002', 5from dual union all
select 3, '6002', 10 from dual union all
select 3, '6003', 8from dual

-- 思路就是先补全月份,再按item的分组进行累加.
select mon, item, num, sum from (
        select mon, item, num, if(@i = item, @x := @x + num, @x := num) as sum, @i := item
        from (select @x := 0) x, (select @i := 0) i, (
                select b.mon, b.item, ifnull(a.num, 0) num
                from tmp_test a
                right join (
                        select mon, item from (select distinct mon from tmp_test) a, (select distinct item from tmp_test) b
                ) b on a.mon = b.mon and a.item = b.item
                order by b.item, b.mon
        ) t
) tt order by mon, item

如果是oracle就更简单啦..(当然还是得先补全月份,就省略了...)
select mon, item, num, sum(num) over(partition by item order by mon) from t


页: [1]
查看完整版本: 求SQL查询语句