简体   繁体   English

SQL子查询协助

[英]SQL sub-query assistance

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. 使用子查询,返回有折扣的所有订单的货币价值-超过15% -这样的名单orderid和订单价值这最后在顶部的最高值。

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 消息156,第15层,状态1,第2行
Incorrect syntax near the keyword 'AS'. 关键字“ AS”附近的语法不正确。
Msg 156, Level 15, State 1, Line 9 消息156,第15层,状态1,第9行
Incorrect syntax near the keyword 'GROUP'. 关键字“ 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. AS [Order Values]一直在那儿徘徊。 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] 以下几行有问题:SELECT SUM(od.orderid)作为OrderID,AS [订单值]

There is nothing specified before AS in the 2nd line. 在第二行中,在AS之前没有指定任何内容。 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. 或者您之前错过了列名,或者您需要删除“ AS OrderID”,还有一点:将WHERE子句的=运算符更改为IN。 Tis is to avoid error if your subquery returns multiple values. 如果子查询返回多个值,则这是为了避免错误。

Your query will look like this: USE Northwind GO 您的查询将如下所示: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; 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 [订单值] ASC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM