3

Query result is

ChannelType |  HA BM  | AV BM      |  COUNTRY HEAD
-----------------------------------------------
  RS        |  NULL   |  NULL      |    abc
  RS        |  NULL   |  NULL      |    xyz
  RS        |  NULL   | prasanta.p |    NULL
  RS        |  NULL   | v.sanjay   |    NULL
  RS        | utpal.c | NULL       |    NULL
  RS        | vipul.k | NULL       |    NULL

I want final result to be:

ChannelType |  HA BM  | AV BM      |  COUNTRY HEAD
-----------------------------------------------
RS          | utpal.c | prasanta.p |    abc
RS          | vipul.k | v.sanjay   |    xyz

I want to remove null values in record.

Thanks in advance

2
  • 2
    It also answered here stackoverflow.com/questions/27425021/… Commented Apr 30, 2015 at 6:40
  • @GeoVIP - the question in your link doesn't have multiple non NULL column values for a single PK. The solution provided there will not work in this question. Commented Apr 30, 2015 at 7:05

1 Answer 1

1

In SQL Server 2008, You can use ROW_NUMBER() to get the order of the values and then do a self join. Something like this.

Data

DECLARE @table TABLE
(
    ChannelType CHAR(2),
    [HA BM] VARCHAR(10),
    [AV BM] VARCHAR(10),
    [COUNTRY HEAD] CHAR(3)
)
INSERT INTO @table VALUES
('RS',NULL,NULL,'abc'),
('RS',NULL,NULL,'xyz'),
('RS',NULL,'prasanta.p',NULL),
('RS',NULL,'v.sanjay',NULL),
('RS','utpal.c',NULL, NULL),
('RS','vipul.k',NULL, NULL)

Query

;WITH CTE as 
(
SELECT ChannelType,[HA BM],[AV BM],[COUNTRY HEAD],
ROW_NUMBER()OVER(ORDER BY CASE WHEN [HA BM] IS NULL THEN 2 ELSE 1 END ASC) h_row,
ROW_NUMBER()OVER(ORDER BY CASE WHEN [AV BM] IS NULL THEN 2 ELSE 1 END ASC) a_row,
ROW_NUMBER()OVER(ORDER BY CASE WHEN [COUNTRY HEAD] IS NULL THEN 2 ELSE 1 END ASC) c_row
FROM @table
)
SELECT C1.ChannelType,C1.[HA BM],C2.[AV BM],C3.[COUNTRY HEAD]
FROM CTE C1
LEFT JOIN CTE C2 ON C1.h_row = C2.a_row AND C1.ChannelType = C2.ChannelType AND C2.a_row <> 0
LEFT JOIN CTE C3 ON C1.h_row = C3.c_row AND C1.ChannelType = C3.ChannelType AND C3.c_row <> 0
WHERE C1.[HA BM] IS NOT NULL AND C2.[AV BM] IS NOT NULL AND C3.[COUNTRY HEAD] IS NOT NULL

SQL Server 2012 and future editions, you can use LEAD and COUNT() OVER(). Something like this

;WITH CTE as 
(
SELECT ChannelType,
LEAD([HA BM])OVER(ORDER BY [HA BM] ASC) [HA BM],
LEAD([AV BM])OVER(ORDER BY [AV BM] ASC) [AV BM],
LEAD([COUNTRY HEAD])OVER(ORDER BY [COUNTRY HEAD] ASC) [COUNTRY HEAD],
COUNT([HA BM])OVER(ORDER BY [HA BM] ASC)  +
COUNT([AV BM])OVER(ORDER BY [AV BM] ASC) +
COUNT([COUNTRY HEAD])OVER(ORDER BY [COUNTRY HEAD] ASC) c_row
FROM @table
)
SELECT ChannelType,MAX([HA BM]) [HA BM],MAX([AV BM]) [AV BM],MAX([COUNTRY HEAD]) [COUNTRY HEAD]
FROM CTE
GROUP BY ChannelType,c_row
Sign up to request clarification or add additional context in comments.

1 Comment

Bravo, that is exactly what I mean also

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.