我只需点击一下即可完成任务,所以任何帮助都会很棒我有这个我在Oracle中编写的sql代码:
SELECT DISTINCT
O.shipcountry, S.companyname,
O.orderid,
TO_CHAR(O.freight, '$999,999.999'),
TO_CHAR(sum(unitprice)*count(quantity), '$999,999.99') as "Order Total"
FROM corp.Order_Details D
LEFT JOIN corp.Orders O ON D.orderid = O.orderid)
LEFT JOIN corp.Shippers S ON O.shipvia = S.Shipperid
WHERE O.freight >= '500'
GROUP BY O.shipcountry, S.companyname, O.orderid, to_char(O.freight, '$999,999.999');
这基本上给了我一个orderid,运到的国家,托运人名称,订单总额以及收取的运费总额。
如果不太复杂,有没有人知道我如何写出运费的百分比(运费/订单总额)?我试图把它放进去,但它说它不能被分组,因为总和的总和。如果有人对我应该如何以及在哪里写这一行有任何想法,我真的非常感谢。
答案 0 :(得分:1)
因此...
SELECT DISTINCT
O.shipcountry,
S.companyname,
O.orderid,
TO_CHAR(O.freight, '$999,999.999'),
TO_CHAR(SUM(unitprice) * COUNT(quantity), '$999,999.99') as "Order Total",
O.freight / (SUM(unitprice) * COUNT(quantity)) as "Percent"
...
......不起作用?
答案 1 :(得分:0)
我认为您的意思是您添加了O.freight / (SUM(unitprice) * COUNT(quantity))
之类的内容并获得了ORA-00979: not a GROUP BY expression
。
原因是O.freight
不是您的GROUP BY表达式之一 - 您按to_char(O.freight, '$999,999.999')
进行分组。您应该能够单独按列值进行分组并使其有效。
SELECT DISTINCT
O.shipcountry, S.companyname,
O.orderid,
TO_CHAR(O.freight, '$999,999.999'),
TO_CHAR(sum(unitprice)*count(quantity), '$999,999.99') as "Order Total",
O.freight / (SUM(unitprice) * COUNT(quantity)) as "Percent"
FROM corp.Order_Details D
LEFT JOIN corp.Orders O ON D.orderid = O.orderid)
LEFT JOIN corp.Shippers S ON O.shipvia = S.Shipperid
WHERE O.freight >= '500'
GROUP BY O.shipcountry, S.companyname, O.orderid, O.freight;
我怀疑一个常见的误解是GROUP BY子句中的表达式必须与SELECT子句中未分组的表达式完全匹配。按实际值分组就足够了 - 您仍然可以在选择中将功能应用于它们。