请求高人指点!帮忙解决sql语句问题

crazycaishen 2004-08-26 05:08:55
以下是我做一个数据窗口的后台sql语句,但结果却无限循环地重复显示,在前面添加unique或distinct确像死锁一样,想不明白。请求高人指点!先谢过了
SELECT ab07.AAB001,
AB07.AAB019,
AB07.AAB020,
AB07.AAB137,
to_number(0000) as year,

Ab01_t.AAB004,a.jan,b.feb,c.mar,d.apr,e.may,f.jun,g.jul,
h.aug,i.sep,j.oct,k.nov,l.dec

FROM (select aab001,aae002,aab137 jan from ab07 where substr(aae002,5,2)='01') a,
(select aab001,aae002,aab137 feb from ab07 where substr(aae002,5,2)='02') b,
(select aab001,aae002,aab137 mar from ab07 where substr(aae002,5,2)='03')c,
(select aab001,aae002,aab137 apr from ab07 where substr(aae002,5,2)='04') d,
(select aab001,aae002,aab137 may from ab07 where substr(aae002,5,2)='05') e,
(select aab001,aae002,aab137 jun from ab07 where substr(aae002,5,2)='06') f,
(select aab001,aae002,aab137 jul from ab07 where substr(aae002,5,2)='07') g,
(select aab001,aae002,aab137 aug from ab07 where substr(aae002,5,2)='08') h,
(select aab001,aae002,aab137 sep from ab07 where substr(aae002,5,2)='09') i,
(select aab001,aae002,aab137 oct from ab07 where substr(aae002,5,2)='10') j,
(select aab001,aae002,aab137 nov from ab07 where substr(aae002,5,2)='11') k,
(select aab001,aae002,aab137 dec from ab07 where substr(aae002,5,2)='12') l,
ab07,ab01_t


WHERE
ab01_t.aab001=ab07.aab001 and ab07.aae140=3 and
ab07.aab001=a.aab001 and substr(ab07.aae002,1,4)=substr(a.aae002,1,4) and
a.aab001=b.aab001 and substr(a.aae002,1,4)=substr(b.aae002,1,4) and
b.aab001=c.aab001 and substr(b.aae002,1,4)=substr(c.aae002,1,4) and
c.aab001=d.aab001 and substr(c.aae002,1,4)=substr(d.aae002,1,4)and
d.aab001=e.aab001 and substr(d.aae002,1,4)=substr(e.aae002,1,4) and
e.aab001=f.aab001 and substr(e.aae002,1,4)=substr(f.aae002,1,4) and
f.aab001=g.aab001 and substr(f.aae002,1,4)=substr(g.aae002,1,4)and
g.aab001=h.aab001 and substr(g.aae002,1,4)=substr(h.aae002,1,4) and
h.aab001=i.aab001 and substr(h.aae002,1,4)=substr(i.aae002,1,4) and
i.aab001=j.aab001 and substr(i.aae002,1,4)=substr(j.aae002,1,4) and
j.aab001=k.aab001 and substr(j.aae002,1,4)=substr(k.aae002,1,4)and
k.aab001=l.aab001 and substr(k.aae002,1,4)=substr(l.aae002,1,4)

...全文
148 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
KingSunSha 2004-08-27
  • 打赏
  • 举报
回复
你的sql效率太差,没这种写法。

下面是一个例子,你看懂了问题就解决了。

SQL> create table test
2 (company_id varchar2(30),
3 pay_month varchar2(6),
4 amount float);
Table created.
SQL> insert into test
2 values ('a', '200401', 1000);
1 row created.
SQL> insert into test
2 values ('a', '200402', 800);
1 row created.
SQL> insert into test
2 values ('a', '200403', 1200);
1 row created.

SQL> select company_id, sum (amount) ttl_amount
2 , sum (decode (substr (pay_month, 5, 2), '01', amount, 0)) jan
3 , sum (decode (substr (pay_month, 5, 2), '02', amount, 0)) feb
4 , sum (decode (substr (pay_month, 5, 2), '03', amount, 0)) mar
5 from test
6 where pay_month between '200401' and '200412'
7 group by company_id;

COMPANY_ID TTL_AMOUNT JAN FEB MAR
------------------------------ ---------- ---------- ---------- ----------
a 3000 1000 800 1200
yujiabian 2004-08-27
  • 打赏
  • 举报
回复
还是看看 KingSunSha(弱水三千) 的例子吧
马前小卒 2004-08-27
  • 打赏
  • 举报
回复
同意楼上
有些有条件的统计用decode,可以化简许多
crazycaishen 2004-08-26
  • 打赏
  • 举报
回复

我要做一个报表,详情如下:

现数据库中相关的表里有各单位每个月缴费记录,所含相关字段例如:
单位编号 缴费时间 金额
1000010 200204 1326.5
1000342 200310 2005.0

要求在报表中显示字段如(部分):
****年

单位编号 合计 一月 二月 三月 四月 五月。。。。。。。。十二月

(每个月的缴费金额)

需要显示的是每个月的金额,原表中没有直接的字段,我在数据窗口中添加了13个虚拟列,
liuyi8903 2004-08-26
  • 打赏
  • 举报
回复
不太明白你的意思。

17,140

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧