楼主: darkstorm

[精华] SQL 大挑战-- 可以放到精华区的几个命题

[复制链接]
论坛徽章:
85
2008新春纪念徽章
日期:2008-02-13 12:43:03双黄蛋
日期:2011-06-17 11:07:502011新春纪念徽章
日期:2011-02-18 11:42:472011新春纪念徽章
日期:2011-01-04 10:24:022010年世界杯参赛球队:荷兰
日期:2010-08-28 00:09:112010年世界杯参赛球队:科特迪瓦
日期:2010-03-02 12:36:542010新春纪念徽章
日期:2010-03-01 11:07:242010新春纪念徽章
日期:2010-03-01 11:07:242010新春纪念徽章
日期:2010-01-04 08:33:082010年世界杯参赛球队:意大利
日期:2009-12-31 14:41:24
61#
发表于 2004-11-29 15:33 | 只看该作者

Re: 这个题目,不就是一个构造么,呵呵

最初由 biti_rainy 发布
[B]SQL> select tt1.tta,tt1.ttb,tt1.ttc,sum(tt2.ttb) - sum(tt2.ttc) youwant
  2  from
  3  (
  4  select t1.aa tta,decode(t2.a,null,0,t2.b) ttb,decode(t2.a,null,0,t2.c) ttc
  5  from
  6  (select ((select min(a) from test) + rownum - 1) aa from all_objects
  7  where rownum <= (select max(a) - min(a) +1 from test)) t1,
  8  test t2
  9  where t1.aa = t2.a(+)
10  ) tt1,
11  (
12  select t1.aa tta,decode(t2.a,null,0,t2.b) ttb,decode(t2.a,null,0,t2.c) ttc
13  from
14  (select ((select min(a) from test) + rownum - 1) aa from all_objects
15  where rownum <= (select max(a) - min(a) +1 from test)) t1,
16  test t2
17  where t1.aa = t2.a(+)
18  ) tt2
19  where tt1.tta >= tt2.tta
20  group by tt1.tta,tt1.ttb,tt1.ttc;
SQL> [/B]

SQL> select * from test;

   A          B          C
---- ---------- ----------
   1      40.00      30.00
   2      30.00      35.00
   3      60.00      25.00
   4      50.00      55.00
   6      30.00      25.00
   6      30.00      25.00
   1      30.00      25.00

7 rows selected

结果:

       TTA        TTB        TTC    YOUWANT
        ---------- ---------- ---------- ----------
         1         30         25         15
         1         40         30         15
         2         30         35         10
         3         60         25         45
         4         50         55         40
         5          0          0         40
         6         30         25        100

7 rows selected
最后一列错了

使用道具 举报

回复
论坛徽章:
1
授权会员
日期:2005-10-30 17:05:33
62#
发表于 2004-12-1 19:50 | 只看该作者
好啊

使用道具 举报

回复
招聘 : 数据库管理员
论坛徽章:
38
ITPUB北京九华山庄2008年会纪念徽章
日期:2008-01-21 16:50:24马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14现任管理团队成员
日期:2012-10-18 17:11:21版主4段
日期:2012-05-15 15:24:112012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:232012新春纪念徽章
日期:2012-02-13 15:09:23
63#
发表于 2004-12-23 17:36 | 只看该作者
biti果然不愧是高手啊,不过如果同一天有多条记录则显示时就存在问题,我的试验数据如下:
SQL> select * from test;

A                       B          C
-------------- ---------- ----------
01-12月-04             50         30
02-12月-04             30         15
05-12月-04             80         90
05-12月-04             10         20
用biti的SQL查询结果如下:
TTA                   TTB        TTC    YOUWANT
-------------- ---------- ---------- ----------
01-12月-04             50         30         20
02-12月-04             30         15         35
03-12月-04              0          0         35
04-12月-04              0          0         35
05-12月-04             10         20         15
05-12月-04             80         90         15
呵呵,所以我小小地修改了一下(加上group by) :)
select tt1.tta,tt1.ttb,tt1.ttc,sum(tt2.ttb) - sum(tt2.ttc) youwant
from
(
select t1.aa tta,sum(decode(t2.a,null,0,t2.b)) ttb,sum(decode(t2.a,null,0,t2.c)) ttc
from
(select ((select min(a) from test) + rownum - 1) aa from all_objects
where rownum <= (select max(a) - min(a) +1 from test)) t1,
test t2
where t1.aa = t2.a(+) group by t1.aa
) tt1,
(
select t1.aa tta,sum(decode(t2.a,null,0,t2.b)) ttb,sum(decode(t2.a,null,0,t2.c)) ttc
from
(select ((select min(a) from test) + rownum - 1) aa from all_objects
where rownum <= (select max(a) - min(a) +1 from test)) t1,
test t2
where t1.aa = t2.a(+) group by t1.aa
) tt2
where tt1.tta >= tt2.tta
group by tt1.tta,tt1.ttb,tt1.ttc;
现在查询结果是:
TTA                   TTB        TTC    YOUWANT
-------------- ---------- ---------- ----------
01-12月-04             50         30         20
02-12月-04             30         15         35
03-12月-04              0          0         35
04-12月-04              0          0         35
05-12月-04             90        110         15

感谢biti的SQL语句,没有你,我可能想破头也想不出这道题的解法啊

使用道具 举报

回复
论坛徽章:
1
2013年新春福章
日期:2013-02-25 14:51:24
64#
发表于 2005-9-14 14:27 | 只看该作者
select mtime,incomt,outgo
select mtime,income,outgo,
       sum(income-outgo)over(order by mtime
       rows between unbounded preceding and current row ) rest,
       row_number()over(partition by mtime order by mtime ) rn
from item_a;
-----具体过程如下-----------------------------------------------------
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

会话已更改。

SQL> create table item_a
  2  (mtime date,
  3   income number,
  4   outgo number
  5   );

表已创建。

SQL> insert into item_a values(to_date('20050301','yyyymmdd'),50,30);

已创建 1 行。

SQL> insert into item_a values(to_date('20050302','yyyymmdd'),45,60);

已创建 1 行。

SQL> insert into item_a values(to_date('20050305','yyyymmdd'),60,10);

已创建 1 行。

SQL> commit;

提交完成。

SQL> select mtime,income,outgo,rest from
  2  (select mtime,income,outgo,
  3          sum(income-outgo)over(order by mtime) rest,
  4          row_number()over(partition by mtime order by mtime desc)rn
  5    from item_a
  6  )where rn=1;

MTIME                   INCOME      OUTGO       REST
------------------- ---------- ---------- ----------
2005-03-01 00:00:00         50         30         20
2005-03-02 00:00:00         45         60          5
2005-03-05 00:00:00         60         10         55

SQL> insert into item_a values(to_date('20050305','yyyymmdd'),60,10);

已创建 1 行。

SQL> select mtime,income,outgo,rest from
  2  (select mtime,income,outgo,
  3          sum(income-outgo)over(order by mtime) rest,
  4          row_number()over(partition by mtime order by mtime desc)rn
  5    from item_a
  6  )where rn=1;

MTIME                   INCOME      OUTGO       REST
------------------- ---------- ---------- ----------
2005-03-01 00:00:00         50         30         20
2005-03-02 00:00:00         45         60          5
2005-03-05 00:00:00         60         10        105

SQL> insert into item_a values(to_date('20050303','yyyymmdd'),0,0);

已创建 1 行。

SQL> insert into item_a values(to_date('20050304','yyyymmdd'),0,0);

已创建 1 行。

SQL> commit;

提交完成。

SQL> select mtime,income,outgo,rest from
  2  (select mtime,income,outgo,
  3          sum(income-outgo)over(order by mtime) rest,
  4          row_number()over(partition by mtime order by mtime desc)rn
  5    from item_a
  6  )where rn=1;

MTIME                   INCOME      OUTGO       REST
------------------- ---------- ---------- ----------
2005-03-01 00:00:00         50         30         20
2005-03-02 00:00:00         45         60          5
2005-03-03 00:00:00          0          0          5
2005-03-04 00:00:00          0          0          5
2005-03-05 00:00:00         60         10        105

SQL>

使用道具 举报

回复
论坛徽章:
18
授权会员
日期:2007-03-20 21:21:402014年新春福章
日期:2014-02-18 16:41:112012新春纪念徽章
日期:2012-01-04 11:50:44ITPUB十周年纪念徽章
日期:2011-11-01 16:20:282011新春纪念徽章
日期:2011-02-18 11:43:342010广州亚运会纪念徽章:自行车
日期:2010-11-22 15:27:372010新春纪念徽章
日期:2010-03-01 11:19:06生肖徽章2007版:猴
日期:2009-11-18 16:42:41CTO参与奖
日期:2009-02-12 11:45:48ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:44
65#
发表于 2005-9-14 15:34 | 只看该作者
支持!!

使用道具 举报

回复
论坛徽章:
0
66#
发表于 2005-11-4 14:31 | 只看该作者
这么多年了也顶一下好了

使用道具 举报

回复
论坛徽章:
6
会员2006贡献徽章
日期:2006-04-17 13:46:34参与2007年甲骨文全球大会(中国上海)纪念
日期:2007-08-06 15:19:01会员2007贡献徽章
日期:2007-09-26 18:42:10ITPUB新首页上线纪念徽章
日期:2007-10-20 08:38:442009新春纪念徽章
日期:2009-01-04 14:52:28ITPUB十周年纪念徽章
日期:2011-11-01 16:20:28
67#
发表于 2006-3-26 13:57 | 只看该作者
第一个问题答案:
select trandate, income, expense,
income-expense+
nvl(lag(income) over(order by trandate),0)
-nvl(lag(expense) over(order by trandate), 0)
balance from mytab;
TRANDATE       INCOME    EXPENSE    BALANCE
---------- ---------- ---------- ----------
2000-03-01         50         30         20
2000-03-02         45         60          5
2000-03-05         60         10         35

使用道具 举报

回复
论坛徽章:
112
2008新春纪念徽章
日期:2008-02-13 12:43:03马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上加薪
日期:2014-02-19 11:55:14马上有车
日期:2014-11-03 12:40:39沸羊羊
日期:2015-03-04 14:43:432015年新春福章
日期:2015-03-06 11:57:31慢羊羊
日期:2015-03-09 16:15:39
68#
发表于 2006-3-26 14:53 | 只看该作者
顶上去,让更多的人了解一下。

使用道具 举报

回复
论坛徽章:
3
授权会员
日期:2006-05-11 15:55:152011新春纪念徽章
日期:2011-02-18 11:42:492013年新春福章
日期:2013-02-25 14:51:24
69#
发表于 2006-3-29 14:04 | 只看该作者
顶一下,都是高人

使用道具 举报

回复
论坛徽章:
0
70#
发表于 2006-3-29 14:07 | 只看该作者
提示: 该帖被管理员或版主屏蔽

使用道具 举报

回复

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

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表