通用表表达式问题
为了计算聚类系数,我创建了以下查询:
--calculate embeddedness and local_gatekepping_role_count (for two-way connections)
declare @observedUser varchar(50)
declare @clusteringCoefficient decimal(18, 7)
declare @possibleConnectionsCount int
declare @neighborConnectionsCount int
declare @rowsAffected int
SET @rowsAffected = 1;
WHILE @rowsAffected > 0
BEGIN
SELECT TOP 1 @observedUser = msisdn FROM users_in_group_91 WHERE clustering_coefficient IS NULL
SET @rowsAffected = @@ROWCOUNT
IF @rowsAffected = 0 break;
;WITH neighbors AS
(
SELECT called_party AS neighbor FROM connections_in_group_91 WHERE calling_party = @observedUser
UNION
SELECT calling_party AS neighbor FROM connections_in_group_91 WHERE called_party = @observedUser
),
possibleConnections AS
(
SELECT n1.neighbor AS user1, n2.neighbor AS user2 FROM neighbors n1 CROSS JOIN neighbors n2 WHERE n1.neighbor < n2.neighbor
)
SELECT @clusteringCoefficient = CAST(COUNT(*) AS decimal)/(SELECT COUNT(*) FROM possibleConnections) FROM possibleConnections pc INNER JOIN connections_in_group_91 c91 ON c91.calling_party = pc.user1 AND c91.called_party = pc.user2
)
UPDATE users_in_group_91 SET clustering_coefficient = @clusteringCoefficient WHERE msisdn = @observedUser
END
效果很好。因为这对检查观察到的用户扮演网守角色的次数我尝试按以下方式修改上面的查询:
declare @observedUser varchar(50)
declare @clusteringCoefficient decimal(18, 7)
declare @gatekeepingRoleCount int
declare @possibleConnectionsCount int
declare @neighborConnectionsCount int
declare @rowsAffected int
SET @rowsAffected = 1;
WHILE @rowsAffected > 0
BEGIN
SELECT TOP 1 @observedUser = msisdn FROM users_in_group_91 WHERE clustering_coefficient IS NULL
SET @rowsAffected = @@ROWCOUNT
IF @rowsAffected = 0 break;
;WITH neighbors AS
(
SELECT called_party AS neighbor FROM connections_in_group_91 WHERE calling_party = @observedUser
UNION
SELECT calling_party AS neighbor FROM connections_in_group_91 WHERE called_party = @observedUser
),
possibleConnections AS
(
SELECT n1.neighbor AS user1, n2.neighbor AS user2 FROM neighbors n1 CROSS JOIN neighbors n2 WHERE n1.neighbor < n2.neighbor
)
SELECT @possibleConnectionsCount = COUNT(*) FROM possibleConnections
SELECT @clusteringCoefficient = CAST(COUNT(*) AS decimal)/(@possibleConnectionsCount) FROM possibleConnections pc INNER JOIN connections_in_group_91 c91 ON c91.calling_party = pc.user1 AND c91.called_party = pc.user2
SET @gatekeepingRoleCount = @possibleConnectionsCount * (1 - @clusteringCoefficient)
UPDATE users_in_group_91 SET clustering_coefficient = @clusteringCoefficient, local_gatekeeping_role_count = @gatekeepingRoleCount WHERE msisdn = @observedUser
END
但后一个查询返回错误:Msg 208,Level 16,State 1,Line 25 无效的对象名称“possibleConnections”。
有谁知道为什么不能以这种方式多次查询相同的表达式以及如何修改上面的查询以便在同一循环中计算@clusteringCoefficient和@gatekeepingRoleCount?
谢谢你!
To calculate clustering coefficient I have created the following query:
--calculate embeddedness and local_gatekepping_role_count (for two-way connections)
declare @observedUser varchar(50)
declare @clusteringCoefficient decimal(18, 7)
declare @possibleConnectionsCount int
declare @neighborConnectionsCount int
declare @rowsAffected int
SET @rowsAffected = 1;
WHILE @rowsAffected > 0
BEGIN
SELECT TOP 1 @observedUser = msisdn FROM users_in_group_91 WHERE clustering_coefficient IS NULL
SET @rowsAffected = @@ROWCOUNT
IF @rowsAffected = 0 break;
;WITH neighbors AS
(
SELECT called_party AS neighbor FROM connections_in_group_91 WHERE calling_party = @observedUser
UNION
SELECT calling_party AS neighbor FROM connections_in_group_91 WHERE called_party = @observedUser
),
possibleConnections AS
(
SELECT n1.neighbor AS user1, n2.neighbor AS user2 FROM neighbors n1 CROSS JOIN neighbors n2 WHERE n1.neighbor < n2.neighbor
)
SELECT @clusteringCoefficient = CAST(COUNT(*) AS decimal)/(SELECT COUNT(*) FROM possibleConnections) FROM possibleConnections pc INNER JOIN connections_in_group_91 c91 ON c91.calling_party = pc.user1 AND c91.called_party = pc.user2
)
UPDATE users_in_group_91 SET clustering_coefficient = @clusteringCoefficient WHERE msisdn = @observedUser
END
which works fine.As there would be useful to check how many times the observed user plays the role of gatekeeper I tried to modify the query above the following way:
declare @observedUser varchar(50)
declare @clusteringCoefficient decimal(18, 7)
declare @gatekeepingRoleCount int
declare @possibleConnectionsCount int
declare @neighborConnectionsCount int
declare @rowsAffected int
SET @rowsAffected = 1;
WHILE @rowsAffected > 0
BEGIN
SELECT TOP 1 @observedUser = msisdn FROM users_in_group_91 WHERE clustering_coefficient IS NULL
SET @rowsAffected = @@ROWCOUNT
IF @rowsAffected = 0 break;
;WITH neighbors AS
(
SELECT called_party AS neighbor FROM connections_in_group_91 WHERE calling_party = @observedUser
UNION
SELECT calling_party AS neighbor FROM connections_in_group_91 WHERE called_party = @observedUser
),
possibleConnections AS
(
SELECT n1.neighbor AS user1, n2.neighbor AS user2 FROM neighbors n1 CROSS JOIN neighbors n2 WHERE n1.neighbor < n2.neighbor
)
SELECT @possibleConnectionsCount = COUNT(*) FROM possibleConnections
SELECT @clusteringCoefficient = CAST(COUNT(*) AS decimal)/(@possibleConnectionsCount) FROM possibleConnections pc INNER JOIN connections_in_group_91 c91 ON c91.calling_party = pc.user1 AND c91.called_party = pc.user2
SET @gatekeepingRoleCount = @possibleConnectionsCount * (1 - @clusteringCoefficient)
UPDATE users_in_group_91 SET clustering_coefficient = @clusteringCoefficient, local_gatekeeping_role_count = @gatekeepingRoleCount WHERE msisdn = @observedUser
END
but the latter query returns an error: Msg 208, Level 16, State 1, Line 25
Invalid object name 'possibleConnections'.
Does anyone know why it is not possible to query the same expression multiple times this way and how to modify the query above in order to calculate @clusteringCoefficient and @gatekeepingRoleCount in the same loop?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CTE 仅适用于以下陈述。
在您的情况下,如果您摆脱标量变量并仅将子查询内联,那么它应该可以工作。
The CTE is only good for the following statement.
In your case if you get rid of the scalar variables and just put the subqueries inline it should work.