简体   繁体   中英

How to get missing values in gaps of Table Data

How to fill the gaps of table . I have a sample data

DECLARE @Table TABLE 
( Rule_ID VARCHAR(10),
  Name    VARCHAR(10),
  Age     INT
)
INSERT INTO @Table(Rule_ID,Name,Age) 
VALUES
('Rule_01','Mohan',29),
('Rule_01','Manasa',25),
('Rule_03','Raju',29),
('Rule_03','Miju',25),
('Rule_05','Ramu',30),
('Rule_05','Rao',35)

Select * from @Table

Present Result Set

Rule_ID Name Age

Rule_01 Mohan   29
Rule_01 Manasa  25
Rule_03 Raju    29
Rule_03 Miju    25
Rule_05 Ramu    30
Rule_05 Rao 35

I want output like this

Rule_ID Name Age

Rule_01 Mohan   29
Rule_01 Manasa  25
Rule_02 NULL    NULL
Rule_03 Raju    29
Rule_03 Miju    25
Rule_04 NULL    NULL
Rule_05 Ramu    30
Rule_05 Rao     35

I have tried lot of answers from Stack overflow. But I'm unable to move forward . Suggest me

Depands on Rule_ID . Work with last two digit of Rule_ID .

DECLARE @Table TABLE 
( Rule_ID VARCHAR(10),
  Name    VARCHAR(10),
  Age     INT
)
INSERT INTO @Table(Rule_ID,Name,Age) 
VALUES
('Rule_01','Mohan',29),
('Rule_01','Manasa',25),
('Rule_03','Raju',29),
('Rule_03','Miju',25),
('Rule_05','Ramu',30),
('Rule_05','Rao',35)

DECLARE @MaxValue INT
SELECT @MaxValue = MAX(CAST(RIGHT(Rule_ID, 2) AS INT)) from @Table

DECLARE @Id INT = 1
;WITH Tmp (Id)
AS 
(
    SELECT @Id AS Id
    UNION ALL
    SELECT Id +1 FROM Tmp
    WHERE
        Id < @MaxValue      
)

SELECT * FROM @Table
UNION ALL
SELECT 'Rule_' + RIGHT('00' + CAST(A.Id AS NVARCHAR(2)), 2), NULL, null FROM Tmp A
WHERE
    NOT EXISTS
    (
        SELECT TOP 1 1 FROM @Table T
        WHERE
            T.Rule_ID = 'Rule_' + RIGHT('00' + CAST(A.Id AS NVARCHAR(2)), 2)
    )
ORDER BY Rule_ID

Produced

Rule_ID    Name       Age
---------- ---------- -----------
Rule_01    Mohan      29
Rule_01    Manasa     25
Rule_02    NULL       NULL
Rule_03    Raju       29
Rule_03    Miju       25
Rule_04    NULL       NULL
Rule_05    Ramu       30
Rule_05    Rao        35

One way to get the result you want, if the different Rule_IDs are known ahead (ie fixed) and you don't have a source for the different Rule_IDs that should exist is to use a table value constructor like this:

SELECT Rules.Rule_ID, t.Name, t.Age 
FROM (
    VALUES ('Rule_01'),('Rule_02'),('Rule_03'),('Rule_04'),('Rule_05')
) AS Rules (Rule_ID)   
LEFT JOIN @Table t ON t.Rule_ID = Rules.Rule_ID

This plays well for fixed data, but if the range of Rule_IDs depend on the data in the table and you want to fill the gaps between the lowest and highest number another solution might be better.

SELECT tally.Rule_ID ,Name ,  Age
FROM (
    SELECT TOP(99) Rule_ID='Rule_' + left('0'+cast(row_number() over (order by (select null)) as varchar(2)), 2)
    FROM sys.all_objects ) AS tally
LEFT JOIN @Table t ON t.Rule_ID = tally.Rule_ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM