0
我想創建索引視圖MyView的這樣的T-SQL索引視圖和T-SQL在SQL Server中得到相同的結果DISTINCT 2008 R2
Select
o.Theme_ID as Theme_ID,
DATEADD(day, DATEDIFF(day, 0, o.Object_CreationDate), 0) as Objext_CreationDate,
Count_BIG(*) as ObjectCount, o.Object_Tonality from [dbo].Object o
inner join [dbo].Theme t on o.Theme_ID = t.Theme_ID
inner join [dbo].[ThemeWorkplace] tw on t.Theme_ID = tw.Theme_ID
inner join [dbo].Workplace w on w.Workplace_ID = tw.Workplace_ID
... where t.Theme_DeletedMark = 0
AND (w.Workplace_AccessType = 1 OR w.Workplace_AccessType = 8)
AND Object_DeletedMark = 0 ...
Group BY o.Theme_ID,o.Object_Tonality, DATEADD(day, DATEDIFF(day, 0, o.Object_CreationDate), 0)
這個T-SQL工作正常,並允許請在MyView上設置聚簇索引。
問題是表ThemeWorkplace
包含幾個記錄,其中有相同的Theme_ID
。 甚至我使用GROUP BY
- 我得到的Object_Count
值
等於:(真實Object_Count
值)*計數(Theme_ID
在ThemeWorkplace
)。
我不能在t-sql中使用DISTINCT
這個詞,因爲在這種情況下,不可能在視圖上創建索引。
在我看來,什麼建議可以獲得正確的結果?