Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按给定字段对多重SQL结果进行分组_Sql_Database_Oracle_Database Schema - Fatal编程技术网

按给定字段对多重SQL结果进行分组

按给定字段对多重SQL结果进行分组,sql,database,oracle,database-schema,Sql,Database,Oracle,Database Schema,如何按员工对以下查询的结果进行分组 好的,我有三张桌子: 1.包含映射到公司id(company.id)的员工id(company\u employee.employee)的公司员工。它还包含一个雇员角色 包含员工名字、姓氏的员工 包含id和名称的公司 现在,我正在检索具有特定角色的所有员工,但我会为具有不同公司映射的同一员工获取多个条目。大概是这样的: 我的问题是: SELECT company_employee.employee, company_employee.company, empl

如何按员工对以下查询的结果进行分组

好的,我有三张桌子: 1.包含映射到公司id(company.id)的员工id(company\u employee.employee)的公司员工。它还包含一个雇员角色

  • 包含员工名字、姓氏的员工

  • 包含id和名称的公司

  • 现在,我正在检索具有特定角色的所有员工,但我会为具有不同公司映射的同一员工获取多个条目。大概是这样的: 我的问题是:

    SELECT company_employee.employee, company_employee.company, employee.fname, employee.lname, company.name
     FROM company_employee
     JOIN employee on employee.id = company_employee.employee
     JOIN company on company.id = company_employee.company
     WHERE company_employee.role= 185;
    
    我的结果是:

    company_employee.employee     company_employee.company     employee.fname     employee.lname    company.name
    111                           100                          John               Smith             Super Candy
    111                           101                          John               Smith             Red Ballons
    222                           102                          Kevin              Lora              Super Computers
    111                           103                          John               Smith             Star Events
    222                           104                          Kevin              Lora              Vintage Pencils
    333                           105                          Margarett          Bush              Top Security
    
    我想得到的是这样一个列表:

    Employee 111 John Smith mapped to a list of companies (100, 101, 103)
    Employee 222 Kevin Lora mapped to a list of companies (102, 104)
    Employee 333 Margarett Bush mapped to a list of companies (105)
    

    这可能吗?

    似乎您正在寻找以下功能:

    SELECT   company_employee.employee, employee.fname, employee.lname, 
             LISTAGG(company_employee.company) 
               WITHIN GROUP (ORDER BY company_employee.company),
             LISTAGG(company.name) 
               WITHIN GROUP (ORDER BY company_employee.company),
    FROM     company_employee
    JOIN     employee ON employee.id = company_employee.employee
    JOIN     company ON company.id = company_employee.company
    WHERE    company_employee.role = 185
    GROUP BY company_employee.employee, employee.fname, employee.lname
    

    似乎您正在寻找以下功能:

    SELECT   company_employee.employee, employee.fname, employee.lname, 
             LISTAGG(company_employee.company) 
               WITHIN GROUP (ORDER BY company_employee.company),
             LISTAGG(company.name) 
               WITHIN GROUP (ORDER BY company_employee.company),
    FROM     company_employee
    JOIN     employee ON employee.id = company_employee.employee
    JOIN     company ON company.id = company_employee.company
    WHERE    company_employee.role = 185
    GROUP BY company_employee.employee, employee.fname, employee.lname
    

    你能分享一下你试图从这些数据中得到的确切结果吗?我不想把实际的客户数据放在这里。我不想以多个员工条目结束。我希望每个员工都有一个条目,但每个条目都包含其关联公司的列表。您能分享您尝试获取此数据的确切结果吗?我不想将实际的客户数据放在这里。我不想以多个员工条目结束。我希望每个员工都有一个条目,但每个条目都包含其关联公司的列表。