This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below input data and expected output to place NULL for repeating values.
Input Data:
1 2 3 4 5 6 7 8 9 |
A B C Code ----------- ----------- ----------- ---- 1 1 1 A 1 1 2 A 1 1 3 A 1 1 4 A 2 2 1 C 2 2 2 C 2 2 3 C |
Expected Output:
1 2 3 4 5 6 7 8 9 |
A B C Code ----------- ----------- ----------- ---- 1 1 1 A NULL NULL 2 NULL NULL NULL 3 NULL NULL NULL 4 NULL 2 2 1 C NULL NULL 2 NULL NULL NULL 3 NULL |
Create a table with sample data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
CREATE TABLE Code ( A int ,B Int ,C int ,Code CHAR(1) ) GO INSERT INTO Code VALUES (1,1,1,'A') ,(1,1,2,'A') ,(1,1,3,'A') ,(1,1,4,'A') ,(2,2,1,'C') ,(2,2,2,'C') ,(2,2,3,'C') GO |
Solution:
1 2 3 4 5 6 7 8 9 10 |
;with ctetest as ( select *, row_number()over (partition by Code,A,B order by A) as rnk from Code ) select case when rnk = 1 then A else NULL end as A ,case when rnk = 1 then B else NULL end AS B ,C ,case when rnk = 1 then Code else NULL end AS Code from ctetest |
Please try the different solutions for this puzzle and share it via comment...