Use a sub-query that returns the monetary values of all orders that have discounts -- greater than 15% -- List the orderid
and the order value this last with the highest value at the top.
I keep getting an error message.
USE Northwind
GO
SELECT
SUM(od.orderid) As OrderID,
AS [Order Values]
FROM
[Order Details] od
WHERE
od.Discount = (SELECT od.Discount
FROM [Order Details] od
GROUP BY od.discount
HAVING od.discount >.15)
GROUP BY
od.quantity, od.discount, od.UnitPrice
ORDER BY
[Order Values] ASC;
Error is:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'GROUP'.
Here is your query:
SELECT SUM(od.orderid) As OrderID,
AS [Order Values]
FROM [Order Details] od
WHERE od.Discount = (SELECT od.Discount
FROM [Order Details] od
GROUP BY od.discount
HAVING od.discount >.15
)
GROUP BY od.quantity, od.discount, od.UnitPrice
ORDER BY [Order Values] ASC;
It has at least two syntactic errors. The AS [Order Values]
is just lingering out there. Incorrectly. Plus, you have =
to a subquery that is likely to return multiple rows. Even after you fix these problems, the query is not going to do what you want, I don't think.
EDIT:
THe query that you want might be:
SELECT od.orderid, sum( od.quantity * od.UnitPrice * (1 - od.discount)) as value
FROM [Order Details] od
GROUP BY od.orderid
HAVING sum(case when od.discount > 0.15 then 1 else 0 end) > 0
ORDER BY value desc;
The following lines have problem: SELECT SUM(od.orderid) As OrderID, AS [Order Values]
There is nothing specified before AS in the 2nd line. Either you have missed a column name before as or you need to remove the "AS OrderID," One additional point: Change the = operator of WHERE clause to IN. Tis is to avoid error if your subquery returns multiple values.
Your query will look like this: USE Northwind GO
SELECT SUM(od.orderid) AS [Order Values] FROM [Order Details] od WHERE od.Discount IN (SELECT od.Discount FROM [Order Details] od GROUP BY od.discount HAVING od.discount >.15) GROUP BY od.quantity, od.discount, od.UnitPrice ORDER BY [Order Values] ASC;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.