创建干净的Oracle SQL视图,而不在每行中重复相同的信息

时间:2018-06-04 20:19:11

标签: sql oracle oracle-sqldeveloper

我创建了一个Oracle SQL视图,如下所示:

John Strange  |  Carnaby Street, S9 8EU | CS5 Degree | Sociology   
John Strange  |  Carnaby Street, S9 8EU | CS5 Degree | Religious Studies  
John Strange  |  Carnaby Street, S9 8EU | CS5 Degree | Environmental Studies  
John Strange  |  Carnaby Street, S9 8EU | CS5 Degree | Management  
John Strange  |  Carnaby Street, S9 8EU | CS5 Degree | Business Intelligence

为了只显示名称,地址和学位名称,您会建议使用一次?我使用过DISTINCT,但它不起作用。

3 个答案:

答案 0 :(得分:2)

为什么它不起作用你只是排除其他列,因为distinct将适用于你所表达的所有列/表达式。

select distinct displayname, address, degree 
from <view> v1;

例如,最后一列具有不同的值。因此,如果您在select语句中包含distinct,则无效。

答案 1 :(得分:1)

http://sqlfiddle.com/#!4/0c113/3/0

您可以按如下方式定义视图查询 -

设置 -

select name, address, degree, listagg(Subject,' , ') within group (order by subject) as Subjects
from Table1
group by name, address, degree

查询 -

NAME            ADDRESS                 DEGREE      SUBJECTS
John Strange    Carnaby Street, S9 8EU  CS5 Degree  Business Intelligence , Environmental Studies , Management , Religious Studies , Sociology

输出 -

mysqli_set_charset($conn_i, "utf8");
mb_internal_encoding('UTF-8');

答案 2 :(得分:0)

或者,您可能希望显示值列表,而不是删除第四列。您可以使用LISTAGG,但请记住它有4000个字符的限制。

select full_name, address, program, 
    listagg(course, ', ') within group (order by course)
from my_view
group by full_name, address, program;