| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3502 人关注过本帖, 1 人收藏
标题:[出题了]2008年9月25日
只看楼主 加入收藏
seiya027848
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2008-6-27
收藏
得分:0 
--获得时间区域
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1

--获得时间区域表
select t1.date,t1.num,t1.tag,dtrang.startdate from t1 left join (
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1
) dtrang on t1.tag=dtrang.tag

--获得数据集合
select * from t2
union all
select * from t3

--获得所需要的数据
select dataset.type as datatype,dataset.num1,timesheet.startdate,timesheet.date as enddate,timesheet.tag from
(select t1.date,t1.num,t1.tag,dtrang.startdate from t1 left join (
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1
) dtrang on t1.tag=dtrang.tag) as timesheet
,(select * from t2 union all select * from t3) dataset
where dataset.date between timesheet.startdate and timesheet.date

--凑结构
select date,num,t1.tag,
sum(case when datatype='in' then num1 end) as numin,
sum(case when datatype='out' then num1 end) as numout from t1
left join (
select dataset.type as datatype,dataset.num1,timesheet.startdate,timesheet.date as enddate,timesheet.tag from
(select t1.date,t1.num,t1.tag,dtrang.startdate from t1 left join (
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1
) dtrang on t1.tag=dtrang.tag) as timesheet
,(select * from t2 union all select * from t3) dataset
where dataset.date between timesheet.startdate and timesheet.date
) as result on t1.tag=result.tag
group by date,num,t1.tag

结果:
图片附件: 游客没有浏览图片的权限,请 登录注册
2008-09-26 17:31
师妃暄
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:805
专家分:107
注 册:2006-3-1
收藏
得分:0 
select a.date as startdate,b.date as enddate,b.tag from t1 as a,t1 as b
where a.tag=b.tag-1

where条件貌似不能这样..因为tag并不保证上下就相差1的呢

有实力才会有魅力 实力来自坚持不懈的努力
2008-09-26 17:42
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
学习中。。。数据库真好玩,又找到好玩的了

学习需要安静。。海盗要重新来过。。
2008-09-26 19:19
Ethip
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:771
专家分:0
注 册:2008-1-18
收藏
得分:0 
路过下...
2008-09-26 21:14
liyanhong
Rank: 3Rank: 3
来 自:水星
等 级:禁止访问
威 望:8
帖 子:1867
专家分:0
注 册:2008-5-3
收藏
得分:0 
好玩就多玩玩哈

爱上你 是 我的错  可是离 开  又舍不得  听着你为我写的歌     好难过
如果说 我说如果  我们还 能  重新来过   不去计 较 谁对谁错  会怎么做
2008-09-26 21:34
happynight
Rank: 8Rank: 8
等 级:贵宾
威 望:15
帖 子:807
专家分:760
注 册:2008-4-26
收藏
得分:0 
create table t1
(
    [date] datetime,
    num int,
    tag varchar(10)
);

create table t2
(
    [id] int identity(1,1) primary key not null,
    num1 int,
    [date] datetime,
    [type] char(8)
);

create table t3
(
    [id] int identity(1,1) primary key not null,
    num2 int,
    [date] datetime,
    [type] char(8)
);

insert into t1 values('2008-2-2',34,'2');
insert into t1 values('2008-3-4',65,'3');
insert into t1 values('2008-11-2',33,'4');

insert into t2 values(22,'2008-2-4','in');
insert into t2 values(44,'2008-3-1','out');
insert into t2 values(67,'2008-9-1','in');
insert into t2 values(22,'2008-2-21','in');
insert into t2 values(10,'2008-5-3','out');

insert into t3 values(55,'2008-3-6','in');
insert into t3 values(44,'2008-3-3','out');
insert into t3 values(21,'2008-2-5','in');
insert into t3 values(11,'2008-6-1','out');


/*
date            num      tag    numin     numout
2008.2.2        34        2                                    (第一条没有上一条的时间,所以这两列为空)
2008.3.4        65        3       43               88       (日期在2008.2.2-2008.3.4之间的tabel2,tabel3中的num1,num2的和)
2008.11.2       33        4      122             21        (日期在2008.3.4-2008.11.2之间的tabel2,tabel3中的num1,num2的和)
*/
SELECT * FROM t1 order by date
SELECT * FROM t2 order by date
SELECT * FROM t3 order by date

SELECT date,num,tag,
       (SELECT SUM(CASE TYPE WHEN 'IN' THEN num1 ELSE 0 END)
        FROM t2  INNER JOIN
             (SELECT MAX(date) AS Ndate FROM t1 a WHERE  t1.date>a.date) a
              ON t2.date>a.Ndate
        WHERE t2.date<=t1.date)
       +  
       (SELECT SUM(CASE TYPE WHEN 'IN' THEN num2 ELSE 0 END)
        FROM t3  INNER JOIN
             (SELECT MAX(date) AS Ndate FROM t1 a WHERE  t1.date>a.date) a
              ON t3.date>a.Ndate
        WHERE t3.date<=t1.date) AS numin,
      
        (SELECT SUM(CASE TYPE WHEN 'OUT' THEN num1 ELSE 0 END)
        FROM t2  INNER JOIN
             (SELECT MAX(date) AS Ndate FROM t1 a WHERE  t1.date>a.date) a
              ON t2.date>a.Ndate
        WHERE t2.date<=t1.date)
       +  
       (SELECT SUM(CASE TYPE WHEN 'OUT' THEN num2 ELSE 0 END)
        FROM t3  INNER JOIN
             (SELECT MAX(date) AS Ndate FROM t1 a WHERE  t1.date>a.date) a
              ON t3.date>a.Ndate
        WHERE t3.date<=t1.date) AS numout
FROM t1  

DROP TABLE t1
DROP TABLE t2
DROP TABLE t3
2008-09-27 15:25
happynight
Rank: 8Rank: 8
等 级:贵宾
威 望:15
帖 子:807
专家分:760
注 册:2008-4-26
收藏
得分:0 
结果:
date                       num      tag      numin    numout
2008-02-02 00:00:00.000    34    2    NULL    NULL
2008-03-04 00:00:00.000    65    3    65    88
2008-11-02 00:00:00.000    33    4    122    21

2008-02-02      2008-03-04 之间
t2:
1             22         2008.2.4         in
4            22          2008.2.21      in

t3:
4            21          2008.2.5        in

一共是65
你应该是漏了t2中的数据了
2008-09-27 15:29
happynight
Rank: 8Rank: 8
等 级:贵宾
威 望:15
帖 子:807
专家分:760
注 册:2008-4-26
收藏
得分:0 
[bo][un]西风独自凉[/un] 在 2008-9-26 14:23 的发言:[/bo]

select a.*,
numin=(select sum(num1)  from table2 where sdate between  (select top 1  sdate from table1 where sdate  


不过你的表结构和LZ的有出入 改了下
select a.*,
numin=(select sum(num1)  from t2
       where date between  
        (select top 1  date from t1 where date <a.date order by date desc )
         and  a.date  group by type having type ='in' )
    + (select sum(num2)  from t3
       where date between   
         (select top 1  date from t1 where date <a.date order by date desc )
          and  a.date   group by type having type ='in' ),

numout=(select sum(num1)  from t2
        where date between  
        (select top 1  date from t1 where date <a.date order by date desc )
         and  a.date  group by type having type ='out' )
        + (select sum(num2)  from t3 where date between   
           (select top 1  date from t1 where date <a.date order by date desc )
          and  a.date   group by type having type ='out' )
from t1 a
2008-09-27 15:49
happynight
Rank: 8Rank: 8
等 级:贵宾
威 望:15
帖 子:807
专家分:760
注 册:2008-4-26
收藏
得分:0 
呵呵 再盗版你的 不会追究版权吧
select a.*,
numin=(select sum(num1)  from t2
       where date between  
        (select top 1  date from t1 where date <a.date order by date desc )
         and  a.date  AND type ='in'  group by type )
    + (select sum(num2)  from t3
       where date between   
         (select top 1  date from t1 where date <a.date order by date desc )
          and  a.date   AND type ='in'  group by type ),

numout=(select sum(num1)  from t2
        where date between  
        (select top 1  date from t1 where date <a.date order by date desc )
         and  a.date  AND type ='out'  group by type  )
        + (select sum(num2)  from t3 where date between   
           (select top 1  date from t1 where date <a.date order by date desc )
          and  a.date   AND type ='out'  group by type   )
from t1 a
--------------------------
修改下我的
SELECT date,num,tag,
       (SELECT SUM( num1)
        FROM t2  INNER JOIN
             (SELECT MAX(date) AS Ndate FROM t1 a WHERE  t1.date>a.date) a
              ON t2.date>a.Ndate
        WHERE t2.date<=t1.date AND t2.TYPE='IN')
       +  
       (SELECT SUM(num2)
        FROM t3  INNER JOIN
             (SELECT MAX(date) AS Ndate FROM t1 a WHERE  t1.date>a.date) a
              ON t3.date>a.Ndate
        WHERE t3.date<=t1.date  AND t3.TYPE='IN') AS numin,
      
        (SELECT SUM(num1)
        FROM t2  INNER JOIN
             (SELECT MAX(date) AS Ndate FROM t1 a WHERE  t1.date>a.date) a
              ON t2.date>a.Ndate
        WHERE t2.date<=t1.date AND t2.TYPE='OUT')
       +  
       (SELECT SUM(num2)
        FROM t3  INNER JOIN
             (SELECT MAX(date) AS Ndate FROM t1 a WHERE  t1.date>a.date) a
              ON t3.date>a.Ndate
        WHERE t3.date<=t1.date AND t3.TYPE='OUT') AS numout
FROM t1

[[it] 本帖最后由 happynight 于 2008-9-27 15:57 编辑 [/it]]
2008-09-27 15:53
jackesing
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2008-9-20
收藏
得分:0 
楼主!
2008.2.2-2008.3.4之间为IN的和是不是漏加了一个22呀!
我怎么算来算去都是65!!(其他的楼的高手解答也是65!!!)
楼主再确认一下如何!
2008-09-28 16:22
快速回复:[出题了]2008年9月25日
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015835 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved