I’ve run into a syntax issue with SQL. What I’m trying to do here is add together all of the amounts paid on each order (paid each) an then only select those that are greater than sum of of paid each for a specific order# (1008). I’ve been trying to move around lots of different things here and I’m not having any luck.
This is what I have right now, though I’ve had many different things. Trying to use this simply returns an SQL statement not ended properly error. Any help you guys could give would be greatly appreciated. Do I have to use DISTINCT anywhere here?
SELECT ORDER#,
TO_CHAR(SUM(PAIDEACH), '$999.99') AS "Amount > Order 1008"
FROM ORDERITEMS
GROUP BY ORDER#
WHERE TO_CHAR > (SUM (PAIDEACH))
WHERE ORDER# = 1008;
Some versions of SQL regard the hash character (
#) as the beginning of a comment. Others use double hyphen (--) and some use both. So, my first thought is that yourORDER#field is named incorrectly (though I can’t imagine the engine would let you create a field with that name).You have two
WHEREkeywords, which isn’t allowed. If you have multipleWHEREconditions, you must link them together using boolean logic, withANDandORkeywords.You have your
WHEREcondition afterGROUP BYwhich should be reversed. SpecifyWHEREconditions beforeGROUP BY.One of your
WHEREconditions makes no sense.TO_CHAR > (SUM(paideach)): TO_CHAR() is a function which as far as I know is an Oracle function that converts numeric values to strings according to a specified format. The equivalent in SQL Server isCASTorCONVERT.I’m guessing that you are trying to write a query that finds orders with amounts exceeding a particular value, but it’s not very clear because one of your
WHEREconditions specifies that the order number should be 1008, which would presumably only return one record.The query should probably look more like this:
This would select records from the
orderitemstable where the sum ofpaideachexceeds 999.99.I’m not sure how order 1008 fits into things, so you will have to elaborate on that.