我已将“Monthbase”添加到我的Time表中。现在我想将它添加到Fact表中。 使用左连接:
SELECT t1.[Posting_Date]
,t1.[Quantity]
,t1.[EmployeeID] --added afterwards
,t2.[Monthbase]
FROM [Fact table] t1 LEFT JOIN [Time table] t2
ON t1.[Posting_Date] = t2.[Posting_Date]
但我只希望每天一次看到价值(8),如果适用的话。 已经创建了时间表,其中包含有关假期等的所有要求。
答案 0 :(得分:1)
这样的事情:
;WITH CTE AS
(
SELECT [EmployeeID],
[Posting_Date]
,[Quantity]
,ROW_NUMBER() OVER (PARTITION BY [EmployeeID], [Posting_Date] ORDER BY [Quantity]) RN
FROM [Fact table]
)
SELECT t1.[Posting_Date]
,t1.[EmployeeID]
,t1.[Quantity]
,CASE WHEN t1.RN = 1 THEN t2.[Monthbase] ELSE 0 END [Monthbase]
FROM CTE t1
LEFT JOIN [Time table] t2
ON t1.[Posting_Date] = t2.[Posting_Date]