我目前有这个命令:
select c.shop, count(*)*c.cost as "Income"
from a
inner join c
on a.courseid = c.courseid
group by c.shop, c.cost
带回一个类似于此的表:
SHOP INCOME
-------------
Shop1 30
Shop2 40
Shop2 25
我希望得到如下结果:
SHOP INCOME
-------------
Shop1 30
Shop2 65
我试过了:
select distinct c.STORE, count(*)*c.FEE as Income, sum(Income) as Store_Income
from attendancecheck a
inner join course c
on a.course_id = c.course_id
group by c.STORE, c.FEE, Income
但我不知道如何参考计算的专栏"收入" - 错误:ORA-00904:"收入":无效标识符
答案 0 :(得分:1)
尝试
select
shop,
sum(income)
from (
select c.shop, count(*)*c.cost as income
from a
inner join c
on a.courseid = c.courseid
group by c.shop, c.cost
)
group by shop
答案 1 :(得分:1)
为了得到你想要的结果,以下就足够了:
SELECT c.shop, SUM(c.cost) AS "Income"
FROM a INNER JOIN c
ON a.courseid = c.courseid
GROUP BY c.shop;
编辑我想我应该解释这个答案,因为我得到了一个downvote,我会尽我所能尝试这样做。但上面的查询将给出与接受的答案相同的结果,并且它相当简单。
假设您有两家商店,Shop1
和Shop2
。 Shop1
i 成本为30,而Shop2
j 成本为40, k 成本为25。 #39; s查询将为Shop1
返回值30 * i ,值为40 * j 和25 * k 为Shop2
。通过将这些结果和商店分组相加,可以得到以下结果:
SHOP INCOME
----------------
Shop1 30*i
Shop2 40*j+25*k
我给出的查询将返回相同的结果! Shop1
的总和成本将返回30 * i ,而Shop2
的总和成本将返回40 * j + 25 * k < / em>的。我很抱歉,我无法严格证明其与已接受的答案的逻辑等同性,但我希望这个例子至少会成为有利的证据。