Tuesday, March 27, 2012

hmm,

You have a row of data set up like this a,b,c,d these are the columns. The
values are a=3, b=1, c=5, d=4 and I need these put in descending order but
this is one row and I need it to come back looking like multiple rows. How
would you suggest I go about this?
Shawntry..
select a as Result from table
union all
select b from table
union all
select c from table
union all
select d from table
order by Result
might be a better way to do this, anyone?
"Shawn Mason" wrote:

> You have a row of data set up like this a,b,c,d these are the columns. Th
e
> values are a=3, b=1, c=5, d=4 and I need these put in descending order but
> this is one row and I need it to come back looking like multiple rows. Ho
w
> would you suggest I go about this?
> Shawn
>
>|||So this is the reverse of the ubiquitous post "How do I combine multiple
rows into one row?":
select a as x from mytable
union all
select b as x from mytable
union all
select c as x from mytable
union all
select d as x from mytable
order by x desc
"Shawn Mason" <shawn@.issda.com> wrote in message
news:OQTuhkTuFHA.3528@.TK2MSFTNGP15.phx.gbl...
> You have a row of data set up like this a,b,c,d these are the columns.
> The values are a=3, b=1, c=5, d=4 and I need these put in descending order
> but this is one row and I need it to come back looking like multiple rows.
> How would you suggest I go about this?
> Shawn
>
>|||Try this:
select a as Result
union
select b
union
select c
union
select d
order by Result
ML|||Sorry, correction:
select a as Result
from <table_name>
union
select b
from <table_name>
union
select c
from <table_name>
union
select d
from <table_name>
order by Result desc
ML|||Have you looked up UNION ALL in BOL?
"Shawn Mason" <shawn@.issda.com> wrote in message
news:OQTuhkTuFHA.3528@.TK2MSFTNGP15.phx.gbl...
> You have a row of data set up like this a,b,c,d these are the columns.
The
> values are a=3, b=1, c=5, d=4 and I need these put in descending order but
> this is one row and I need it to come back looking like multiple rows.
How
> would you suggest I go about this?
> Shawn
>
>|||Please post DDL, sample data, and expected results
(http://www.aspfaq.com/etiquette.asp?id=5006)
Ideally, if each column is the same type of "thing", you would normalize the
table design such that these values *are* in multiple rows.
Barring that, you can do some kind of kludge like this:
SELECT BadColumn FROM
(SELECT ColumnA AS BadColumn FROM BadTable
UNION ALL
SELECT ColumnB AS BadColumn FROM BadTable
UNION ALL
SELECT ColumnC AS BadColumn FROM BadTable
UNION ALL
SELECT ColumnD AS BadColumn FROM BadTable) ReallyBadTable
ORDER BY BadColumn DESC
"Shawn Mason" <shawn@.issda.com> wrote in message
news:OQTuhkTuFHA.3528@.TK2MSFTNGP15.phx.gbl...
> You have a row of data set up like this a,b,c,d these are the columns.
> The values are a=3, b=1, c=5, d=4 and I need these put in descending order
> but this is one row and I need it to come back looking like multiple rows.
> How would you suggest I go about this?
> Shawn
>
>

No comments:

Post a Comment