473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ORDER BY error, need help!!

When I run this query, I get an ORDER BY error "Incorrect syntax near the keyword 'ORDER'. I've bolded it below.

If I take the ORDER by out then it works correctly, but I need the ORDER BY because of the TOP 9. Any Suggestions???
Expand|Select|Wrap|Line Numbers
  1. (SELECT TOP 9 trans.vchTrustee, trans.vchCaseNumber, Trans.TotalReceipts + SUM(a.mnyBalanceAmount) As Top9Total
  2.  
  3.     FROM tblCase c1
  4.  
  5.         LEFT JOIN tblAsset a ON c1.intCaseID = a.intCaseID
  6.         LEFT JOIN
  7.         (Select trans40.vchTrustee, trans40.vchCaseNumber, SUM(trans40.TdMnyAmount) As TotalReceipts
  8.  
  9.         From
  10.             (SELECT c.vchTrustee, c.vchCaseNumber, SUM(td.mnyAmount) As TdMnyAmount
  11.                    FROM   tblTransactionDetail td
  12.  
  13.                       INNER JOIN tblTransaction t ON td.intTransactionID = t.intTransactionID
  14.             INNER JOIN tblBankAccount b ON t.intAccountID = b.intAccountID
  15.             INNER JOIN tblCase c ON b.intCaseID = c.intCaseID
  16.  
  17.                    GROUP BY c.intDeleted, b.intDeleted, td.intDeleted, t.intDeleted,
  18.             c.vchTrustee, c.vchCaseNumber, c.intCaseID, td.intTransactionType,
  19.             t.intTransactionMethod, c.intStatus
  20.  
  21.             HAVING t.intDeleted = 0 AND td.intDeleted = 0 AND b.intDeleted = 0 AND c.intDeleted = 0
  22.             AND td.intTransactionType = 200
  23.             AND (t.intTransactionMethod = 40 OR t.intTransactionMethod = 41 OR t.intTransactionMethod = 43)
  24.             AND c.vchTrustee = 'RLW' AND c.intStatus = 1
  25.  
  26.                    ) As Trans40
  27.  
  28.         GROUP BY Trans40.vchTrustee, Trans40.vchCaseNumber
  29.         ) As Trans ON c1.vchCaseNumber = Trans.vchCaseNumber
  30.  
  31.     GROUP BY trans.vchTrustee, trans.vchCaseNumber, a.intDeleted, c1.intDeleted, trans.TotalReceipts, c1.intStatus
  32.  
  33.     HAVING a.intDeleted = 0 AND c1.intDeleted = 0 AND trans.vchTrustee = 'RLW' AND c1.intStatus = 1
  34.  
  35.     ORDER BY (Trans.TotalReceipts + SUM(a.mnyBalanceAmount)) DESC)
  36.  
  37. UNION All
  38.  
  39.     (SELECT Top9.vchTrustee, ' ', (AllCases.TR - SUM(Top9.Top9Total)) As TRMinusTop9
  40.     FROM
  41.  
  42.         (SELECT AllTotalReceipts.vchTrustee, SUM(AllTotalReceipts.TotalMoney) As TR
  43.         FROM
  44.  
  45.             (SELECT trans.vchTrustee, trans.vchCaseNumber, Trans.TotalReceipts, SUM(a.mnyBalanceAmount) As totalAssetBalance, 
  46.             (Trans.TotalReceipts + SUM(a.mnyBalanceAmount)) As TotalMoney
  47.  
  48.             FROM tblCase c1
  49.  
  50.             LEFT JOIN tblAsset a ON c1.intCaseID = a.intCaseID
  51.             LEFT JOIN
  52.                 (Select trans40.vchTrustee, trans40.vchCaseNumber, SUM(trans40.TdMnyAmount) As TotalReceipts
  53.  
  54.                 From
  55.                     (SELECT c.vchTrustee, c.vchCaseNumber, SUM(td.mnyAmount) As TdMnyAmount
  56.                            FROM   tblTransactionDetail td
  57.                                   INNER JOIN tblTransaction t ON td.intTransactionID = t.intTransactionID
  58.                         INNER JOIN tblBankAccount b ON t.intAccountID = b.intAccountID
  59.                         INNER JOIN tblCase c ON b.intCaseID = c.intCaseID
  60.  
  61.                         GROUP BY c.intDeleted, b.intDeleted, td.intDeleted, t.intDeleted,
  62.                         c.vchTrustee, c.vchCaseNumber, c.intCaseID, td.intTransactionType,
  63.                         t.intTransactionMethod, c.intStatus
  64.  
  65.                         HAVING t.intDeleted = 0 AND td.intDeleted = 0 AND b.intDeleted = 0 AND c.intDeleted = 0
  66.                         AND td.intTransactionType = 200
  67.                         AND (t.intTransactionMethod = 40 OR t.intTransactionMethod = 41 OR t.intTransactionMethod = 43)
  68.                         AND c.vchTrustee = 'RLW' AND c.intStatus = 1
  69.  
  70.                            ) As Trans40
  71.  
  72.                 GROUP BY Trans40.vchTrustee, Trans40.vchCaseNumber
  73.                 ) As Trans ON c1.vchCaseNumber = Trans.vchCaseNumber
  74.  
  75.             GROUP BY trans.vchTrustee, trans.vchCaseNumber, a.intDeleted, c1.intDeleted, trans.TotalReceipts, c1.intStatus
  76.             HAVING a.intDeleted = 0 AND c1.intDeleted = 0 AND trans.vchTrustee = 'RLW' AND c1.intStatus = 1
  77.             ) As AllTotalReceipts
  78.  
  79.         GROUP BY AllTotalReceipts.vchTrustee) As AllCases
  80.  
  81.     LEFT JOIN
  82.  
  83.         (SELECT TOP 9 trans.vchTrustee, trans.vchCaseNumber, Trans.TotalReceipts, SUM(a.mnyBalanceAmount) As totalAssetBalance, 
  84.         Trans.TotalReceipts + SUM(a.mnyBalanceAmount) As Top9Total
  85.  
  86.         FROM tblCase c1
  87.  
  88.             LEFT JOIN tblAsset a ON c1.intCaseID = a.intCaseID
  89.             LEFT JOIN
  90.             (Select trans40.vchTrustee, trans40.vchCaseNumber, SUM(trans40.TdMnyAmount) As TotalReceipts
  91.  
  92.             From
  93.                 (SELECT c.vchTrustee, c.vchCaseNumber, SUM(td.mnyAmount) As TdMnyAmount
  94.                        FROM   tblTransactionDetail td
  95.  
  96.                           INNER JOIN tblTransaction t ON td.intTransactionID = t.intTransactionID
  97.                 INNER JOIN tblBankAccount b ON t.intAccountID = b.intAccountID
  98.                 INNER JOIN tblCase c ON b.intCaseID = c.intCaseID
  99.  
  100.                        GROUP BY c.intDeleted, b.intDeleted, td.intDeleted, t.intDeleted,
  101.                 c.vchTrustee, c.vchCaseNumber, c.intCaseID, td.intTransactionType,
  102.                 t.intTransactionMethod, c.intStatus
  103.  
  104.                 HAVING t.intDeleted = 0 AND td.intDeleted = 0 AND b.intDeleted = 0 AND c.intDeleted = 0
  105.                 AND td.intTransactionType = 200 AND (t.intTransactionMethod = 40 OR t.intTransactionMethod = 41 OR t.intTransactionMethod = 43)
  106.                 AND c.vchTrustee = 'RLW' AND c.intStatus = 1
  107.  
  108.                        ) As Trans40
  109.  
  110.             GROUP BY Trans40.vchTrustee, Trans40.vchCaseNumber
  111.             ) As Trans ON c1.vchCaseNumber = Trans.vchCaseNumber
  112.  
  113.         GROUP BY trans.vchTrustee, trans.vchCaseNumber, a.intDeleted, c1.intDeleted, trans.TotalReceipts, c1.intStatus
  114.  
  115.         HAVING a.intDeleted = 0 AND c1.intDeleted = 0 AND trans.vchTrustee = 'RLW' AND c1.intStatus = 1
  116.         ORDER BY Trans.TotalReceipts + SUM(a.mnyBalanceAmount) DESC
  117.         ) As Top9
  118.  
  119.     ON AllCases.vchTrustee = Top9.vchTrustee
  120.     GROUP By Top9.vchTrustee, AllCases.TR
  121.  
  122.     )
Nov 1 '07 #1
2 1319
ck9663
2,878 Expert 2GB
When I run this query, I get an ORDER BY error "Incorrect syntax near the keyword 'ORDER'. I've bolded it below.

If I take the ORDER by out then it works correctly, but I need the ORDER BY because of the TOP 9. Any Suggestions???

(SELECT TOP 9 trans.vchTrustee, trans.vchCaseNumber, Trans.TotalReceipts + SUM(a.mnyBalanceAmount) As Top9Total

FROM tblCase c1

LEFT JOIN tblAsset a ON c1.intCaseID = a.intCaseID
LEFT JOIN
(Select trans40.vchTrustee, trans40.vchCaseNumber, SUM(trans40.TdMnyAmount) As TotalReceipts

From
(SELECT c.vchTrustee, c.vchCaseNumber, SUM(td.mnyAmount) As TdMnyAmount
FROM tblTransactionDetail td

INNER JOIN tblTransaction t ON td.intTransactionID = t.intTransactionID
INNER JOIN tblBankAccount b ON t.intAccountID = b.intAccountID
INNER JOIN tblCase c ON b.intCaseID = c.intCaseID

GROUP BY c.intDeleted, b.intDeleted, td.intDeleted, t.intDeleted,
c.vchTrustee, c.vchCaseNumber, c.intCaseID, td.intTransactionType,
t.intTransactionMethod, c.intStatus

HAVING t.intDeleted = 0 AND td.intDeleted = 0 AND b.intDeleted = 0 AND c.intDeleted = 0
AND td.intTransactionType = 200
AND (t.intTransactionMethod = 40 OR t.intTransactionMethod = 41 OR t.intTransactionMethod = 43)
AND c.vchTrustee = 'RLW' AND c.intStatus = 1

) As Trans40

GROUP BY Trans40.vchTrustee, Trans40.vchCaseNumber
) As Trans ON c1.vchCaseNumber = Trans.vchCaseNumber

GROUP BY trans.vchTrustee, trans.vchCaseNumber, a.intDeleted, c1.intDeleted, trans.TotalReceipts, c1.intStatus

HAVING a.intDeleted = 0 AND c1.intDeleted = 0 AND trans.vchTrustee = 'RLW' AND c1.intStatus = 1

ORDER BY (Trans.TotalReceipts + SUM(a.mnyBalanceAmount)) DESC)

UNION All

(SELECT Top9.vchTrustee, ' ', (AllCases.TR - SUM(Top9.Top9Total)) As TRMinusTop9
FROM

(SELECT AllTotalReceipts.vchTrustee, SUM(AllTotalReceipts.TotalMoney) As TR
FROM

(SELECT trans.vchTrustee, trans.vchCaseNumber, Trans.TotalReceipts, SUM(a.mnyBalanceAmount) As totalAssetBalance,
(Trans.TotalReceipts + SUM(a.mnyBalanceAmount)) As TotalMoney

FROM tblCase c1

LEFT JOIN tblAsset a ON c1.intCaseID = a.intCaseID
LEFT JOIN
(Select trans40.vchTrustee, trans40.vchCaseNumber, SUM(trans40.TdMnyAmount) As TotalReceipts

From
(SELECT c.vchTrustee, c.vchCaseNumber, SUM(td.mnyAmount) As TdMnyAmount
FROM tblTransactionDetail td
INNER JOIN tblTransaction t ON td.intTransactionID = t.intTransactionID
INNER JOIN tblBankAccount b ON t.intAccountID = b.intAccountID
INNER JOIN tblCase c ON b.intCaseID = c.intCaseID

GROUP BY c.intDeleted, b.intDeleted, td.intDeleted, t.intDeleted,
c.vchTrustee, c.vchCaseNumber, c.intCaseID, td.intTransactionType,
t.intTransactionMethod, c.intStatus

HAVING t.intDeleted = 0 AND td.intDeleted = 0 AND b.intDeleted = 0 AND c.intDeleted = 0
AND td.intTransactionType = 200
AND (t.intTransactionMethod = 40 OR t.intTransactionMethod = 41 OR t.intTransactionMethod = 43)
AND c.vchTrustee = 'RLW' AND c.intStatus = 1

) As Trans40

GROUP BY Trans40.vchTrustee, Trans40.vchCaseNumber
) As Trans ON c1.vchCaseNumber = Trans.vchCaseNumber

GROUP BY trans.vchTrustee, trans.vchCaseNumber, a.intDeleted, c1.intDeleted, trans.TotalReceipts, c1.intStatus
HAVING a.intDeleted = 0 AND c1.intDeleted = 0 AND trans.vchTrustee = 'RLW' AND c1.intStatus = 1
) As AllTotalReceipts

GROUP BY AllTotalReceipts.vchTrustee) As AllCases

LEFT JOIN

(SELECT TOP 9 trans.vchTrustee, trans.vchCaseNumber, Trans.TotalReceipts, SUM(a.mnyBalanceAmount) As totalAssetBalance,
Trans.TotalReceipts + SUM(a.mnyBalanceAmount) As Top9Total

FROM tblCase c1

LEFT JOIN tblAsset a ON c1.intCaseID = a.intCaseID
LEFT JOIN
(Select trans40.vchTrustee, trans40.vchCaseNumber, SUM(trans40.TdMnyAmount) As TotalReceipts

From
(SELECT c.vchTrustee, c.vchCaseNumber, SUM(td.mnyAmount) As TdMnyAmount
FROM tblTransactionDetail td

INNER JOIN tblTransaction t ON td.intTransactionID = t.intTransactionID
INNER JOIN tblBankAccount b ON t.intAccountID = b.intAccountID
INNER JOIN tblCase c ON b.intCaseID = c.intCaseID

GROUP BY c.intDeleted, b.intDeleted, td.intDeleted, t.intDeleted,
c.vchTrustee, c.vchCaseNumber, c.intCaseID, td.intTransactionType,
t.intTransactionMethod, c.intStatus

HAVING t.intDeleted = 0 AND td.intDeleted = 0 AND b.intDeleted = 0 AND c.intDeleted = 0
AND td.intTransactionType = 200 AND (t.intTransactionMethod = 40 OR t.intTransactionMethod = 41 OR t.intTransactionMethod = 43)
AND c.vchTrustee = 'RLW' AND c.intStatus = 1

) As Trans40

GROUP BY Trans40.vchTrustee, Trans40.vchCaseNumber
) As Trans ON c1.vchCaseNumber = Trans.vchCaseNumber

GROUP BY trans.vchTrustee, trans.vchCaseNumber, a.intDeleted, c1.intDeleted, trans.TotalReceipts, c1.intStatus

HAVING a.intDeleted = 0 AND c1.intDeleted = 0 AND trans.vchTrustee = 'RLW' AND c1.intStatus = 1
ORDER BY Trans.TotalReceipts + SUM(a.mnyBalanceAmount) DESC
) As Top9

ON AllCases.vchTrustee = Top9.vchTrustee
GROUP By Top9.vchTrustee, AllCases.TR

)

i believe ORDER BY has to be placed on the outermost QUERY, resulting in a sorted final returned resultset
Nov 1 '07 #2
i believe ORDER BY has to be placed on the outermost QUERY, resulting in a sorted final returned resultset
I tried that and it won't produce the correct order by, because the First query with the TOP 9 isn't being sorted.

So if I put it on the outermost query, it doesn't sort correctly.

Maybe you can help me with my query, its very long and probably too much code. I'm trying to create a query the will SUM a value from two tables and pull the TOP 9 cases. Then also add a TOTAL SUM value - Top 9 Sum Value.

Any help would be great!!
Nov 1 '07 #3

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

Similar topics

5
by: Bernard | last post by:
Hi, I have a problem with a CGI script (Perl) on a Win2000 server. The script is for sending E-cards and was written by Jason Maloney:...
11
by: darrel | last post by:
I'm modyfying an image upload and resizing script that we've had laying around for a long while. I'm getting a NullReferenceException error (see full error at bottom) from this line: Dim g...
7
by: coolindienc | last post by:
I am trying to figure out what is wrong with this code. It says that global variable is not defined. Below is the error and code of the program. I have also highlighted the error place. Please...
0
by: GamingGeek | last post by:
Alright, I'm working on a project for class that I need to use both .gifs and .wavs. I've gotten the .wavs to work, but some of the gifs bring up a GDI+ error when I run the program. Here's the error...
0
by: =?Utf-8?B?dGVzaHRyYWRlQGdtYWlsLmNvbQ==?= | last post by:
Hi All, I am still new to Webservices in .NET Basically, This is what I am trying to accomplish. My Client has given me the soft copy of the WSDL file and XSD Files and has asked me to create...
0
by: shyamsunderrai | last post by:
Dear All, I am having the latest version of PostgreSQL i.e. 8.4.2 and in order to increase the number of client connection I have increased the parameters "max_connections" and "shared_buffers"...
1
by: vikjohn | last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified...
2
by: aszush | last post by:
//Title: Programming Assignment 1 //Version: //Copyright: Copyright (c) 1999 //Author: Andrew //Company: //Description: Computes employee's weekly gross and net pay....
8
by: Jhei | last post by:
int get_value(void), get_rom(void); { /*Turbo C says I have a Declaration Syntax Error on this line.*/ int val, roman; printf("Enter a Roman Numerical number: ");...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.