需要在查询中进行分组

时间:2018-10-12 15:56:09

标签: sql oracle

有人可以帮助我进行此查询,我需要在此查询中应用u.ac.d的分组:

SELECT DISTINCT u.a as ID,DECODE(u.b,'ACT','0','1') as STAT,
                c.d AS role,
                NVL(SUBSTR(c.d, 0, INSTR(c.d, '-')-1), c.d) as app,
                SUBSTR(c.d, INSTR(c.d, ':') + 1) AS role 
  FROM dbo.USER u left outer join dbo.CONNECTION c on u.a = c.a

示例:

我们有两个表userconnection

user中,数据将为:

id. Stat. 
a1  1    
a2  0

connection中,数据将为:

Id  app.    Role
a1  abc-d.  Read
a3  cde-f.  Write
a1  abc-d   Read

因此,我需要基于id进行左外部联接,然后需要基于两个键进行聚合,即Id中的user和{{1}中的role }。

输出:

connection

1 个答案:

答案 0 :(得分:0)

SELECT DISTINCT u.a as ID,DECODE(u.b,'ACT','0','1') as STAT,c.d AS 
role,NVL(SUBSTR(c.d, 0, INSTR(c.d, '-')-1), c.d) as app,SUBSTR(c.d, INSTR(c.d, 
':') + 1) as  role FROM user u ,connection c where u.id=c id group by 
u.a,SUBSTR(c.d, INSTR(c.d, ':') + 1)

您不能在group by子句中拥有列别名