创建索引视图

发布于 2024-10-03 01:38:41 字数 837 浏览 2 评论 0原文

当我尝试创建索引视图时,出现错误“datepart 返回不确定性值,使用系统定义的确定性函数或使用户定义的函数返回确定性值” 当我尝试在视图上创建索引时,出现此错误

。查询是

CREATE VIEW dbo.two_weeks_performance WITH SCHEMABINDING
as 
  SELECT dbo.day_dim.date_time AS Date, dbo.order_dim.quantity AS Target_Acheived
    FROM dbo.day_dim 
    JOIN dbo.order_fact ON dbo.day_dim.day_id = dbo.order_fact.day_id 
    JOIN dbo.branch_dim ON dbo.order_fact.branch_id = dbo.branch_dim.branch_id 
    JOIN dbo.order_dim ON dbo.order_fact.order_id = dbo.order_dim.order_id
   WHERE (DATEPART(wk, CONVERT(datetime, dbo.day_dim.date_time, 101)) <= DATEPART(wk, GETDATE() - 2))
GROUP BY dbo.order_dim.quantity, dbo.day_dim.date_time
GO
CREATE UNIQUE CLUSTERED INDEX two_weeks_performance_I on two_weeks_performance (Date,Target_Acheived)`

在这个date_time是varchar(30)类型。给我一个解决方案来解决这个问题。

When I try to create indexed view I am getting an error "datepart returns indeterministic value, use system defined deterministic function or make the user defined function to return deterministic value" I get this error when i try to create index on the view..

The query is

CREATE VIEW dbo.two_weeks_performance WITH SCHEMABINDING
as 
  SELECT dbo.day_dim.date_time AS Date, dbo.order_dim.quantity AS Target_Acheived
    FROM dbo.day_dim 
    JOIN dbo.order_fact ON dbo.day_dim.day_id = dbo.order_fact.day_id 
    JOIN dbo.branch_dim ON dbo.order_fact.branch_id = dbo.branch_dim.branch_id 
    JOIN dbo.order_dim ON dbo.order_fact.order_id = dbo.order_dim.order_id
   WHERE (DATEPART(wk, CONVERT(datetime, dbo.day_dim.date_time, 101)) <= DATEPART(wk, GETDATE() - 2))
GROUP BY dbo.order_dim.quantity, dbo.day_dim.date_time
GO
CREATE UNIQUE CLUSTERED INDEX two_weeks_performance_I on two_weeks_performance (Date,Target_Acheived)`

In this date_time is varchar(30) type. Give me a solution to over come this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她如夕阳 2024-10-10 01:38:41

使用:

CREATE VIEW dbo.two_weeks_performance WITH SCHEMABINDING
AS 
  SELECT dbo.day_dim.date_time AS Date, 
         dbo.order_dim.quantity AS Target_Acheived
    FROM dbo.day_dim 
    JOIN dbo.order_fact ON dbo.day_dim.day_id = dbo.order_fact.day_id 
    JOIN dbo.branch_dim ON dbo.order_fact.branch_id = dbo.branch_dim.branch_id 
    JOIN dbo.order_dim ON dbo.order_fact.order_id = dbo.order_dim.order_id   
GROUP BY dbo.order_dim.quantity, dbo.day_dim.date_time

问题是 GETDATE() 的使用;它是不确定的,每次都会返回不同的值。

所以你必须使用:

SELECT t.*
  FROM dbo.two_weeks_performance t
 WHERE DATEPART(wk, CONVERT(datetime, t.date, 101)) <= DATEPART(wk, GETDATE() - 2)

Use:

CREATE VIEW dbo.two_weeks_performance WITH SCHEMABINDING
AS 
  SELECT dbo.day_dim.date_time AS Date, 
         dbo.order_dim.quantity AS Target_Acheived
    FROM dbo.day_dim 
    JOIN dbo.order_fact ON dbo.day_dim.day_id = dbo.order_fact.day_id 
    JOIN dbo.branch_dim ON dbo.order_fact.branch_id = dbo.branch_dim.branch_id 
    JOIN dbo.order_dim ON dbo.order_fact.order_id = dbo.order_dim.order_id   
GROUP BY dbo.order_dim.quantity, dbo.day_dim.date_time

The issue was the use of GETDATE(); it's non-deterministic, returning a different value every time.

So you'd have to use:

SELECT t.*
  FROM dbo.two_weeks_performance t
 WHERE DATEPART(wk, CONVERT(datetime, t.date, 101)) <= DATEPART(wk, GETDATE() - 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文