您可以在SQL Order语句中进行分组吗(SQL Server)

时间:2019-03-21 18:40:32

标签: sql sql-server sql-server-2016

我不敢相信我正在这样做,但是我坚持订购查询。

DECLARE @purseprioritylist VARCHAR(512) = 'ANT,ANT,BAT,BAT'

DECLARE @PriorityListTable TABLE
                           (
                                Id INT IDENTITY (1,1),
                                PurseType CHAR(3),
                                SecondaryPurseType BIT DEFAULT(0)
                           )     

INSERT INTO @PriorityListTable (PurseType)
    SELECT Value
    FROM STRING_SPLIT(@PursePriorityList, ',')

/*******************************************************
 * Update the @PriorityListTable. The the second instance
 * of a purse type will be marked as secondary 
 * If a purse type appears only once it will not be
 * updated
 *******************************************************/
UPDATE @PriorityListTable 
SET SecondaryPurseType = 1 
WHERE ID IN (SELECT MAX(ID) AS MaxValue
             FROM @PriorityListTable pt
             GROUP BY pt.PurseType
             HAVING COUNT(pt.PurseType) > 1)             

DECLARE @TV_PURSELIST TABLE 
                      (
                          RecNum INT IDENTITY(1,1) PRIMARY KEY,
                          PurseCanDoId INT,
                          SlotId INT,
                          PurseName VARCHAR(255),
                          PurseType CHAR(3),
                          EffectiveDate DATETIME,
                          ExpDate DATETIME,
                          EndDate DATETIME,
                          ExtensionDate DATETIME
                      )

DECLARE @TV_MCCGROUPSWITHSECONDARYFLAG TABLE  
                                       (
                                           RecNum INT IDENTITY(1,1) PRIMARY KEY,
                                           PurseCanDoId INT,
                                           MCCGroup INT,
                                           IsSecondaryMCC BIT DEFAULT(0)
                                       )

INSERT INTO @tv_purselist (pursecandoid, slotid, pursename, pursetype, EffectiveDate, expdate, enddate, extensiondate) 
VALUES (1204970, 2, 'ANT2019', 'ANT', '2019-01-01', '2020-03-31', '2020-03-31', NULL),
       (1204971, 3, 'BAT2019', 'BAT', '2019-01-01', '2019-12-31', '2019-12-31', NULL),
       (1204972, 4, 'ANT2020', 'ANT', '2020-01-01', '2020-12-31', '2020-12-31', NULL)

INSERT INTO @TV_MCCGROUPSWITHSECONDARYFLAG(pursecandoid, mccgroup, issecondarymcc) 
VALUES (1204970, 69696, 0), (1204970, 65321, 1),
       (1204971, 32222, 0), (1204971, 45007, 1),
       (1204972, 69696, 0), (1204972, 65321, 1)


SELECT  
    plt.*,
    pl.SlotId, pl.PurseName, pl.PurseType,
    mg.MCCGroup, pl.PurseCanDoId, pl.EffectiveDate, pl.EndDate,
    ISNULL(mg.IsSecondaryMCC, 0)
FROM 
    @TV_PURSELIST pl
LEFT JOIN 
    @TV_MCCGROUPSWITHSECONDARYFLAG mg ON mg.PurseCanDoId = pl.PurseCanDoId  
INNER JOIN 
    @PriorityListTable plt ON plt.PurseType = pl.PurseType 
                           AND plt.SecondaryPurseType = mg.IsSecondaryMCC
ORDER BY 
    plt.Id, pl.PurseName, pl.EffectiveDate, pl.EndDate

问题是顺序不正确。

此查询返回...

ANT 0   2   ANT2019 ANT 69696   1204970 2019-01-01 00:00:00.000 2020-03-31 00:00:00.000 0
ANT 0   4   ANT2020 ANT 69696   1204972 2020-01-01 00:00:00.000 2020-12-31 00:00:00.000 0
ANT 1   2   ANT2019 ANT 65321   1204970 2019-01-01 00:00:00.000 2020-03-31 00:00:00.000 1
ANT 1   4   ANT2020 ANT 65321   1204972 2020-01-01 00:00:00.000 2020-12-31 00:00:00.000 1
BAT 0   3   BAT2019 BAT 32222   1204971 2019-01-01 00:00:00.000 2019-12-31 00:00:00.000 0
BAT 1   3   BAT2019 BAT 45007   1204971 2019-01-01 00:00:00.000 2019-12-31 00:00:00.000 1

我想要的是...

enter image description here

2 个答案:

答案 0 :(得分:0)

我认为这可以满足您的需求

order by 1, 3, 2

我建议使用列名,但我不知道它们是什么。

答案 1 :(得分:0)

按以下顺序删除“ plt.Id”

        SELECT  
            plt.Id, plt.PurseType, plt.SecondaryPurseType
        ,   pl.SlotId
        ,   pl.PurseName
        ,   pl.PurseType
        ,   mg.MCCGroup 
        ,   pl.PurseCanDoId
        ,  pl.EffectiveDate
        ,  pl.EndDate
        ,  ISNULL(mg.IsSecondaryMCC, 0)
        FROM @TV_PURSELIST pl
        LEFT JOIN @TV_MCCGROUPSWITHSECONDARYFLAG mg on mg.PurseCanDoId = pl.PurseCanDoId  
        INNER JOIN @PriorityListTable plt on plt.PurseType = pl.PurseType and plt.SecondaryPurseType = mg.IsSecondaryMCC
        ORDER BY pl.PurseName, pl.EffectiveDate, pl.EndDate

结果:enter image description here