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

  • This lists all not null columns with the record counts

    --Drop table #T

    select Row_number() over(order by column_name) Row,COLUMN_NAME

    into #T

    from INFORMATION_SCHEMA.columns

    where table_name = '<table_name>'

    Create table #Results ([Column_Name] varchar(200), Rec_Count int)

    --Truncate table #Results

    declare @col varchar(50), @n int=1, @STR varchar(max)

    while @n <= (select count(*) from #T)

    begin

    set @col = (select column_name from #T where Row = @n)

    set @n=@n+1

    set @STR = 'Insert into #Results select '''+@col+''' as Column_Name, count( ['+@col+']) Rec_Count '+ ' from <table_name> having count( ['+@col+']) >0 '

    exec (@str)

    end

    select * from #Results

     

  • I have to ask... what problem are you trying to solve here?

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

  • A newbie replied to a decade old post.  I'm not sure you're going to get much info from the original poster.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

Viewing 7 posts - 1 through 6 (of 6 total)

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