所以我有三个表包含有关用户及其部门的数据,我需要创建一个查询来组织它们之间的数据。第一个表包含用户'名称及其ID,如下所示:
user_name user_id
-----------------------
bob 1
chuck 2
bill 3
下一个表将用户的部门ID编入索引,如下所示:
user_id department_id
-----------------------------
1 x
1 y
2 x
3 z
2 z
2 y
,最后一个表根据ID标识部门名称:
department_id department_name
-------------------------------
x research
y development
z advertising
如何编写一个列出所有用户及其所在部门的查询?看起来应该是这样的:
user_name user_id department_1 department_2 department_3
----------------------------------------------------------------------------
bob 1 research development
chuck 2 research development advertising
bill 3 advertising
我尝试过很多不同的方法,而且找不到能给我这个结果的东西。我的输出目前列出了重复用户,如果他们有多个部门,如下所示:
user_name user_id department_1 department_2 department_3
----------------------------------------------------------------------------
bob 1 research research research
bob 1 development development development
chuck 2 research research research
chuck 2 development development development
chuck 3 advertising advertising advertising
bill 3 advertising advertising advertising
当前代码:
WITH DATA AS
(
SELECT
Users.user_name AS Name,
Users.user_id AS Id,
Departments.department_name AS Department1,
Departments.department_name AS Department2,
Departments.department_name AS Department3
FROM Users
JOIN Department_Index ON Users.user_id = Department_Index.user_id
JOIN Departments ON Departments.department_id = Department_Index.department_id
WHERE Departments.department_id = Department_Index.department_id
)
SELECT
Name,Id,Department1,Department2,Department3
FROM DATA
我也试过这样的事情
WITH DATA AS
(
SELECT
Users.user_name AS Name,
Users.user_id AS Id,
( SELECT Departments.department_name
FROM Departments
JOIN Department_Index ON Department_Index.user_id = Users.user_id
WHERE Departments.department_name = Department_Index.department_name
) AS Departments
FROM Users
)
SELECT
Name,Id,Departments[0],Departments[1],Departments[2]
FROM DATA
但那也不起作用。
感谢任何帮助!
答案 0 :(得分:0)
在你告诉我们表名之前我写了这个。
关键是使用row_number()窗口函数对部门名称进行排序,然后再加入此CTE 3次。
with ud_numbered as
(
select u.user_id, ud.department_id, d.department_name
row_number() over (partition by u.user_id order by ud.department_id asc) as dept_no
from user_table u
left join user_departement ud on u.user_id = ud1.user_id
left join department d on ud.department_id = d.department_id
)
select u.user_id, u.user_name, n1.department_name as department_1,
n2.department_name as department_2,
n3.department_name as department_3
from user_table u
left join ud_numbered n1 on u.user_id = n1.user_id and n1.dept_no = 1
left join ud_numbered n2 on u.user_id = n2.user_id and n2.dept_no = 2
left join ud_numbered n3 on u.user_id = n3.user_id and n3.dept_no = 3
答案 1 :(得分:0)
我个人会使用ROW_NUMBER()
和PIVOT
来解决这个问题。 ROW_NUMBER()
将为每个部门提供每个人的唯一标识符,从1开始,然后您可以使用此标识符进行转动。在这种情况下使用的聚合函数MAX
是任意的,因为如上所述,标识符无论如何都是唯一的,因此您将获取单个值的MAX
:
WITH UserData AS
( SELECT u.[user_id],
u.[user_name],
d.department_name,
RowNumber = ROW_NUMBER() OVER(PARTITION BY u.[user_id] ORDER BY d.department_id)
FROM Users AS u
INNER JOIN Department_Index AS di
ON di.user_id = u.user_id
INNER JOIN Departments AS d
ON d.department_id = di.department_id
)
SELECT pvt.[user_id],
pvt.[user_name],
Department1 = pvt.[1],
Department2 = pvt.[2],
Department3 = pvt.[3]
FROM Data AS d
PIVOT
( MAX(department_name)
FOR RowNumber IN ([1], [2], [3])
) AS pvt;