使用分组依据

时间:2019-11-14 10:31:49

标签: sql sql-server group-by sql-order-by

我正在使用以下SQL查询将数据插入到临时表中

INSERT INTO @tmp_Notify 
    SELECT     'U' + CONVERT(varchar,UserId), CONVERT([varchar](50), [FirstName]) + ' ' + CONVERT([varchar](50), [LastName])
    FROM  [User] 
            INNER JOIN dbo.UserGroup ON UserId = FkUserId  
            INNER JOIN [Group] ON GroupId = FkGroupId 
    GROUP BY UserId, CONVERT([varchar](50), [FirstName]), CONVERT([varchar](50), [LastName])
    HAVING MIN(GroupLevel) >= @minGroup
    ORDER BY CONVERT([varchar](50), [FirstName]), CONVERT([varchar](50), [LastName])

我要插入以下数据,

将组数据插入为

INSERT INTO @tmp_Notify 
    SELECT     'G' + CONVERT(varchar,GroupId), 'Group - ' + GroupName
    FROM  [Group] 
    WHERE GroupLevel >= @minGroup AND IsActive = 1
    ORDER BY GroupName

所以我需要先显示用户名列表,然后再按GroupName显示组名顺序。

我的完整代码是

    --Users of the same group as currently logged in user and userÆs of higher groups
    INSERT INTO @tmp_Notify 
        SELECT     'U' + CONVERT(varchar,UserId), CONVERT([varchar](50), DECRYPTBYKEY([FirstName])) + ' ' + CONVERT([varchar](50), DECRYPTBYKEY([LastName]))
        FROM  [User] 
                INNER JOIN dbo.UserGroup ON UserId = FkUserId  
                INNER JOIN [Group] ON GroupId = FkGroupId 
        GROUP BY UserId, CONVERT([varchar](50), DECRYPTBYKEY([FirstName])), CONVERT([varchar](50), DECRYPTBYKEY([LastName]))
        HAVING MIN(GroupLevel) >= @minGroup
        ORDER BY CONVERT([varchar](50), DECRYPTBYKEY([FirstName])), CONVERT([varchar](50), DECRYPTBYKEY([LastName]))

    select * from @tmp_Notify

    --Currently logged in userÆs group and higher groups
    INSERT INTO @tmp_Notify 
        SELECT     'G' + CONVERT(varchar,GroupId), 'Group - ' + GroupName
        FROM  [Group] 
        WHERE GroupLevel >= @minGroup AND IsActive = 1
--      GROUP BY GroupId, GroupName
--      HAVING MIN(GroupLevel) >= @minGroup
        ORDER BY GroupName
END
ELSE
BEGIN
    INSERT INTO @tmp_Notify VALUES('Me','Me')

    IF((SELECT FkSupervisor FROM [User] WHERE UserId = @currentUser) IS NOT NULL)
    BEGIN
        INSERT INTO @tmp_Notify VALUES('Supervisor','Supervisor')
    END
END

    --List Option (to allow a list of email addresses)
    INSERT INTO @tmp_Notify VALUES('List','List')   

    --SELECT * FROM @tmp_Notify tn order by tn.NotifyName
    SELECT * FROM @tmp_Notify

但是它的顺序不正确,我需要按FirstNameLastName对数据进行排序。我该怎么办

我的预期输出是

U10059  Emily Windrow
U10039  Esta Kulzer
U10030  Ileana Conklin
U10040  Kamala Millerbernd
U10007  Katherine Tayan
G110    Group - Managers
G112    Group - Membership Officers
G113    Group - Mortgage
G108    Group - Operations Administrator
G109    Group - PFS
List    List

2 个答案:

答案 0 :(得分:1)

这是一个常见的误解。

向插入语句添加顺序没有意义,因为原始数据表没有固有的保证顺序。在SQL Server中,无论数据进入的顺序如何,都将存储在聚集索引中,并由此排序。它还将被添加到表中的其他索引中,这些索引可能又有所不同。如果您根本没有索引,并且您的表是堆,那么无论您做什么,该顺序都将不确定。

当您查询表时,没有默认的保证顺序,除非您使用ORDER BY指定数据,否则数据将以返回。没有这个,返回的数据理论上就可以是任何顺序的(SQL Server将以允许其在最快的时间内完成查询的任何顺序输出)。

将数据放入表中的顺序与将其再次取回的顺序无关。

答案 1 :(得分:1)

您可以使用类似于以下内容的东西来获得想要的东西:

DECLARE @tmp_Notify TABLE (id INT IDENTITY(1, 1), FirstName VARCHAR(50), lastname VARCHAR(50))

INSERT @tmp_Notify (FirstName, lastname)
VALUES ( 'Bob', 'Curtis')
,       ('Mary', 'Magdalene')

INSERT @tmp_Notify (FirstName, lastname)
VALUES ( 'GROUP', '1')
,       ('GROUP', '2')

INSERT @tmp_Notify (FirstName, lastname)
VALUES ( 'List', 'List')

SELECT  TN.id
,       TN.FirstName
,       TN.lastname
FROM    @tmp_Notify AS TN
ORDER BY TN.id

在这种情况下,您可以在ORDER BY中使用INSERT,因为这将确定id的值。