有一个表定义如下
year,month,sumrk(数值型),sumck(数值型)四个字段现要实现如下查询:
要查询2003年的年报数据时
返回的结果集为
year,sumrk,sumck三个字段组成的query
要求sumrk的数值为2002年12月的统计数据,而sumck的数值为2003年整年的统计数据
应该怎么写SQL语句?

解决方案 »

  1.   

    select year,sumrk,sumck,(select sum(sumck) as sumck1 from yourtable) from your table where year=... and month=...
      

  2.   

    select year 
          (select sum(sumck) from yourtable where year = 2002 and month = 12)
          (select sum(sumrk) from yourtable where year = 2003)
          from yourtable where year = .. and month = ..
      

  3.   

    select '2003', 
          (select sumrk from yourtable where year = '2002' and month = '12'),
          (select sum(sumck) from yourtable where year = '2003')
          from yourtable
      

  4.   

    假设你的year,month为数值型
    select C.year,C.sumck,D.sumrk from 
    (select A.year,B.sumck from table1 A 
    INNER join 
    (select sum(sumck) as sumck from table1 where year=2003) B on a.year=b.year) C 
    INNER JOIN 
    (select sum(sumrk) as sumrk from table1 where year=2002 and month=12) D 
    ON C.YEAR=(D.YEAR-1) where c.year=2003
      

  5.   

    select year, 
          (select sum(sumrk) from table where year='2002' and month='12')as sumrk ,
           sum(sumck) as sumck
    from table 
    where year='2003'
    group by year
      

  6.   

    select year,month,sumrk,b.sumck from table1 
    left join 
    (select sum(sumck) as sumck from table1 where year=2003)) B on a.year=b.year
    where month=12
      

  7.   

    假设你的 year month 为字符型:
    select 
    year, 
    (select sum(sumrk) from yourtable where year='2002' and month='12')as sumrk ,
    sum(sumck) as sumck
    from yourtable 
    where year='2003'
    group by year
    但是不知道你要这样表示怎么用途,因为像这样结果显示 有点怪,比如
    year 年为2003, 那么sumrk为 2002年12和为 1000 , sumck为 2003年和为 1000000
    作为一条记录看起来让人糊涂。
      

  8.   

    SELECT table_a.year,table_a.sumrk,table_b.sumck
    FROM (SELECT year,sumrk FROM table WHERE (year=2003) and (month=12)) table_a LEFT  OUTER JOIN
          (SELECT year,SUM(sumck)AS sumck FROM table WHERE (year=2003) GROUP BY year) table_b ON table_a.year=table_b.year
      

  9.   

    select 2003,
          (select sum(sumck) from yourtable where year = 2002 and month = 12),
          (select sum(sumrk) from yourtable where year = 2003)
          from yourtable
      

  10.   

    select year,sumrk,sumck,(select sum(sumck) as sumck1 from yourtable) 
    from your table where year=... and month=...
      

  11.   

    select 2003,
          (select sum(sumck) from yourtable where year = 2002 and month = 12),
          (select sum(sumrk) from yourtable where year = 2003)
      

  12.   

    反了,应该是
    select 2003,
          (select sum(sumrk) from yourtable where year = 2002 and month = 12),
          (select sum(sumck) from yourtable where year = 2003)
      

  13.   

    关于本问题的求救:
    前题说明:
    zwfckbb 出入库详细数据表
    product 产品表,里面包含产品的大类名称
    cpdl    产品大类分类表,包含大类序号和名称,
    本来写了以下查询
    select  bb.ckbh,bb.year,tc.xh,p.dl,sum(bb.lastnum) as lastnum,sum(bb.rk) as rk,
    sum(bb.chk) as chk
     from zwfckbb bb,product p,cpdl tc where bb.year='2000' and (bb.cpbh=p.cpbh) and (p.dl=tc.dl) 
    group by bb.ckbh,bb.year,tc.xh,p.dl 
    查出来对就的结果是:
    出库编号  统计年份 序号 大类名称 年初结存 本年入库 本年出库但是如果这样查询 统计数据都是一年的,如2000年的
    但是问题在:年初结存指的是上年12月份的lastnum数目,并不是本年lastnum的合计
    请问本问题如何解决!
    我另开帖子,请朋友们看看!