I need a function for all distinct values in my table in SQL Server.
For example, if my table looks like this:
field1 | field2 | ... | field8
---------+----------+-----+---------
valuef11 | valuef21 | ... | valuef81
valuef12 | valuef22 | ... | valuef82
etc...
Then I need to return a table with 2 fields as follows:
field1 valuef11
field1 valuef12
field1 valuef13
field2 valuef21
field2 valuef22
....
field8 valuef81
field8 valuef82
field8 valuef83
field8 valuef84
I think you are looking for this:
Note that this assumes that all fields have the same type.
In SQL Server 2005 or newer you could also use
UNPIVOT:Again, this assumes that all fields have the same type.