Get Distinct Value from each column in a Table

  • Hi,

    how can i get distinct Value from each column from a Table?

    I think dynamic-sql is the rigth way 🙂

    Foreach column in Table

    Select distinct [column] from Table

    next

    Thanks

    Nicole

  • Another way is to copy the run the contents of the below SELECT statement:

    SELECT 'SELECT DISTINCT [' + COLUMN_NAME + '] FROM ' + TABLE_NAME

    FROM INFORMATION_SCHEMA.COLUMNS

    WHERE TABLE_NAME = 'SomeTable' -- your table

    ---------------------------------------------------------

    It takes a minimal capacity for rational thought to see that the corporate 'free press' is a structurally irrational and biased, and extremely violent, system of elite propaganda.
    David Edwards - Media lens[/url]

    Society has varying and conflicting interests; what is called objectivity is the disguise of one of these interests - that of neutrality. But neutrality is a fiction in an unneutral world. There are victims, there are executioners, and there are bystanders... and the 'objectivity' of the bystander calls for inaction while other heads fall.
    Howard Zinn

  • Another way to get your code:SELECT 'SELECT DISTINCT ' + AC.[name] + ' AS Column_Name FROM ' + T.[name]

    FROM sys.tables AS T

    INNER JOIN sys.all_columns AC ON T.[object_id] = AC.[object_id]

    INNER JOIN sys.types TY ON AC.[system_type_id] = TY.[system_type_id]

    AND AC.[user_type_id] = TY.[user_type_id]

    WHERE T.[is_ms_shipped] = 0

    I think the dynamic code using sp_executesql would be helpful.

  • Here's my interpretation of your requirement:

    WITH SampleData (C1, C2) AS

    (

    SELECT 'A', 'AA'

    UNION ALL SELECT 'A', 'BB'

    UNION ALL SELECT 'B', 'CC'

    UNION ALL SELECT 'C', 'CC'

    UNION ALL SELECT 'C', 'DD'

    )

    SELECT Col, C

    FROM

    (

    SELECT *, rn=ROW_NUMBER() OVER (PARTITION BY Col, C ORDER BY (SELECT NULL))

    FROM SampleData a

    CROSS APPLY

    (

    VALUES ('C1', C1),('C2', C2)

    ) b (Col, C)

    ) a

    WHERE rn=1;

    Please let us know if any of these responses have been helpful.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 4 posts - 1 through 3 (of 3 total)

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