“Distinct” column in SQL query results

  • I have the following data

    COL1 COL2

    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 would like a query to give out the below output

    COL1 COL2 COL3

    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

  • I didn't get ur requirement...

  • Plz re-check your post...

    and come with more clarity plz...

    [font="Comic Sans MS"]Praveen Goud[/font]

  • I have made some clarification.

    I basically want COL3 to return the Distinct values of COL2, (remove duplicates from COL2 and return the unique values in COL3. Hope am much clear now.

  • What you are going to achieve with this..??

    Can i know where you are going to implement(practically) this..?

    [font="Comic Sans MS"]Praveen Goud[/font]

  • Hi there,

    I hope this helps..

    DECLARE @x TABLE (col1 INT, col2 VARCHAR(10))

    INSERT INTO @x

    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 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'

    ;WITH col1_and_col2 AS

    (

    SELECT ROW_NUMBER() OVER (ORDER BY col1,col2) AS row_num, col1, col2

    FROM @x

    )

    ,col3 AS

    (

    SELECT col2, ROW_NUMBER() OVER (ORDER BY col1,col2) AS row_num

    FROM @x

    GROUP BY col1,col2

    )

    SELECT a.col1,a.col2,b.col2 AS col3

    FROM col1_and_col2 a

    LEFT JOIN col3 b on a.row_num=b.row_num

    Cheers!

  • Praveen Goud Kotha (7/21/2010)


    What you are going to achieve with this..??

    Can i know where you are going to implement(practically) this..?

    I have an SSRS report which takes a value in COL2 as a parameter. Values that will be returned in COL2 are unknown at report design time. The available values for this report parameter should be all the possible (DISTINCT) values that are returned in COL2, so the parameter selection area (prompt) in the report should be a dropdownlist. Unfortunately, COL2 returns duplicate values(see my original post for the example), now COL3 is the solution to the problem of duplicates in the parameter prompt dropdownlist(Instead of providing a parameter dropdownlist with 100 duplicate options, if i return distinct values of COL2 AS COL3, and set my parameter source of values to COL3) i can reduce the available parameter options to say 10 instead of 100 duplicate values.

    I would have added a different query to retrieve distinct values of COL2 and then set it to be the source of my parameters available values ,but apparently, SSRS 2005 does not support multiple datasets in a single report.

    There is possibly a better way way to accomplish this, am not sure and would like to hear your opinion. thanks

  • Have you tried to use the column from the DataSet used for report to populate the Paramaeter ListBox? Does it works at all?

    I suggets you creating separate DataSet as SELEct DISTINCT Col2 FROM YoutrTable, and then use it in Available Values for your report Parameter.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Eugene Elutin (7/21/2010)


    Have you tried to use the column from the DataSet used for report to populate the Paramaeter ListBox? Does it works at all?

    Yes i have done that and it woks, but the parameter dropdownlist has duplication values, and thats what i want to overcome.

    Eugene Elutin (7/21/2010)


    I suggets you creating separate DataSet as SELEct DISTINCT Col2 FROM YoutrTable, and then use it in Available Values for your report Parameter.

    Now thats where the problem is. Can i have two completely seperate SQL queries in a single report dataset? I have tried adding one query just after the other and it cause an error.

    Do you know how to define two completely separate queries in a single SQL SERVER 2005 reporting services report??.

    I hear support for multiple datasets is only avaialable in sql server 2008

  • Praveen Goud Kotha (7/21/2010)


    What you are going to achieve with this..??

    Can i know where you are going to implement(practically) this..?

    Praveen,

    Please change your avatar. I made that avatar and I'd like to keep it for exclusive use. Thanks.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply