Oracle中的SUM(COUNT())

时间:2018-04-17 14:39:38

标签: sql oracle count

我正在尝试根据一列的计数乘以连接表的另一列中的数字来确定如何创建列。

以下是我到目前为止所尝试的内容:

尝试1:

select a.course_id, sum(count(*)*c.FEE) as "Income", c.COURSE_NAME,c.STORE,c.VENUE,c.COURSE_LEVEL,c.COURSE_DATE,c.DURATION,c.FEE,c.STAFF 
from attendancecheck a
inner join course c
on a.course_id = c.course_id
group by a.course_id, c.COURSE_NAME,c.STORE,c.VENUE,c.COURSE_LEVEL,c.COURSE_DATE,c.DURATION,c.FEE,c.STAFF 
order by "Income"

我收到错误:ORA-00937:不是单组组功能

尝试2:

select a.course_id, sum(People_on_course * c.Fee) as "Income", c.COURSE_NAME,c.STORE,c.VENUE,c.COURSE_LEVEL,c.COURSE_DATE,c.DURATION,c.FEE,c.STAFF 
from (SELECT COUNT(a.participant_id) as People_on_course
from attendancecheck a)
inner join course c
on a.course_id = c.course_id
group by a.course_id, c.COURSE_NAME,c.STORE,c.VENUE,c.COURSE_LEVEL,c.COURSE_DATE,c.DURATION,c.FEE,c.STAFF
order by "Income"

我收到错误:ORA-00904:“A”。“COURSE_ID”:标识符无效

如果有人可以指导我正确的方向,那就太好了。

干杯, 丹尼尔

3 个答案:

答案 0 :(得分:2)

为什么总结已经计算过的参与者?只需使用count,乘以费用即可。如果没有任何参与者参加任何课程,请应用left join代替join。您还可以使用子查询来计算每门课程的参与者。

<强> demo

select c.course_id, count(a.participant_id) * c.fee as income, c.course_name, 
       c.store, c.venue, c.course_level, c.course_date, c.duration, c.fee, c.staff 
  from course c
  left join attendancecheck a on a.course_id = c.course_id
  group by c.course_id, c.course_name, c.store, c.venue, 
       c.course_level, c.course_date, c.duration, c.fee, c.staff 
  order by income

答案 1 :(得分:0)

首先根据课程表和group by子句计算总和。然后根据课程ID和出勤检查加入。

select
  a.course_id, c."income", c.COURSE_NAME, c.STORE, c.VENUE, c.COURSE_LEVEL,
  c.COURSE_DATE, c.DURATION,c.FEE, c.STAFF
from attendancecheck a,
(
  select
    sum(People_on_course * c.Fee) as "Income",
    c.cource_id, c.COURSE_NAME, c.STORE, c.VENUE, c.COURSE_LEVEL, c.COURSE_DATE,
    c.DURATION, c.FEE, c.STAFF
  from course c
  group by
    c.course_id, c.COURSE_NAME, c.STORE, c.VENUE, c.COURSE_LEVEL, c.COURSE_DATE,
    c.DURATION, c.FEE, c.STAFF
) C
where a.course_id = c.course_id
order by c."Income" ;

答案 2 :(得分:0)

您想要选择课程及其总费用,您需要参加者数量。因此,选择课程并加入每门课程的参与人数:

select
  c.course_id,
  c.fee * a.participants as income,
  c.course_name,
  c.store,
  c.venue,
  c.course_level,
  c.course_date,
  c.duration,
  c.fee,
  c.staff
from course c
join
(
  select course_id, count(*) as participants
  from attendancecheck
  group by course_id
) a on a.course_id = c.course_id
order by income;