Question

I have the following data

COL-1     COL-2

1        0TY/OK
1        0TY/OK
1        0TY/OK 
1        0TY/OK 
1        0TY/OK
2        2KP/L         
2        2KP/L
2        2KP/L  
2        2KP/L       
2        2KP/L      
3        7U5/2M
3        7U5/2M 
3        7U5/2M 
3        7U5/2M

And i want to construct a select query to retrieve that data in the output below

COL-1     COL-2        COL-3

1        0TY/OK      0TY/OK
1        0TY/OK      2KP/L  
1        0TY/OK      7U5/2M
1        0TY/OK 
1        0TY/OK
2        2KP/L         
2        2KP/L
2        2KP/L  
2        2KP/L       
2        2KP/L      
3        7U5/2M
3        7U5/2M 
3        7U5/2M 
3        7U5/2M

I want COL3 to return the distinct values of COL2

Using SELECT COL1, COL2, DISTINCT COL2 AS COL3 FROM MYTable does not work is SQL SERVER

Was it helpful?

Solution

Although I'm sure that some SQL wizard will be able to construct a way to do this, I feel the need to point out that conceptually this doesn't make sense - the values in the rows of column 3 are completely unrelated to the row values in columns 1 and 2.

Can you not simply return the distinct values of COL2 in a separate query?

SELECT DISTINCT COL2 FROM MyTable

(Note that you can return multiple resultsets from a single SQL query)

OTHER TIPS

This is really unusual, and I can't see why you want this in one result set as it does not make any sense... There is no reason to associate the rows of the distinct query with the rows in the non-distinct query., but what you have to do is simply run both queries

Select Col1, Col2 From Table
Order By Col1, Col2

And

Select Distinct Col2 From Table

and join them together (To join them on row number, add a Row_Number() function to each query:

   Select Col1, Col2, Col3
   From   (Select Row_Number() Over(Order By Col1, Col2)RowNum, 
               Col1, Col2 
           From Table) T1
     Left Join 
          (Select Distinct Col2 As Col3,
           (Select Count(Distinct Col2)
            From Table
            Where Col2 <= T2.Col3) RowNum                    
           From Table) T2
        On T2.RowNum = T1.RowNum

Try this out..

WITH MyTable AS
(
    SELECT 1 Col1,CONVERT (VarChar (25), '0TY/OK') Col2 UNION ALL
    SELECT 1,'0TY/OK' UNION ALL
    SELECT 1,'0TY/OK' UNION ALL
    SELECT 1,'0TY/OK' UNION ALL
    SELECT 1,'0TY/OK' UNION ALL
    SELECT 2,'2KP/L' UNION ALL
    SELECT 2,'2KP/L' UNION ALL
    SELECT 2,'2KP/L' UNION ALL
    SELECT 2,'2KP/L' UNION ALL
    SELECT 2,'2KP/L' UNION ALL
    SELECT 3,'7U5/2M' UNION ALL
    SELECT 3,'7U5/2M' UNION ALL
    SELECT 3,'7U5/2M' UNION ALL
    SELECT 3,'7U5/2M'
)
,
AllData AS
(
    SELECT 
        *, 
        ROW_NUMBER () OVER (ORDER BY Col2) as Id
    FROM MyTable
)
,
DistinctData AS
(
        SELECT 
            Distinct Col2 AS Col3
        FROM MyTable
),
DistinctWithRowNumber AS
(
    SELECT 
        *, 
        ROW_NUMBER () OVER (ORDER BY Col3) as Id
    FROM DistinctData
)
SELECT 
    Col1, 
    Col2, 
    Col3
FROM AllData
    LEFT JOIN DistinctWithRowNumber 
        ON AllData.Id = DistinctWithRowNumber.Id

returns this result

Col1        Col2                      Col3
----------- ------------------------- -------------------------
1           0TY/OK                    0TY/OK
1           0TY/OK                    2KP/L
1           0TY/OK                    7U5/2M
1           0TY/OK                    NULL
1           0TY/OK                    NULL
2           2KP/L                     NULL
2           2KP/L                     NULL
2           2KP/L                     NULL
2           2KP/L                     NULL
2           2KP/L                     NULL
3           7U5/2M                    NULL
3           7U5/2M                    NULL
3           7U5/2M                    NULL
3           7U5/2M                    NULL

You can use CTEs to create a ROW_NUMBER and JOIN over those virtual columns.

DECLARE @t TABLE (
   Col1 INT
   ,Col2 VARCHAR(10)
);

INSERT INTO @t VALUES (1, '0TY/OK');
INSERT INTO @t VALUES (1, '0TY/OK');
INSERT INTO @t VALUES (1, '0TY/OK');
INSERT INTO @t VALUES (1, '0TY/OK');
INSERT INTO @t VALUES (1, '0TY/OK');
INSERT INTO @t VALUES (2, '2KP/L,');
INSERT INTO @t VALUES (2, '2KP/L');
INSERT INTO @t VALUES (2, '2KP/L');
INSERT INTO @t VALUES (2, '2KP/L');
INSERT INTO @t VALUES (2, '2KP/L');
INSERT INTO @t VALUES (3, '7U5/2M');
INSERT INTO @t VALUES (3, '7U5/2M');
INSERT INTO @t VALUES (3, '7U5/2M');
INSERT INTO @t VALUES (3, '7U5/2M');

; WITH all_data AS (
   SELECT
      Col1
      ,Col2
      ,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RowNum
   FROM @t
),
distinct_data AS (
   SELECT
      Col2
      ,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RowNum
   FROM @t
   GROUP BY
      Col2
)
SELECT
   all_data.Col1
   ,all_data.Col2
   ,distinct_data.Col2
FROM all_data
   LEFT JOIN distinct_data ON all_data.RowNum = distinct_data.RowNum
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top