在select中计算错误组合

时间:2012-08-03 13:29:14

标签: sql group-by

我有一个问题。

这是查询:

SELECT a.naam as "Artiest", SUM(o.numberOfOrders) as "Aantal singles",   
(SUM(o.numberOfOrders) * s.price) * 0.35 as "Winst  

FROM orders o
JOIN singles s ON s.singleid = b.singleid
JOIN artist a ON a.artistid = s.artistid
WHERE b.date= TO_DATE('15/MAY/2012','dd/mon/yyyy')
GROUP BY a.name

当我执行此查询时,我收到错误:

  

“不是GROUP BY表达式”

我已经尝试将(SUM(o.numberOfOrders)* s.price)* 0.35 添加到组中了,
但后来我得到了错误:

  

“此处不允许使用群组功能”

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

您希望将其替换为以下之一:

sum(o.numberOfOrders*s.price)*0.35

或:

sum(o.numberOfOrdrs)*avg(s.price)*0.35

或(也许):

sum(o.numberOfOrdrs)*max(s.price)*0.35

换句话说,s.price不在

组中

答案 1 :(得分:1)

你错过了一个双引号。试试这个:

SELECT 
    a.naam as "Artiest", 
    SUM(o.numberOfOrders) as "Aantal singles",   
    (SUM(o.numberOfOrders) * s.price) * 0.35 as "Winst"
FROM 
    orders o
        JOIN singles s ON s.singleid = b.singleid
        JOIN artist a ON a.artistid = s.artistid
WHERE 
    b.date= TO_DATE('15/MAY/2012','dd/mon/yyyy')
GROUP BY a.name