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
-
-
)
2 1319
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
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!!
Sign in to post your reply or Sign up for a free account.
Similar topics
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:...
|
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...
|
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...
|
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...
|
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...
|
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"...
|
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...
|
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....
|
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: ");...
|
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...
|
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...
|
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...
|
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...
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
| | |