Oracle 9.i SELECT FROM 2个表和3条记录生成一条记录

时间:2018-12-03 08:11:34

标签: sql oracle oracle9i

我有以下查询(查询1):

SELECT AVG(a.nnilai)*75/100 
FROM trangketmhs a WHERE cthajar='20172018' AND a.csmt='E' AND a.kddos='00084'

查询1的结果: click for preview

(查询2):

    SELECT t.nbobot FROM TRNILAIKINERJA t 
WHERE t.ckddos='00084' AND t.cthajar='20172018' AND t.csmt='E' AND t.ckdnilai='TLT'

查询2的结果: click for preview

(查询3):

SELECT p.nbobot FROM TRNILAIKINERJA p 
WHERE p.ckddos='00084' AND p.cthajar='20172018' AND p.csmt='E' AND p.ckdnilai='PKM'

查询3的结果: click for preview

到目前为止,我已经运行了以下查询:

SELECT AVG(a.nnilai)*75/100,
(SELECT t.nbobot FROM TRNILAIKINERJA t WHERE t.ckddos='00084' AND t.cthajar='20172018' AND t.csmt='E' AND t.ckdnilai='TLT'),
(SELECT p.nbobot FROM TRNILAIKINERJA p WHERE p.ckddos='00084' AND p.cthajar='20172018' AND p.csmt='E' AND p.ckdnilai='PKM')
 FROM trangketmhs a WHERE cthajar='20172018' AND a.csmt='E' AND a.kddos='00084'

但是结果是

  

ORA-00937: not a single-group group function

查询中的完整记录(*):

SELECT * 
FROM trangketmhs a 
WHERE cthajar='20172018' 
  AND a.csmt='E' 
  AND a.kddos='00084'

click for preview

(2) and (3) 

SELECT * 
FROM TRNILAIKINERJA t 
WHERE t.ckddos='00084' 
  AND t.cthajar='20172018' 
  AND t.csmt='E' 

click for preview

我的问题

如何使3个查询的结果成为此输出:

| AVG(a.nnilai)*75/100  | NBOBOT1  | NBOBOT2 |
|-----------------------|----------|---------|
|     2,58818181818182  |     0.6  |     0.4 |

3 个答案:

答案 0 :(得分:0)

也许我遗漏了一些东西,但是您对trangketmhs(= a)的查询与对TRNILAIKINERJA(t和p)的查询之间似乎没有任何关系。

所以我认为您可以执行以下操作:

SELECT a.the_average, t.nbobot1, p.nbobot2 
FROM (SELECT AVG(a.nnilai)*75/100 the_average
  FROM trangketmhs a 
  WHERE a.cthajar='20172018' 
  AND a.csmt='E' 
  AND a.kddos='00084') a
CROSS JOIN (SELECT t.nbobot nbobot1 
  FROM TRNILAIKINERJA t 
  WHERE t.ckddos='00084' 
  AND t.cthajar='20172018' 
  AND t.csmt='E' 
  AND t.ckdnilai='TLT') t
CROSS JOIN (SELECT p.nbobot nbobot2 
  FROM TRNILAIKINERJA p 
  WHERE p.ckddos='00084' 
  AND p.cthajar='20172018' 
  AND p.csmt='E' 
  AND p.ckdnilai='PKM') p

答案 1 :(得分:0)

比我以前的答案更简单的方法:

 123455
 123456
 123457
 123458
0123456
 aaabcc

答案 2 :(得分:0)

您可以尝试将联接表用作

create table trangketmhs(nnilai float,cthajar varchar2(8),csmt varchar2(1),kddos varchar2(8));
insert into trangketmhs values(4.001818181818186,'20172018','E','00084');
insert into trangketmhs values(2.9              ,'20172018','E','00084');

create table trnilaikinerja( ckddos varchar2(8), cthajar varchar2(8), csmt varchar2(1), ckdnilai varchar2(3));
insert into trnilaikinerjavalues('00084','20172018','E','TLT');
insert into trnilaikinerjavalues('00084','20172018','E','PKM');

select avg(t1.nnilai)*75/100 avg_nnilai,
       max(decode(t2.ckdnilai,'TLT',.6)) nbobot1,
       max(decode(t2.ckdnilai,'TLT',.4)) nbobot2 
  from trangketmhs t1
  join trnilaikinerja t2 
    on t1.cthajar = t2.cthajar
   and t1.csmt = t2.csmt
   and t1.kddos = t2.ckddos;

   AVG_NNILAI      NBOBOT1  NBOBOT2
----------------   -------  --------
2,58818181818182     0,6      0,4