公用表表达问题

时间:2010-12-07 13:53:59

标签: sql sql-server-2008 common-table-expression

要计算clustering coefficient,我创建了以下查询:

--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

但后一个查询返回错误:消息208,级别16,状态1,行25 无效的对象名称'possibleConnections'。

有谁知道为什么不能以这种方式多次查询同一个表达式以及如何修改上面的查询以便在同一个循环中计算@clusteringCoefficient和@gatekeepingRoleCount?

谢谢!

1 个答案:

答案 0 :(得分:1)

CTE仅适用于以下声明。

在你的情况下,如果你摆脱了标量变量,只是将子查询内联,它应该可以工作。