如何写这样的SQL语句?

[复制链接]
查看11 | 回复9 | 2005-2-28 12:57:00 | 显示全部楼层 |阅读模式
已有数据:
==========
会计期 物料分类 期初 本期入 本期出 期末
200901C110129
200901C210138
200902C19 0 27
200902C28 1 27

查询 200801-200912 的数据,查询结果:
==========
开始会计期 结束会计期 物料分类 期初 本期入 本期出 期末
200901 200902 C1101
4 7
200901 200902 C2102
5 7
注意:
期初,取查询范围内存在的最小会计期的期初
期末,取查询范围内存在的最大会计期的期末
本期入和出,求和
求助!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
[ 本帖最后由 WilliamGui 于 2009-4-17 11:06 编辑 ]
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
关注。。。
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
用自连接
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
非常笨的一个方法,不知是不是这个意思:
SQL> with tmp as (
2select '200901' dat, 'C1' typ,10 st,1 inc, 2 exp, 9 remain from dual
3union
4select '200901' dat,'C2' typ,10 st,1 inc, 3 exp, 8 remain from dual
5union
6select '200902' dat, 'C1' typ,9 st,0 inc, 2 exp, 7 remain from dual
7union
8select '200902' dat,'C2' typ,8 st,1 inc, 2 exp, 7 remain from dual
9union
10select '200903' dat, 'C1' typ,7 st,3 inc, 1 exp, 9 remain from dual
11union
12select '200903' dat,'C2' typ,7 st,2 inc, 2 exp, 7 remain from dual)
13select t1.dat, t2.dat, t1.st, t3.inc, t3.exp, t2.remain
14from (select dat, typ, st from tmp where dat = '&from_c') t1,
15 (select dat, typ, remain from tmp where dat = '&to_c') t2,
16 (select typ, sum(inc) inc, sum(exp) exp
17
from tmp
18 where to_date(dat, 'yyyymm') between to_date('&from_c', 'yyyymm') and
19
to_date('&to_c', 'yyyymm')
20 group by typ) t3
21 where t1.typ = t2.typ
22 and t1.typ = t3.typ
23;
DATDAT
STINCEXP REMAIN
------ ------ ---------- ---------- ---------- ----------
200901 200903 10
4
5
9
200901 200903 10
4
7
7
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
输入开始月:from_c YYYYMM
输入结束月:to_c YYYYMM
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层

以后提问最好是附上相关的建表和原始数据的脚本!
具体如下:
SQL> selectkjq 会计期,
2
wlfl 物料分类,
3
qc 期初,
4
bql本期入,
5
bqc本期出,
6
qm 期末
7from tmp;
会计期 物料分类 期初 本期入 本期出 期末
------ -------- ---------- ---------- ---------- ----------
200901 C1
10
1
2
9
200901 C2
10
1
3
8
200902 C1
9
0
2
7
200902 C2
8
1
2
7
SQL>
SQL> select min(kjq) 开始会计期,
2 max(kjq) 结束会计期,
3 wlfl 物料分类,
4 min(qc) keep(dense_rank first order by kjq) 期初,
5 sum(bql) 本期入,
6 sum(bqc) 本期出,
7 max(qm) keep(dense_rank last order by kjq) 期末
8from tmp
9group by wlfl;
开始会计期 结束会计期 物料分类 期初 本期入 本期出 期末
---------- ---------- -------- ---------- ---------- ---------- ----------
200901 200902 C1
10
1
4
7
200901 200902 C2
10
2
5
7
SQL>

[ 本帖最后由 bell6248 于 2009-4-17 12:28 编辑 ]
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
我也来献丑一把
SQL> select * from a;
A1 A2 A3 A4 A5 A6
---------- ---------- ---------- ---------- ---------- ----------
200901 C1 10 1
2
9
200901 C2 10 1
3
8
200902 C1 9
0
2
7
200902 C2 8
1
2
7
SQL> select a.a1,b.a1,a.a2,
2decode(sign(a.a1-b.a1),-1,a.a3,b.a3)a3,
3a.a4+b.a4 a4,
4a.a5+b.a5 a5,
5decode(sign(a.a1-b.a1),1,a.a6,b.a6)a6
6from (select * from a where a1=200901)a,
7(select * from a where a1=200902)b
8where a.a2=b.a2
9/
A1
A1 A2 A3
A4 A5 A6
---------- ---------- ---------- ---------- ---------- ---------- ----------
200901 200902 C1 10
1
4 7
200901 200902 C2 10
2
5 7
SQL>
[ 本帖最后由 guolin-cai 于 2009-4-17 13:59 编辑 ]
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层



学习了
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
功能满足,性能不好说了
select a.curmonth,

b.curmonth,

a.type,

a.begin,

a.curin + b.curin,

a.curout + b.curout,

b.end
from test a, test b
where a.type = b.type
anda.curmonth = ( select min(c.curmonth)

from test c)
andb.curmonth = ( select min(d.curmonth)

from test d)
回复

使用道具 举报

千问 | 2005-2-28 12:57:00 | 显示全部楼层
建表脚本 还有测试数据.........
么数据 怎么成 呵呵
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行