473,545 Members | 529 Online
Bytes | Software Development & Data Engineering Community
+ 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 2067
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
9190
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 a table (c) re-queries another table using a subquery which references the inserted table (correlated or not)
8
19575
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 couple of tables in my database using INNER JOINS and the WHERE clause to specify the required constraints. However, I also want to read two...
7
2359
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 which a part may be composed. I have a table of parts and their subparts. The problem is that each of those subparts may be composed of smaller...
5
6574
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 following SQL statement. INSERT INTO OrderItems (ClientID, ProductID, OrderHeaderID, Quantity, Dispatched, BackOrdered) SELECT ClientID, ProductID,...
13
3810
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. This is what I came up with: SELECT GI.generalinfoid AS GeneralInfoId, max(GI.FirstName) AS FirstName, max(GI.LastName) AS LastName,...
0
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7752
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3449
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1878
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 we have to send another system
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.