从动态创建的内联表中进行选择

时间:2013-09-27 07:56:11

标签: sql sql-server tsql

好的,我有相当大的查询要执行,但我对它的要求很简单。我需要在查询本身创建的内联表中运行带有Where子句的Select Query。

这是代码

SELECT *FROM (
    SELECT * FROM
    (
        SELECT MomentId, Moment, ExpressionsCount, ShareComments, SharedDt, SharedBy, SharedByProfilePicture, OwnerProfilePicture, SharedVia,
        MomentOwnerName, SharedById, OwnerId, SharedViaId, ROW_NUMBER() OVER (ORDER BY SharedDt DESC) As RowNum FROM
        (
        SELECT moments.MomentId,
        Moment,
        ExpressionsCount =
                (SELECT COUNT(ExpressionId) FROM moments_Expressions WHERE moments_Expressions.momentId = moments.momentId),
        MomentDt As SharedDt,
        '' As ShareComments,
        FirstName + ' ' + LastName As SharedBy,
        '' As SharedByProfilePicture,
        OwnerProfilePicture =
        CASE
            WHEN ProfilePicture = ''
                THEN dbo.fn_GetProfilePicture(1)
            ELSE
                dbo.fn_GetProfilePicture(0) + ProfilePicture
        END,
        '' As SharedById,
        moments.UserId As OwnerId,
        '' As SharedViaId,
        '' As SharedVia,
        '' As MomentOwnerName,
        dbo.fn_CheckUserMomentPrivacy(moments.UserId, @UserId, moments.MomentId) As SharingStatus
        FROM moments_Master moments
        INNER JOIN tbl_User_Profiles profiles ON profiles.UserId = moments.UserId
        WHERE moments.UserId = @FriendId
        UNION ALL
        SELECT moments.MomentId,
        Moment,
        ExpressionsCount =
                (SELECT COUNT(ExpressionId) FROM moments_Expressions WHERE moments_Expressions.momentId = moments.momentId),
        SharedDt,
        sharing.ShareComments,
        FirstName + ' ' + LastName As SharedBy,
        SharedByProfilePicture =
        CASE
            WHEN ProfilePicture = ''
                THEN dbo.fn_GetProfilePicture(1)
            ELSE
                dbo.fn_GetProfilePicture(0) + ProfilePicture
        END,
        OwnerProfilePicture =
        (
        SELECT
        CASE
            WHEN ProfilePicture = ''
                THEN dbo.fn_GetProfilePicture(1)
            ELSE
                dbo.fn_GetProfilePicture(0) + ProfilePicture
        END
        FROM tbl_User_Profiles WHERE UserId = moments.UserId
        ), 
        CAST(sharing.SharedBy As VARCHAR(40)) As SharedById,
        moments.UserId As OwnerId, CAST(sharing.SharedVia As VARCHAR(40)) As SharedViaId,
        (SELECT FirstName + ' ' + LastName FROM tbl_User_Profiles WHERE UserId = SharedVia) As SharedVia,
        (SELECT FirstName + ' ' + LastName FROM tbl_User_Profiles WHERE UserId = moments.UserId) As MomentOwnerName,
        dbo.fn_CheckUserMomentPrivacy(SharedBy, @UserId, moments.MomentId) As SharingStatus
        FROM moments_Master moments
        INNER JOIN moments_Sharing sharing ON sharing.MomentId = moments.MomentId
        INNER JOIN tbl_User_Profiles profiles ON profiles.UserId = sharing.SharedBy
        WHERE sharing.SharedBy = @FriendId
        --AND dbo.fn_CheckUserMomentPrivacy(SharedBy, @UserId, moments.MomentId) = 1 AND sharing.SharedBy = @FriendId
        ) Moments
        WHERE SharingStatus = 1
    )  FinalRes
    WHERE RowNum BETWEEN @StartRow AND @EndRow
    --ORDER BY SharedDt DESC
    )  UltRes

    WHERE RowNum BETWEEN (SELECT RowNum FROM UltRes Where MomentId = 31928) AND CASE WHEN RowNum <=5 THEN (RowNum + 5) ELSE (RowNum - 5)
END

当我运行此查询时,它告诉我对象是'UltRes'无效。我需要RowNum位于MomentId的RowNum之间,并根据其值,确定其另一端。

基本上我需要在Select的{​​{1}}声明中重复使用UltRes。

2 个答案:

答案 0 :(得分:1)

您可以使用CTE声明UltRes,然后您可以将其视为表格。

;WITH UltRes AS
(
    SELECT * FROM
    (
        SELECT MomentId, Moment, ExpressionsCount, ShareComments, SharedDt, SharedBy, SharedByProfilePicture, OwnerProfilePicture, SharedVia,
        MomentOwnerName, SharedById, OwnerId, SharedViaId, ROW_NUMBER() OVER (ORDER BY SharedDt DESC) As RowNum FROM
        (
        SELECT moments.MomentId,
        Moment,
        ExpressionsCount =
                (SELECT COUNT(ExpressionId) FROM moments_Expressions WHERE moments_Expressions.momentId = moments.momentId),
        MomentDt As SharedDt,
        '' As ShareComments,
        FirstName + ' ' + LastName As SharedBy,
        '' As SharedByProfilePicture,
        OwnerProfilePicture =
        CASE
            WHEN ProfilePicture = ''
                THEN dbo.fn_GetProfilePicture(1)
            ELSE
                dbo.fn_GetProfilePicture(0) + ProfilePicture
        END,
        '' As SharedById,
        moments.UserId As OwnerId,
        '' As SharedViaId,
        '' As SharedVia,
        '' As MomentOwnerName,
        dbo.fn_CheckUserMomentPrivacy(moments.UserId, @UserId, moments.MomentId) As SharingStatus
        FROM moments_Master moments
        INNER JOIN tbl_User_Profiles profiles ON profiles.UserId = moments.UserId
        WHERE moments.UserId = @FriendId
        UNION ALL
        SELECT moments.MomentId,
        Moment,
        ExpressionsCount =
                (SELECT COUNT(ExpressionId) FROM moments_Expressions WHERE moments_Expressions.momentId = moments.momentId),
        SharedDt,
        sharing.ShareComments,
        FirstName + ' ' + LastName As SharedBy,
        SharedByProfilePicture =
        CASE
            WHEN ProfilePicture = ''
                THEN dbo.fn_GetProfilePicture(1)
            ELSE
                dbo.fn_GetProfilePicture(0) + ProfilePicture
        END,
        OwnerProfilePicture =
        (
        SELECT
        CASE
            WHEN ProfilePicture = ''
                THEN dbo.fn_GetProfilePicture(1)
            ELSE
                dbo.fn_GetProfilePicture(0) + ProfilePicture
        END
        FROM tbl_User_Profiles WHERE UserId = moments.UserId
        ), 
        CAST(sharing.SharedBy As VARCHAR(40)) As SharedById,
        moments.UserId As OwnerId, CAST(sharing.SharedVia As VARCHAR(40)) As SharedViaId,
        (SELECT FirstName + ' ' + LastName FROM tbl_User_Profiles WHERE UserId = SharedVia) As SharedVia,
        (SELECT FirstName + ' ' + LastName FROM tbl_User_Profiles WHERE UserId = moments.UserId) As MomentOwnerName,
        dbo.fn_CheckUserMomentPrivacy(SharedBy, @UserId, moments.MomentId) As SharingStatus
        FROM moments_Master moments
        INNER JOIN moments_Sharing sharing ON sharing.MomentId = moments.MomentId
        INNER JOIN tbl_User_Profiles profiles ON profiles.UserId = sharing.SharedBy
        WHERE sharing.SharedBy = @FriendId
        --AND dbo.fn_CheckUserMomentPrivacy(SharedBy, @UserId, moments.MomentId) = 1 AND sharing.SharedBy = @FriendId
        ) Moments
        WHERE SharingStatus = 1
    )  FinalRes
    WHERE RowNum BETWEEN @StartRow AND @EndRow
    --ORDER BY SharedDt DESC
    ) 
)

SELECT * 
FROM UltRes 
WHERE RowNum BETWEEN (SELECT RowNum FROM UltRes Where MomentId = 31928) AND CASE WHEN RowNum <=5 THEN (RowNum + 5) ELSE (RowNum - 5) END

答案 1 :(得分:0)

;WITH UltRes 

(
MomentId, 
Moment, 
ExpressionsCount, 
ShareComments, 
SharedDt, 
SharedBy, 
SharedByProfilePicture, 
OwnerProfilePicture, 
SharedVia,
MomentOwnerName, 
SharedById, 
OwnerId, 
SharedViaId, 
RowNum
)
AS(
    SELECT * FROM
    (
        SELECT MomentId, Moment, ExpressionsCount, ShareComments, SharedDt, SharedBy, SharedByProfilePicture, OwnerProfilePicture, SharedVia,
        MomentOwnerName, SharedById, OwnerId, SharedViaId, ROW_NUMBER() OVER (ORDER BY SharedDt DESC) As RowNum FROM
        (
        SELECT moments.MomentId,
        Moment,
        ExpressionsCount =
                (SELECT COUNT(ExpressionId) FROM moments_Expressions WHERE moments_Expressions.momentId = moments.momentId),
        MomentDt As SharedDt,
        '' As ShareComments,
        FirstName + ' ' + LastName As SharedBy,
        '' As SharedByProfilePicture,
        OwnerProfilePicture =
        CASE
            WHEN ProfilePicture = ''
                THEN dbo.fn_GetProfilePicture(1)
            ELSE
                dbo.fn_GetProfilePicture(0) + ProfilePicture
        END,
        '' As SharedById,
        moments.UserId As OwnerId,
        '' As SharedViaId,
        '' As SharedVia,
        '' As MomentOwnerName,
        dbo.fn_CheckUserMomentPrivacy(moments.UserId, @UserId, moments.MomentId) As SharingStatus
        FROM moments_Master moments
        INNER JOIN tbl_User_Profiles profiles ON profiles.UserId = moments.UserId
        WHERE moments.UserId = @FriendId
        UNION ALL
        SELECT moments.MomentId,
        Moment,
        ExpressionsCount =
                (SELECT COUNT(ExpressionId) FROM moments_Expressions WHERE moments_Expressions.momentId = moments.momentId),
        SharedDt,
        sharing.ShareComments,
        FirstName + ' ' + LastName As SharedBy,
        SharedByProfilePicture =
        CASE
            WHEN ProfilePicture = ''
                THEN dbo.fn_GetProfilePicture(1)
            ELSE
                dbo.fn_GetProfilePicture(0) + ProfilePicture
        END,
        OwnerProfilePicture =
        (
        SELECT
        CASE
            WHEN ProfilePicture = ''
                THEN dbo.fn_GetProfilePicture(1)
            ELSE
                dbo.fn_GetProfilePicture(0) + ProfilePicture
        END
        FROM tbl_User_Profiles WHERE UserId = moments.UserId
        ), 
        CAST(sharing.SharedBy As VARCHAR(40)) As SharedById,
        moments.UserId As OwnerId, CAST(sharing.SharedVia As VARCHAR(40)) As SharedViaId,
        (SELECT FirstName + ' ' + LastName FROM tbl_User_Profiles WHERE UserId = SharedVia) As SharedVia,
        (SELECT FirstName + ' ' + LastName FROM tbl_User_Profiles WHERE UserId = moments.UserId) As MomentOwnerName,
        dbo.fn_CheckUserMomentPrivacy(SharedBy, @UserId, moments.MomentId) As SharingStatus
        FROM moments_Master moments
        INNER JOIN moments_Sharing sharing ON sharing.MomentId = moments.MomentId
        INNER JOIN tbl_User_Profiles profiles ON profiles.UserId = sharing.SharedBy
        WHERE sharing.SharedBy = @FriendId
        --AND dbo.fn_CheckUserMomentPrivacy(SharedBy, @UserId, moments.MomentId) = 1 AND sharing.SharedBy = @FriendId
        ) Moments
        WHERE SharingStatus = 1
    )  FinalRes
    WHERE RowNum BETWEEN @StartRow AND @EndRow
    --ORDER BY SharedDt DESC
    )

   SELECT 
        *  -- SELECT SPECIFIC COLUMNS WHICH ON YOU NEED. 
   FROM 
    UltRes 
   WHERE 
    RowNum BETWEEN 
        (
            SELECT RowNum FROM UltRes Where MomentId = 31928
        ) 
    AND 
        CASE WHEN RowNum <=5 THEN (RowNum + 5) ELSE (RowNum - 5)END