473,473 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Subquery Problem

1 New Member
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...

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   C.NOMBRE,V.COBJCOD,CS.DESC,V.CSUBCOD,
  3.   N.NOMBRE,CS.NIVEL,V.SUBLIBRO,
  4.   V.TSUBLIBRO,CS.CAT5COD,CAT7COD,
  5.   A.CODIGO,V.ANIO,M.codigo,V.MES,
  6.   sum(V.saldod),acumulado
  7. FROM
  8.   QS36F.FIN0002M V
  9. Inner Join
  10.   QS36F.V570010 S
  11. on
  12.   (S.codigo=V.SUCCOD)
  13. Inner Join
  14.   QS36F.V57CIA E
  15. on
  16.   (S.CODEMP=E.codigo)
  17. Inner Join
  18.   QS36F.V570909 C
  19. on
  20.   (C.cuenta=V.COBJCOD)
  21. Inner Join
  22.   QS36F.V570901 CS
  23. on
  24.   (CS.UNEGCOD=V.UNEGCOD
  25.   and
  26.   CS.CSUBCOD=V.CSUBCOD
  27.   and
  28.   CS.COBJCOD=V.COBJCOD)
  29. Inner Join
  30.   QS36F.NIVEL N
  31. on
  32.   (CS.nivel=N.codigo)
  33. Inner Join
  34.   QS36F.ANIO A
  35. on
  36.   (A.codigo=V.anio)
  37. Inner Join
  38.   QS36F.MES M
  39. on
  40.   (M.codigo=V.mes) 
  41.  
  42. WHERE 
  43.  
  44.   acumulado IN 
  45.  (
  46.   ///SUBQUERY
  47.   select
  48.     acumulado
  49.   from
  50.     QS36F.FIN0002M 
  51.   // order by acumulado desc ( <--  this return a error)
  52.   ) 
  53. AND
  54.   (E.codigo='E01')
  55. AND
  56.   (A.codigo='2007' OR A.codigo='2006')
  57.  
  58. GROUP BY
  59.   C.nombre,V.COBJCOD,CS.DESC,
  60.   V.CSUBCOD,N.nombre,CS.nivel,
  61.   V.sublibro,V.TSUBLIBRO,CS.cat5cod,
  62.   CS.cat7cod,A.codigo,V.anio,
  63.   M.codigo,V.mes
  64. ORDER BY
  65.   C.nombre,V.COBJCOD,CS.DESC,
  66.   V.CSUBCOD,N.nombre,CS.nivel, V.sublibro,
  67.   V.TSUBLIBRO,CS.cat5cod,CS.cat7cod,
  68.   A.codigo,V.anio,M.codigo,V.mes,acumulado
  69.  
  70. FETCH FIRST 80 ROWS ONLY
Apr 4 '08 #1
1 2062
docdiesel
297 Recognized Expert Contributor
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

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.   max(acumulado)
  3. FROM
  4.   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
Apr 4 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
7
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
5
by: Rod | last post by:
I have a client site where the code below has been working happily for at least four months. The site is using SQL Server 7. The code is ASP.NET Last week an error appeared related to the...
13
by: ThePrinceIsRight | last post by:
I have a problem with using a subquery in MS Access. The purpose of the sub-query is to create a list of people who have had doctor exams in the past 6 months and exclude them from the main query....
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.