I have added "Monthbase" to my Time table. Now I want to add it to the Fact table. Using a left join:

 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]

Monthbase

But I only want to see the value (8) one time per day, when applicable. The Time table has already been created with all requirements regarding holidays etc.

有帮助吗?

解决方案

Something like this:

;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]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top