我有两张桌子
表1
EmployeeId EmployeeName
1 A
表2
EmployeeId Month
1 Jan
1 Feb
1 Mar
1 Apr
当我根据employeeid应用内部联接时,我会得到这样的结果
EmployeeId EmployeeName Month
1 A Jan
1 A Feb
1 A Mar
1 A Apr
我想从表-1中删除所有重复的列值。我的结果应该是这样的。
EmployeeId EmployeeName Month
1 A Jan
Feb
Mar
Apr
请就此提出任何建议。
答案 0 :(得分:1)
这就是工作:
WITH CTE AS
(
SELECT t1.EmployeeId AS t1EmployeeID, t1.EmployeeName,
t2.EmployeeId AS t2EmployeeID, Month,
RN = ROW_NUMBER() OVER (Partition BY t1.EmployeeId
ORDER BY MONTH(t2.Month + ' 1 2015'))
FROM Table2 t2
LEFT OUTER JOIN Table1 t1
ON t1.EmployeeID = t2.EmployeeID
)
SELECT EmployeeId = CASE WHEN RN = 1
THEN t1EmployeeID ELSE NULL END,
EmployeeName = CASE WHEN RN = 1
THEN EmployeeName ELSE NULL END,
Month
FROM CTE
棘手的部分是按月缩写的月份名称。