解决方案 »

  1.   


     select SUM(n) from tableB where [time] between '' and '' and ID =(select id from tablea where stationname='')
      

  2.   

    select stationname,sum(n)
    from A join B on A.id=b.id
    where time between '' and ''
    group by stationname
      

  3.   


    SELECT B.ID, SUM(B.N), MAX(B.TIME), MIN(B.TIME) 
    FROM 表A A LEFT JOIN 表B B ON A.ID = B.ID  
    WHERE A.区域 = '区域1'
    GROUP BY B.ID
      

  4.   


    --测试数据
    create table ta (id int,stationname nvarchar(20),area nvarchar(20))
    insert into ta
    select 1,'站点1','区域1' union all
    select 2,'站点2','区域1' union all
    select 3,'站点3','区域1' union all
    select 4,'站点4','区域1' union all
    select 5,'站点5','区域1' union all
    select 6,'站点6','区域2' union all
    select 7,'站点7','区域2' union all
    select 8,'站点8','区域2' union all
    select 9,'站点9','区域2' union all
    select 10,'站点10','区域2'create table tb (id int,time datetime,n int)
    insert into tb
    select 1,'2013-1-11',10 union all
    select 1,'2013-1-2',12 union all
    select 1,'2013-1-3',15 union all
    select 1,'2013-1-4',18 union all
    select 1,'2013-1-8',25 union all
    select 1,'2013-1-9',30 union all
    select 2,'2013-1-1',2 union all
    select 2,'2013-1-2',9 union all
    select 2,'2013-1-3',15 union all
    select 2,'2013-1-4',16 union all
    select 2,'2013-1-5',19 union all
    select 2,'2013-1-6',25 union all
    select 2,'2013-1-7',30 union all
    select 8,'2013-1-1',50 --结果
    select SUM(n) as total,tt.id,MAX(tt.time)as '最新',MIN(tt.time) as'最旧' from (select ta.id,ta.stationname,tb.time,tb.n  from  ta inner join  tb on ta.id=tb.id and ta.area='区域1' )tt where tt.time between '2013-1-2' and '2013-1-7' group by tt.id
     
     
      

  5.   


    SELECT  t.stationname ,t.id ,t.bigTime , bb.n ,t.minTime , cc.n
    FROM    ( SELECT    MAX(TIME) bigTime , MIN(TIME) minTime , a.id , a.stationname
              FROM      ta a INNER JOIN TB b ON a.id = b.id  AND a.area = '区域1'
              WHERE     b.TIME BETWEEN '2013-01-02' AND '2013-01-07'
              GROUP BY  a.stationname ,a.id
            ) t
            LEFT JOIN TB bb ON t.bigTime = bb.TIME AND t.id = bb.id
            LEFT JOIN TB cc ON t.minTime = cc.TIME AND t.id = cc.id结果:
      

  6.   

    --测试数据
    create table A (id int,stationname nvarchar(10),area nvarchar(10))
    insert into A
    select 1,'站点1','区域1' UNION ALL
    select 2,'站点2','区域1' UNION ALL
    select 3,'站点3','区域1' UNION ALL
    select 4,'站点4','区域1' UNION ALL
    select 5,'站点5','区域1' UNION ALL
    select 6,'站点6','区域2' UNION ALL
    select 7,'站点7','区域2' UNION ALL
    select 8,'站点8','区域2' UNION ALL
    select 9,'站点9','区域2' UNION ALL
    select 10,'站点10','区域2'CREATE TABLE B (id int,[time] datetime,n int)
    INSERT INTO B
    SELECT 1,'2013-1-1',10 UNION ALL
    SELECT 1,'2013-1-2',12 UNION ALL
    SELECT 1,'2013-1-3',15 UNION ALL
    SELECT 1,'2013-1-1',2 UNION ALL
    SELECT 2,'2013-1-2',9 UNION ALL
    SELECT 2,'2013-1-3',15 UNION ALL
    SELECT 2,'2013-1-4',16
    --语句SELECT C.stationname AS '站点名',C.最旧日期,BB.n AS '最旧度',C.最新日期,CC.n AS '最新度'
    FROM
    (
    SELECT A.stationname,A.id,MIN(B.time) AS '最旧日期',MAX(B.time) AS '最新日期'
    FROM A
    LEFT JOIN B
    ON A.id=B.id
    WHERE A.area='区域1'
    AND B.time BETWEEN '2013-1-2' AND '2013-1-7'
    GROUP BY A.stationname,A.id
    ) AS C
    LEFT JOIN B AS BB
    ON BB.id=C.id AND BB.time=C.最旧日期
    LEFT JOIN B AS CC
    ON CC.id=C.id AND CC.time=C.最新日期--结果
    站点名        最旧日期                    最旧度         最新日期                    最新度
    ---------- ----------------------- ----------- ----------------------- -----------
    站点1        2013-01-02 00:00:00.000 12          2013-01-03 00:00:00.000 15
    站点2        2013-01-02 00:00:00.000 9           2013-01-04 00:00:00.000 16
      

  7.   



    select a.id,b.N as max_val,case when b.n is null then null else b.max_time end max_time,
    d.n as min_val,case when d.n is null then null else b.min_time end min_time
    from A  a left join B b on a.id=b.id
    left join (select id,max(time) max_time,min(time) min_time
    from B where time between '20130102' and '20130107'
    group by id )c on b.id=c.id and  b.time=c.max_time 
    left join B d on c.id=d.id and c.min_time=d.time
    where a.area='區域1'