Hall of Fame
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
Another way is to copy the run the contents of the below SELECT statement:
SELECT 'SELECT DISTINCT [' + COLUMN_NAME + '] FROM ' + TABLE_NAME
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.
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
SELECT *, rn=ROW_NUMBER() OVER (PARTITION BY Col, C ORDER BY (SELECT NULL))
FROM SampleData a
VALUES ('C1', C1),('C2', C2)
) b (Col, C)
Please let us know if any of these responses have been helpful.
My thought question: Have you ever been told that your query runs too fast?
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]
This lists all not null columns with the record counts
--Drop table #T
select Row_number() over(order by column_name) Row,COLUMN_NAME
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)
set @col = (select column_name from #T where Row = @n)
set @STR = 'Insert into #Results select '''+@col+''' as Column_Name, count( ['+@col+']) Rec_Count '+ ' from <table_name> having count( ['+@col+']) >0 '
select * from #Results
I have to ask... what problem are you trying to solve here?
Change is inevitable... Change for the better is not.
A newbie replied to a decade old post. I'm not sure you're going to get much info from the original poster.
J. Drew Allen
Business Intelligence Analyst
Viewing 7 posts - 1 through 6 (of 6 total)