SQL查询中的变量

时间:2018-04-26 13:20:43

标签: sql-server

我有这个:

dataset.SliceLocation

在参数字段中是id(0C01C994-1205-E511-988E-26EE4189191B)。是否有可能从列表中为每一行顺序分配不同的ID?总共有5个id。

我想要达到的是这个结果集在5个不同的id之间平均分配。

由于

3 个答案:

答案 0 :(得分:0)

您可以在子查询中再添加一个NEWID(),并在SELECT中处理,如下所示:

SELECT id, [name], TypeId, OrgName, SystemUserId, StatusId, CreatedAt, 
       '{"EntityName":"account","Ids":["' + AccountId +'"],"OwnerId":"' + ParamId + '"}' as [Parameters]
FROM ( 
    SELECT  NEWID() as id,
            'OwnerReassign' as name, 
            1 as TypeId, 
            'MyOrganisation' as OrgName,
            '07DA8E53-74BD-459C-AF94-A037897A51E3' as SystemUserId,
            0 as StatusId,
            GETDATE() as CreatedAt,
            CAST(NEWID() AS VARCHAR (36)) as ParamId,
            CAST(AccountId as varchar(50)) as AccountId
    FROM Account
    WHERE OwnerIdName IN ('John Smith') AND New_AccountType = 1
) A

答案 1 :(得分:0)

您可以使用以下内容。基本上,使用行号来更新您的ID和数据表,然后使用您要分配的ID量执行MOD (%)操作,以便将要更新的数据表拆分为N个组。然后使用该组ID分配每个ID。

IF OBJECT_ID('tempdb..#IDsToAssign') IS NOT NULL
    DROP TABLE #IDsToAssign

CREATE TABLE #IDsToAssign (
    IDToAssign VARCHAR(100))

-- 3 IDs example
INSERT INTO #IDsToAssign (
    IDToAssign)
SELECT IDToAssign = NEWID()
UNION ALL
SELECT IDToAssign = NEWID()
UNION ALL
SELECT IDToAssign = NEWID()

DECLARE @AmountIDsToAssign INT = (SELECT COUNT(1) FROM #IDsToAssign)


IF OBJECT_ID('tempdb..#Account') IS NOT NULL
    DROP TABLE #Account

CREATE TABLE #Account (
    PrimaryKey INT PRIMARY KEY,
    AssignedID VARCHAR(100))

-- 10 Rows example
INSERT INTO #Account (
    PrimaryKey)
VALUES
    (100),
    (200),
    (351),
    (154),
    (194),
    (345),
    (788),
    (127),
    (124),
    (14)

;WITH DataRowNumber AS
(
    SELECT
        A.*,
        RowNumber = ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
    FROM
        #Account AS A
),
IDsRowNumbers AS
(
    SELECT
        D.IDToAssign,
        RowNumber = ROW_NUMBER() OVER (ORDER BY D.IDToAssign)
    FROM
        #IDsToAssign AS D
),
NewIDAssignation AS
(
    SELECT
        R.*,
        IDRowNumberAssignation = (R.RowNumber % @AmountIDsToAssign) + 1
    FROM
        DataRowNumber AS R
)
UPDATE A SET
    AssignedID = R.IDToAssign
FROM
    NewIDAssignation AS N
    INNER JOIN IDsRowNumbers AS R ON N.IDRowNumberAssignation = R.RowNumber
    INNER JOIN #Account AS A ON N.PrimaryKey = A.PrimaryKey



SELECT
    *
FROM
    #Account AS A
ORDER BY
    A.AssignedID

/* Results:

    PrimaryKey  AssignedID
    ----------- ------------------------------------
    124         1CC7F0F1-7EDE-4F7F-B0A3-739D74A62390
    194         1CC7F0F1-7EDE-4F7F-B0A3-739D74A62390
    351         1CC7F0F1-7EDE-4F7F-B0A3-739D74A62390
    788         2A58A573-EDCB-428E-A87A-6BFCED265A9C
    200         2A58A573-EDCB-428E-A87A-6BFCED265A9C
    127         2A58A573-EDCB-428E-A87A-6BFCED265A9C
    14          2A58A573-EDCB-428E-A87A-6BFCED265A9C
    100         FD8036DA-0E15-453E-8A59-FA3C2BDB8FB1
    154         FD8036DA-0E15-453E-8A59-FA3C2BDB8FB1
    345         FD8036DA-0E15-453E-8A59-FA3C2BDB8FB1
*/

ROW_NUMBER()函数的排序将决定如何分配ID。

答案 2 :(得分:0)

您可以使用子查询中的ROW_NUMBER()字段来执行此操作;例如:

SELECT NEWID() as id, 'OwnerReassign' as name, 1 as TypeId, 
    'MyOrganisation' as OrgName,
    '07DA8E53-74BD-459C-AF94-A037897A51E3' as SystemUserId,
    0 as StatusId, GETDATE() as CreatedAt,
    case B / @@ROWCOUNT
       when 0 then '0C01C994-1205-E511-988E-26EE4189191B'
       when 1 then '12345677-1205-E511-988E-26EE4189191B'           
       when 2 then '66666666-1205-E511-988E-26EE4189191B'
       etc...
    end
FROM
(
  SELECT ROW_NUMBER() OVER (ORDER BY A.Id)
  FROM Account A
  WHERE OwnerIdName IN ('John Smith') AND New_AccountType = 1
) AS B

如果您希望系统选择这些值,那么您也可以将它们放在自己的临时表中。