[thelist] [sql] selecting multiple distinct columns

Paul Cowan evolt at funkwit.com
Tue Oct 28 23:37:00 CST 2003


Paul Bennett wrote:
> Is it possible to query the db to get the distinct values of  all these
> columns combined
> (eg all the unique values from not just int_area_1, but from int_area_2
> and 3 as well)
> ?

Yup. Assuming all you want out are the values, you can do something like
(you don't say what DB you're using, but this will certainly work in SQL
server):

	SELECT
		int_area_1
	FROM
		mytable
	UNION
	SELECT
		int_area_2
	FROM
		mytable
	UNION
	SELECT
		int_area_3
	FROM
		mytable

the UNION operator, by default, removes duplicates, so you'll just get out
the distinct values. If you did want each value to appear multiple
times as required, you'd use UNION ALL instead.

Cheers,

Paul



More information about the thelist mailing list