排序/排名|如果在子查询中?

时间:2018-04-24 12:11:50

标签: sql sql-server sql-server-2008 tsql

我正在尝试编写一个proc,它将根据帐户类型返回多人的联系详细信息。

我使用ifunions来获取数据,但我还想在proc中命令我的查询,但我不确定最佳方法。

这是我的查询:

    SELECT Name, Type, RecipientId FROM (
IF(@EntityType = 'Tenant')
BEGIN


SELECT
    c.Title + ' ' + c.FirstName + ' ' + c.LastName AS Name,
    'Tenant' AS [Type],
    tc.TenantId AS [RecipientId],
    1 AS DisplayOrder
FROM
    Tenant.Account tc
WHERE
    tc.AccountId = @EntityId

UNION

SELECT
    c.Title + ' ' + c.FirstName + ' ' + c.LastName AS Name,
    tc.Relationship AS [Type],
    tc.ContactId AS [RecipientId],
    2 AS DisplayOrder
FROM
    Tenant.Contacts tc
JOIN
    General.Contact c ON tc.ContactId = c.ContactId
WHERE
    tc.ContactId = @EntityId
END

IF(@EntityType = 'Landlord')
BEGIN


SELECT
    c.Title + ' ' + c.FirstName + ' ' + c.LastName AS Name,
    'Landlord' AS [Type],
    tc.LandlordId AS [RecipientId],
    2 AS DisplayOrder
FROM
    Landlords.Account tc
WHERE
    tc.LandlordId = @EntityId

UNION

SELECT
    c.Title + ' ' + c.FirstName + ' ' + c.LastName AS Name,
    tc.Relationship AS [Type],
    tc.ContactId AS [RecipientId],
    1 AS DisplayOrder
FROM
    Landlords.Contacts tc
JOIN
    General.Contact c ON tc.ContactId = c.ContactId
WHERE
    tc.ContactId = @EntityId
END) a 

 ORDER BY a.DisplayOrder

这导致在子查询中使用IF时出错,因此欢迎任何建议,因为这是一个非常长的查询,其中需要从proc订购显示只是一个要求。

2 个答案:

答案 0 :(得分:2)

您可以在SORT声明中IF结果集。

试试这样:

IF(@EntityType = 'AccountType1')
BEGIN
    SELECT Name, Type, RecipientId 
    FROM (      
            SELECT
                c.Title + ' ' + c.FirstName + ' ' + c.LastName AS Name,
                tc.Relationship AS [Type],
                tc.ContactId AS [RecipientId],
                1 AS DisplayOrder
            FROM
                Temp.Contacts tc
            JOIN
                General.Contact c ON tc.ContactId = c.ContactId
            WHERE
                tc.ContactId = @EntityId

            UNION

            SELECT
                c.Title + ' ' + c.FirstName + ' ' + c.LastName AS Name,
                tc.Relationship AS [Type],
                tc.ContactId AS [RecipientId],
                2 AS DisplayOrder
            FROM
                All.Contacts tc
            JOIN
                General.Contact c ON tc.ContactId = c.ContactId
            WHERE
                tc.ContactId = @EntityId
    )A
    ORDER BY A.DisplayOrder
END

IF(@EntityType = 'AccountType2')
BEGIN
    SELECT Name, Type, RecipientId 
    FROM (      
        SELECT
            c.Title + ' ' + c.FirstName + ' ' + c.LastName AS Name,
            tc.Relationship AS [Type],
            tc.ContactId AS [RecipientId],
            2 AS DisplayOrder
        FROM
            Temp.Contacts tc
        JOIN
            General.Contact c ON tc.ContactId = c.ContactId
        WHERE
            tc.ContactId = @EntityId

        UNION

        SELECT
            c.Title + ' ' + c.FirstName + ' ' + c.LastName AS Name,
            tc.Relationship AS [Type],
            tc.ContactId AS [RecipientId],
            1 AS DisplayOrder
        FROM
            All.Contacts tc
        JOIN
            General.Contact c ON tc.ContactId = c.ContactId
        WHERE
            tc.ContactId = @EntityId
    )A
    ORDER BY A.DisplayOrder
END

答案 1 :(得分:1)

如果我正确地得到这两个查询是相同的,唯一的区别是DisplayOrder

只需使用一个SELECT,然后将1 AS DisplayOrder替换为

CASE WHEN @EntityType = 'AccountType1' THEN 1 ELSE 2 END AS DisplayOrder

在第二次出现时你会把它带到另一个地方

CASE WHEN @EntityType = 'AccountType1' THEN 2 ELSE 1 END AS DisplayOrder

在这种情况下不需要任何IF ......

编辑和更新......

必须纠正错字......

如果您有两种以上类型,则可以使用其他CASE语法

CASE @EntityType WHEN 'a' THEN 1
                 WHEN 'b' THEN 2
                 ... add more ...
                 ELSE 99 END