如何制作DISTINCT CONCAT声明?

时间:2016-09-22 13:32:24

标签: sql oracle

此处的新SQL开发人员,如何制作DISTINCT CONCAT声明?

这是我没有DISTINCT键的声明:

COLUMN Employee FORMAT a25;
SELECT CONCAT(CONCAT(EMPLOYEEFNAME, ' '), EMPLOYEELNAME) AS "Employee", JOBTITLE "Job Title" 
FROM Employee
ORDER BY EMPLOYEEFNAME;

这是输出:

Employee                  Job Title               
------------------------- -------------------------
Bill Murray               Cable Installer          
Bill Murray               Cable Installer          
Bob Smith                 Project Manager          
Bob Smith                 Project Manager          
Frank Herbert             Network Specilist        
Henry Jones               Technical Support        
Homer Simpson             Programmer               
Jane Doe                  Programmer               
Jane Doe                  Programmer               
Jane Doe                  Programmer               
Jane Fonda                Project Manager          
John Jameson              Cable Installer          
John Jameson              Cable Installer          
John Carpenter            Technical Support        
John Carpenter            Technical Support        
John Jameson              Cable Installer          
John Carpenter            Technical Support        
John Carpenter            Technical Support        
Kathy Smith               Network Specilist        
Mary Jane                 Project Manager          
Mary Jane                 Project Manager          

 21 rows selected 

如果我使用DISTINCT密钥,我应该只选择11行 如果我使用SELECT DISTINCT CONCAT,我会收到错误。

1 个答案:

答案 0 :(得分:3)

一种选择是使用GROUP BY

SELECT CONCAT(CONCAT(EMPLOYEEFNAME, ' '), EMPLOYEELNAME) AS "Employee",
       JOBTITLE AS "Job Title" 
FROM Employee
GROUP BY CONCAT(CONCAT(EMPLOYEEFNAME, ' '), EMPLOYEELNAME),
         JOBTITLE
ORDER BY "Employee"

如果您真的想使用DISTINCT,另一个选项是查询您当前的查询:

SELECT DISTINCT t.Employee,
                t."Job Title"
FROM
(
    SELECT CONCAT(CONCAT(EMPLOYEEFNAME, ' '), EMPLOYEELNAME) AS "Employee",
           JOBTITLE AS "Job Title" 
    FROM Employee
) t