Subquery Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mipo1984
    New Member
    • Apr 2008
    • 1

    #1

    Subquery Problem

    I have subquery into a principal query, and i need the subquery return me only the last row of all results, but i can`t use "order by <field> desc " in the subquery because this return me an error, nither "fetch first 1 rows only" because this return a error too. Any sugestion?


    this es the SQL...

    Code:
    SELECT
      C.NOMBRE,V.COBJCOD,CS.DESC,V.CSUBCOD,
      N.NOMBRE,CS.NIVEL,V.SUBLIBRO,
      V.TSUBLIBRO,CS.CAT5COD,CAT7COD,
      A.CODIGO,V.ANIO,M.codigo,V.MES,
      sum(V.saldod),acumulado
    FROM
      QS36F.FIN0002M V
    Inner Join
      QS36F.V570010 S
    on
      (S.codigo=V.SUCCOD)
    Inner Join
      QS36F.V57CIA E
    on
      (S.CODEMP=E.codigo)
    Inner Join
      QS36F.V570909 C
    on
      (C.cuenta=V.COBJCOD)
    Inner Join
      QS36F.V570901 CS
    on
      (CS.UNEGCOD=V.UNEGCOD
      and
      CS.CSUBCOD=V.CSUBCOD
      and
      CS.COBJCOD=V.COBJCOD)
    Inner Join
      QS36F.NIVEL N
    on
      (CS.nivel=N.codigo)
    Inner Join
      QS36F.ANIO A
    on
      (A.codigo=V.anio)
    Inner Join
      QS36F.MES M
    on
      (M.codigo=V.mes) 
    
    WHERE 
    
      acumulado IN 
     (
      ///SUBQUERY
      select
        acumulado
      from
        QS36F.FIN0002M 
      // order by acumulado desc ( <--  this return a error)
      ) 
    AND
      (E.codigo='E01')
    AND
      (A.codigo='2007' OR A.codigo='2006')
    
    GROUP BY
      C.nombre,V.COBJCOD,CS.DESC,
      V.CSUBCOD,N.nombre,CS.nivel,
      V.sublibro,V.TSUBLIBRO,CS.cat5cod,
      CS.cat7cod,A.codigo,V.anio,
      M.codigo,V.mes
    ORDER BY
      C.nombre,V.COBJCOD,CS.DESC,
      V.CSUBCOD,N.nombre,CS.nivel, V.sublibro,
      V.TSUBLIBRO,CS.cat5cod,CS.cat7cod,
      A.codigo,V.anio,M.codigo,V.mes,acumulado
    
    FETCH FIRST 80 ROWS ONLY
    Last edited by docdiesel; Apr 4 '08, 08:19 PM. Reason: Added code tags
  • docdiesel
    Recognized Expert Contributor
    • Aug 2007
    • 297

    #2
    Hi,

    first, I took the liberty to add some code tags to your post for better readability.

    Second, if you sort descending by 'acumulado' and just need the first row, then why don't you use

    Code:
    SELECT
      max(acumulado)
    FROM
      QS36F.FIN0002M
    If that doesn't fit your needs, then you could create a view based on your subquery and uses that view in the conditions of your big query.

    Saludos,

    Bernd

    Comment

    Working...