SQL查询组以及案例语句

时间:2017-10-17 04:43:03

标签: sql sql-server

我有三张像bellow一样的表

**tRole**
+--------+----------+-----------+
| RoleID | RoleCode | RoleTitle |
+--------+----------+-----------+
|      1 | Role1    | RT1       |
|      2 | Role2    | RT2       |
|      3 | Role3    | RT3       |
+--------+----------+-----------+

**tEmployee**
+-------+-------+
| EmpID | Name  |
+-------+-------+
|     1 | Emp 1 |
|     2 | Emp 2 |
|     3 | Emp 3 |
+-------+-------+

**tEmployeeRole**

+-------+--------+
| EmpID | RoleID |
+-------+--------+
|     1 |      1 |
|     1 |      2 |
|     2 |      1 |
|     2 |      2 |
|     3 |      3 |
+-------+--------+

我想要一个如下所示的输出,如果一个角色只映射到一个员工,那么员工名称将显示其他明智的多个将显示。

+--------+----------+-----------+----------+
| RoleID | RoleCode | RoleTitle | Employee |
+--------+----------+-----------+----------+
|      1 | Role1    | RT1       | Multiple |
|      2 | Role2    | RT2       | Multiple |
|      3 | Role3    | RT3       | Emp 3    |
+--------+----------+-----------+----------+

我写了下面的查询,但是当我按emp.First_Name分组时,结果是错误的

select cr.RoleCode,cr.RoleID,
case
when count(ear.RoleID)=1 then emp.First_Name
else 'M' end as 'AssignedTO'

 from tRole as cr
left outer join tEmployeeRole as ear on cr.RoleID=ear.RoleID
left outer join tEmployee as  emp on ear.EmployeeID=emp.EmployeeID
group by cr.RoleCode,crRoleID,emp.First_Name

3 个答案:

答案 0 :(得分:1)

您好您可以将此查询用于您的解决方案: 你需要用分区来计算并使用不同的数据

    DECLARE @tRole TABLE (
             RoleID INT
            ,RoleCode VARCHAR(50)
            ,RoleTitle VARCHAR(50)
            )
    DECLARE @tEmployee TABLE (
             EmpID INT
            ,EmpName VARCHAR(50)
            )
    DECLARE @tEmployeeRole TABLE ( EmpID INT, RoleID INT )


    INSERT @tRole ( RoleID, RoleCode, RoleTitle )
        SELECT 1, 'Role1', 'RT1'
        UNION
        SELECT 2, 'Role2', 'RT2'
        UNION
        SELECT 3, 'Role3', 'RT3'


    INSERT @tEmployee ( EmpID, EmpName )
        SELECT 1, 'Epm 1'
        UNION
        SELECT 2, 'Epm 2'
        UNION
        SELECT 3, 'Epm 3'


    INSERT @tEmployeeRole ( EmpID, RoleID )
        SELECT 1, 1
        UNION
        SELECT 1, 2
        UNION
        SELECT 2, 1
        UNION
        SELECT 2, 2
        UNION
        SELECT 3, 3



    SELECT DISTINCT tRole.RoleID
    , RoleCode
    , RoleTitle
    , CASE  WHEN COUNT(tEmployeeRole.EmpID) OVER ( PARTITION BY tEmployee.EmpID ) = 1 THEN tEmployee.EmpName ELSE 'Multiple'END [Employee]
      FROM @tEmployee tEmployee
        LEFT OUTER JOIN @tEmployeeRole tEmployeeRole ON tEmployeeRole.EmpID = tEmployee.EmpID
        LEFT OUTER JOIN @tRole tRole ON tRole.RoleID = tEmployeeRole.RoleID

答案 1 :(得分:0)

您可以修改@Pratik的答案,添加列出员工的列表

;with CTE as(
SELECT 
DISTINCT tRole.RoleID
RoleCode
, RoleTitle
, CASE  WHEN COUNT(tEmployeeRole.EmpID) OVER ( PARTITION BY tEmployee.EmpID ) = 1 THEN tEmployee.EmpName ELSE 'Multiple'END [Employee]
FROM @tEmployee tEmployee
LEFT OUTER JOIN @tEmployeeRole tEmployeeRole ON tEmployeeRole.EmpID = tEmployee.EmpID
LEFT OUTER JOIN @tRole tRole ON tRole.RoleID = tEmployeeRole.RoleID
)
select * 
,stuff( (select ','+EmpName from @tEmployee IE inner join @tEmployeeRole IER on IE.EmpID = IER.EmpID where IER.RoleID = CTE.rolecode for xml PATH('')  ),1,1,'') AS EMList
from CTE 

答案 2 :(得分:0)

此查询可能会帮助您解决问题。试一试

您的论坛数据类似

create TABLE  #tRole (RoleID INT ,RoleCode VARCHAR(50) ,RoleTitle VARCHAR(50) )

create TABLE  #tEmployee (EmpID INT ,EmpName VARCHAR(50) )

create  TABLE #tEmployeeRole( EmpID INT, RoleID INT )


INSERT #tRole ( RoleID, RoleCode, RoleTitle )
SELECT 1, 'Role1', 'RT1'
UNION
SELECT 2, 'Role2', 'RT2'
UNION
SELECT 3, 'Role3', 'RT3'

INSERT #tEmployee ( EmpID, EmpName )
SELECT 1, 'Epm 1'
UNION
SELECT 2, 'Epm 2'
UNION
SELECT 3, 'Epm 3'

INSERT #tEmployeeRole ( EmpID, RoleID )
SELECT 1, 1
UNION
SELECT 1, 2
UNION
SELECT 2, 1
UNION
SELECT 2, 2
UNION
SELECT 3, 3

所需查询

;with cte as 
(
select  tr.roleid,tr.rolecode,tr.roletitle,te.empname
,COUNT(ter.EmpID) OVER ( PARTITION BY ter.EmpID ) as emp_count
from #tEmployee te
inner join #tEmployeeRole tER on tER.empid=te.empid
inner join #tRole tr on tr.roleid=ter.roleid 
)
select distinct RoleID,RoleCode,RoleTitle 
,case when emp_count>1 then 'Multiple' else empname end as Employee 
from cte