Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

SELECTing Rows to Columns - How to Get Column Alias From T..

 
   Database Help (Home) -> Programming RSS
Next:  How to return this result set?  
Author Message
Bob Johnson

External


Since: Nov 17, 2007
Posts: 11



(Msg. 1) Posted: Fri Feb 09, 2007 9:56 pm
Post subject: SELECTing Rows to Columns - How to Get Column Alias From Table?
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Considering the following query which transposes rows into columns as
desired; please notice that the column name aliases are "hard coded" (e.g.,
AS ABO, AS RHFactor, AS A1, AS A2, etc).

Each row in the table Lookup_TransplantParticipantFacts contains the desired
"column name" for the corresponding FactID value. The row where FactID = 1
has FactName = 'ABO'. Rather than hard coding "AS ABO" (etc) in the SELECT
list, I'd like to somehow return that value ("ABO") - as the column alias in
the result set - from the underlying table. Is there any way I can do that?

SELECT
T.TransplantEventID,
T.DonorID,
MAX(CASE WHEN L.FactID = 1 THEN T.FactValue END) AS ABO,
MAX(CASE WHEN L.FactID = 2 THEN T.FactValue END) AS RHFactor,
MAX(CASE WHEN L.FactID = 3 THEN T.FactValue END) AS A1,
MAX(CASE WHEN L.FactID = 4 THEN T.FactValue END) AS A2,
MAX(CASE WHEN L.FactID = 5 THEN T.FactValue END) AS B1,
MAX(CASE WHEN L.FactID = 6 THEN T.FactValue END) AS B2,
MAX(CASE WHEN L.FactID = 7 THEN T.FactValue END) AS DR1,
MAX(CASE WHEN L.FactID = 8 THEN T.FactValue END) AS DR2
FROM
TransplantEventsXDonorFacts T INNER JOIN
Lookup_TransplantParticipantFacts L ON T.FactID = L.FactID
GROUP BY
T.TransplantEventID,
T.DonorID

-----------------------------------
DDL For Relevant Tables Below:
-----------------------------------

CREATE TABLE Lookup_TransplantParticipantFacts(
FactID int IDENTITY(1,1) NOT NULL,
FactName varchar (30) NOT NULL,
Comments varchar (500) NULL
CONSTRAINT PK_Lookup_TransplantParticipantFacts PRIMARY KEY CLUSTERED
(FactID ASC)
....

CREATE TABLE TransplantEventsXDonorFacts(
TransplantEventID int NOT NULL,
DonorID int NOT NULL,
FactID int NOT NULL,
FactValue varchar(300) NULL,
CONSTRAINT PK_TransplantEventsXDonorFacts PRIMARY KEY CLUSTERED
(
TransplantEventID ASC,
DonorID ASC,
FactID ASC
....


Thanks!

 >> Stay informed about: SELECTing Rows to Columns - How to Get Column Alias From T.. 
Back to top
Login to vote
Ramesh Subramaniyan

External


Since: Mar 16, 2007
Posts: 41



(Msg. 2) Posted: Sat Feb 10, 2007 5:21 am
Post subject: RE: SELECTing Rows to Columns - How to Get Column Alias From Table? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

if u r using sqlserver2005 use PIVOT / UNPIVOT Operator

"Bob Johnson" wrote:

> Considering the following query which transposes rows into columns as
> desired; please notice that the column name aliases are "hard coded" (e.g.,
> AS ABO, AS RHFactor, AS A1, AS A2, etc).
>
> Each row in the table Lookup_TransplantParticipantFacts contains the desired
> "column name" for the corresponding FactID value. The row where FactID = 1
> has FactName = 'ABO'. Rather than hard coding "AS ABO" (etc) in the SELECT
> list, I'd like to somehow return that value ("ABO") - as the column alias in
> the result set - from the underlying table. Is there any way I can do that?
>
> SELECT
> T.TransplantEventID,
> T.DonorID,
> MAX(CASE WHEN L.FactID = 1 THEN T.FactValue END) AS ABO,
> MAX(CASE WHEN L.FactID = 2 THEN T.FactValue END) AS RHFactor,
> MAX(CASE WHEN L.FactID = 3 THEN T.FactValue END) AS A1,
> MAX(CASE WHEN L.FactID = 4 THEN T.FactValue END) AS A2,
> MAX(CASE WHEN L.FactID = 5 THEN T.FactValue END) AS B1,
> MAX(CASE WHEN L.FactID = 6 THEN T.FactValue END) AS B2,
> MAX(CASE WHEN L.FactID = 7 THEN T.FactValue END) AS DR1,
> MAX(CASE WHEN L.FactID = 8 THEN T.FactValue END) AS DR2
> FROM
> TransplantEventsXDonorFacts T INNER JOIN
> Lookup_TransplantParticipantFacts L ON T.FactID = L.FactID
> GROUP BY
> T.TransplantEventID,
> T.DonorID
>
> -----------------------------------
> DDL For Relevant Tables Below:
> -----------------------------------
>
> CREATE TABLE Lookup_TransplantParticipantFacts(
> FactID int IDENTITY(1,1) NOT NULL,
> FactName varchar (30) NOT NULL,
> Comments varchar (500) NULL
> CONSTRAINT PK_Lookup_TransplantParticipantFacts PRIMARY KEY CLUSTERED
> (FactID ASC)
> ....
>
> CREATE TABLE TransplantEventsXDonorFacts(
> TransplantEventID int NOT NULL,
> DonorID int NOT NULL,
> FactID int NOT NULL,
> FactValue varchar(300) NULL,
> CONSTRAINT PK_TransplantEventsXDonorFacts PRIMARY KEY CLUSTERED
> (
> TransplantEventID ASC,
> DonorID ASC,
> FactID ASC
> ....
>
>
> Thanks!
>
>
>

 >> Stay informed about: SELECTing Rows to Columns - How to Get Column Alias From T.. 
Back to top
Login to vote
Bob Johnson

External


Since: Nov 17, 2007
Posts: 11



(Msg. 3) Posted: Sat Feb 10, 2007 6:44 am
Post subject: Re: SELECTing Rows to Columns - How to Get Column Alias From Table? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

RE:
<< sing sqlserver2005 use PIVOT / UNPIVOT Operator>>

Unless I'm missing something, the PIVOT operator does not provide the
functionality I'm looking for. I'm specifically looking for a way to get
*row values* in the underlying table to become *column names* in the result
set. Maybe that's not possible? PIVOT or otherwise? (and yes I'm using SQL
Server 2005).
 >> Stay informed about: SELECTing Rows to Columns - How to Get Column Alias From T.. 
Back to top
Login to vote
Steve Dassin

External


Since: Jul 15, 2007
Posts: 48



(Msg. 4) Posted: Sat Feb 10, 2007 1:25 pm
Post subject: Re: SELECTing Rows to Columns - How to Get Column Alias From Table? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You can do this with RAC using the @replacepvtcols parameter.
In the online documentation check out:
Developers
RAC for Developers
Dynamically Renaming Pivot Columns

www.rac4sql.net

"Bob Johnson" wrote in message

> Considering the following query which transposes rows into columns as
> desired; please notice that the column name aliases are "hard coded"
> (e.g., AS ABO, AS RHFactor, AS A1, AS A2, etc).
>
> Each row in the table Lookup_TransplantParticipantFacts contains the
> desired "column name" for the corresponding FactID value. The row where
> FactID = 1 has FactName = 'ABO'. Rather than hard coding "AS ABO" (etc) in
> the SELECT list, I'd like to somehow return that value ("ABO") - as the
> column alias in the result set - from the underlying table. Is there any
> way I can do that?
>
> SELECT
> T.TransplantEventID,
> T.DonorID,
> MAX(CASE WHEN L.FactID = 1 THEN T.FactValue END) AS ABO,
> MAX(CASE WHEN L.FactID = 2 THEN T.FactValue END) AS RHFactor,
> MAX(CASE WHEN L.FactID = 3 THEN T.FactValue END) AS A1,
> MAX(CASE WHEN L.FactID = 4 THEN T.FactValue END) AS A2,
> MAX(CASE WHEN L.FactID = 5 THEN T.FactValue END) AS B1,
> MAX(CASE WHEN L.FactID = 6 THEN T.FactValue END) AS B2,
> MAX(CASE WHEN L.FactID = 7 THEN T.FactValue END) AS DR1,
> MAX(CASE WHEN L.FactID = 8 THEN T.FactValue END) AS DR2
> FROM
> TransplantEventsXDonorFacts T INNER JOIN
> Lookup_TransplantParticipantFacts L ON T.FactID = L.FactID
> GROUP BY
> T.TransplantEventID,
> T.DonorID
>
> -----------------------------------
> DDL For Relevant Tables Below:
> -----------------------------------
>
> CREATE TABLE Lookup_TransplantParticipantFacts(
> FactID int IDENTITY(1,1) NOT NULL,
> FactName varchar (30) NOT NULL,
> Comments varchar (500) NULL
> CONSTRAINT PK_Lookup_TransplantParticipantFacts PRIMARY KEY CLUSTERED
> (FactID ASC)
> ...
>
> CREATE TABLE TransplantEventsXDonorFacts(
> TransplantEventID int NOT NULL,
> DonorID int NOT NULL,
> FactID int NOT NULL,
> FactValue varchar(300) NULL,
> CONSTRAINT PK_TransplantEventsXDonorFacts PRIMARY KEY CLUSTERED
> (
> TransplantEventID ASC,
> DonorID ASC,
> FactID ASC
> ...
>
>
> Thanks!
>
 >> Stay informed about: SELECTing Rows to Columns - How to Get Column Alias From T.. 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 5) Posted: Sat Feb 10, 2007 6:44 pm
Post subject: Re: SELECTing Rows to Columns - How to Get Column Alias From Table? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Bob Johnson (A@B.com) writes:
> Considering the following query which transposes rows into columns as
> desired; please notice that the column name aliases are "hard coded"
> (e.g., AS ABO, AS RHFactor, AS A1, AS A2, etc).
>
> Each row in the table Lookup_TransplantParticipantFacts contains the
> desired "column name" for the corresponding FactID value. The row where
> FactID = 1 has FactName = 'ABO'. Rather than hard coding "AS ABO" (etc)
> in the SELECT list, I'd like to somehow return that value ("ABO") - as
> the column alias in the result set - from the underlying table. Is
> there any way I can do that?

You would need query the table about the values, and from that information
build a query that you execute with dynamic SQL. As you may guess, it is
quite a road to get there. I can only express my regret that there is no
built-in for this very common request in SQL Server.

I found this item:

https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?Fee...ckID=12

You can add your vote for this. I would bet there are other requests of the same nature.

In the meanwhile, check out http://www.rac4sql.net for a third-party
product that helps you with this task.

--
Erland Sommarskog, SQL Server MVP, esquel.TakeThisOut@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: SELECTing Rows to Columns - How to Get Column Alias From T.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
When can we use column alias in where, group by, having, o.. - I have long been confused about this question. Look, the query below works. select vendorname, count(*) as invqty, avg(invoicetotal) as invavg from vendors join invoices on vendors.vendorid=invoices.vendorid group by vendorname having avg(invoicetotal)...

Selecting rows that do not meet an inner join - HI all I wnat to retrieve rows that do not meet the requirements of a simple inner join If I join two tables , questions and answers, normally I would get all questions that have answers. I would like to list all questions that do not have answers. ..

Selecting Date and time int Columns to Datetime - Hi all, DECLARE @DATE INT DECLARE @TIME INT SET @DATE = '20061201' SET @TIME = '110002' SET NOCOUNT ON SELECT CONVERT(DATETIME, LEFT(CAST(@DATE AS VARCHAR(10)), 4) + '-' + SUBSTRING(CAST(@DATE AS VARCHAR(10)), 5, 2) + '-' +..

Selecting everything in 2 different tables based on a valu.. - I have 5 tables. [Tablen1, Tablen2, Tablex1, Tablex2 and Table3] I need to be able to select all in either tablen1 and tablen2 or tablex1 and tablex2 based on a value from Columnx in table 3. How do you do something like this? That wouldn't be the end...

Rows to Columns - I have a table that stores data as EAV as follows: -------------------------------------------------------------------- ID | Name | Str Value| Date Value| Num Value -------------------------------------------------------------------- 01 | Type | ABC ...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]