2017-10-17 125 views
-3

我有三個表像波紋管SQL查詢組由case語句

**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 

回答

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 
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 
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