解决方案 »

  1.   

    你要的不会是这样吧~select a.id,a.name,a.age,b.name from a  ,a b
    where a.time=2014 and b.time=2013 and a.id=b.id
      

  2.   

    标准的行转列的需求,采用decode的方法比自关联的方法要快
    11g只有版本可以考虑使用pivot进行行转列
    select id,
    max(decode(time,2014,name)),
    max(age),
    max(decode(time,2013,name))
    from a
    where time in (2014,2013)
    group by id
      

  3.   

    用了
    使用了这个可以,又出现新问题,要求time=2014时,才能查询结果来,该如何写
      

  4.   

    select id,
    max(decode(time,2014,name)),
    max(age),
    max(decode(time,2013,name))
    from a a1
    where time=2014 or
    (time=2013 and exists (select 1 from a where id=a1.id and time=2014))
    group by id
    或是采用1#的方法改为左连接的方式
    select a.id,a.name,a.age,b.name from a  ,a b
    where a.time=2014 and b.time=2013 and a.id(+)=b.id
      

  5.   

    很奇怪,用了where time=2014 or
    time=2013  后提示无效数字是为什么
      

  6.   

    你的time是什么数据类型?
    把你报错的正式语句贴上来,如果time是数字型的话这么写没啥问题
      

  7.   

    select t.item_id,
        max(a.item_name) as itemname,
        max(getunitname(a.item_code)) as unitname,
        max(decode(t.company_code,'0101',t.item_value)) as value1,
        max(decode(t.company_code,'0102',t.item_value)) as value2,
        max(decode(t.company_code,'0103',t.item_value)) as value3,
        max(decode(t.company_code,'0104',t.item_value)) as value4,
        max(decode(t.company_code,'0105',t.item_value)) as value5,
        max(decode(t.company_code,'0106',t.item_value)) as value6,
        sum(decode(t.budget_time,2014,t.item_value)) as ietmvalue,
        sum(decode(t.budget_time,2013,t.item_value)) as lastietmvalue
      from CBM_J_BUDGET_GATHERCOM t,CBM_C_ITEM a
      where a.item_id = t.item_id
        and t.budget_time ='2014'
        and t.topic_id = '45'
        or t.budget_time='2013'
        and exists (select 1 from CBM_J_BUDGET_GATHERCOM tt,CBM_C_ITEM aa 
    where aa.item_id = tt.item_id and tt.topic_id = '45' and tt.budget_time ='2013')
     group by t.item_id  order by t.item_id 我现在用in(2013,2014)行了,不过还是想知道原因
      

  8.   

    这语句逻辑有问题啊
    select t.item_id,
        max(a.item_name) as itemname,
        max(getunitname(a.item_code)) as unitname,
        max(decode(t.company_code,'0101',t.item_value)) as value1,
        max(decode(t.company_code,'0102',t.item_value)) as value2,
        max(decode(t.company_code,'0103',t.item_value)) as value3,
        max(decode(t.company_code,'0104',t.item_value)) as value4,
        max(decode(t.company_code,'0105',t.item_value)) as value5,
        max(decode(t.company_code,'0106',t.item_value)) as value6,
        sum(decode(t.budget_time,2014,t.item_value)) as ietmvalue,
        sum(decode(t.budget_time,2013,t.item_value)) as lastietmvalue
      from CBM_J_BUDGET_GATHERCOM t,CBM_C_ITEM a
      where a.item_id = t.item_id
        and t.topic_id = '45'
        and (t.budget_time ='2014'
            or (t.budget_time='2013'
                and exists (select 1 from CBM_J_BUDGET_GATHERCOM
                    where item_id = t.item_id and topic_id = '45' and budget_time ='2014')))
     group by t.item_id  order by t.item_id 
      

  9.   

    可以不用管上面的,主要是sum两个,有什么问题,该怎么改啊
      

  10.   

    把2013、2014都用引号引起来
    你的budget_time应该是字符串类型的吧    sum(decode(t.budget_time,'2014',t.item_value)) as ietmvalue,
        sum(decode(t.budget_time,'2013',t.item_value)) as lastietmvalue
      

  11.   

    我去,字符串类型的怎么sum求和,改成max,脑子抽了,现在才转过来