dbTalk Databases Forums  

change structure of a simple query

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss change structure of a simple query in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ina
 
Posts: n/a

Default change structure of a simple query - 02-25-2008 , 02:53 AM






Hello,

I would like to do a little query in T-SQL.

I have two tables:

PERSON (PERSONID, PERSON)
PHONE ( PHONEID, PERSONID, TYPE, PHONE) -- TYPE can be (business, fax,
business 2)

If I do a simple select I will get

SELECT PERSONID, PESON, TYPE, PHONE FROM PERSON PE INNER JOIN PHONE P
ON P.PERSONID = PE.PERSONID

Result:

PERSONID PERSON TYPE PHONE
1 Rose business xxxxxxxxx
1 Rose business xxxxxxxxxx
1 Rose business2 xxxxxxxxxxx
1 Rose fax
xxxxxxxxxxx


How to obtain the following

PERSONID PERSON BUSINESS BUSINESS2 BUSINESS3 FAX
1 Rose business xxxxxxxxx
xxxxxxx xxxxxxxxxx


any help appreciate
Thank you

Ina

Reply With Quote
  #2  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: change structure of a simple query - 02-25-2008 , 07:50 AM






It is better if the table can have the different type phones as columns
rather than rows.

To get the results with your current tables you need to do pivoting.

-- SQL Server 2000
SELECT P.personid, P.person,
MAX(CASE WHEN T.type = 'business' THEN T.phone END) AS business,
MAX(CASE WHEN T.type = 'business2' THEN T.phone END) AS business2,
MAX(CASE WHEN T.type = 'fax' THEN T.phone END) AS fax
FROM Person AS P
JOIN Phone AS T
ON P.personid = T.personid
GROUP BY P.personid, P.person

-- SQL Server 2005
SELECT personid, person,
[business],
[business2],
[fax]
FROM
(SELECT P.personid, P.person,
T.type, T.phone
FROM Person AS P
JOIN Phone AS T
ON P.personid = T.personid) AS T
PIVOT
(MAX(phone) FOR [type] IN ([business], [business2], [fax])) AS P

The above queries will work if you have unique phone type list per person.
If you may have duplicate types, then you have to add one more step to add a
sequence to the duplicate types:

-- SQL Server 2005
SELECT T.personid, T.person,
MAX(CASE WHEN T.type = 'business' AND seq = 1 THEN T.phone END) AS
business1,
MAX(CASE WHEN T.type = 'business' AND seq = 2 THEN T.phone END) AS
business2,
MAX(CASE WHEN T.type = 'fax' THEN T.phone END) AS fax
FROM
(SELECT P.personid, P.person,
T.type, T.phone,
ROW_NUMBER() OVER(PARTITION BY
P.personid, T.type
ORDER BY T.phone) AS seq
FROM Person AS P
JOIN Phone AS T
ON P.personid = T.personid) AS T
GROUP BY T.personid, T.person;

HTH,

Plamen Ratchev
http://www.SQLStudio.com


Reply With Quote
  #3  
Old   
Ina
 
Posts: n/a

Default Re: change structure of a simple query - 02-25-2008 , 10:42 AM



On Feb 25, 2:50*pm, "Plamen Ratchev" <Pla... (AT) SQLStudio (DOT) com> wrote:
Quote:
It is better if the table can have the different type phones as columns
rather than rows.

To get the results with your current tables you need to do pivoting.

-- SQL Server 2000
SELECT P.personid, P.person,
* * * * * MAX(CASE WHEN T.type = 'business' THEN T.phone END) AS business,
* * * * * MAX(CASE WHEN T.type = 'business2' THEN T.phone END)AS business2,
* * * * * MAX(CASE WHEN T.type = 'fax' THEN T.phone END) AS fax
FROM Person AS P
JOIN Phone AS T
* ON P.personid = T.personid
GROUP BY P.personid, P.person

-- SQL Server 2005
SELECT personid, person,
* * * * * [business],
* * * * * [business2],
* * * * * [fax]
FROM
(SELECT P.personid, P.person,
* * * * * *T.type, T.phone
*FROM Person AS P
*JOIN Phone AS T
* *ON P.personid = T.personid) AS T
PIVOT
(MAX(phone) FOR [type] IN ([business], [business2], [fax])) AS P

The above queries will work if you have unique phone type list per person.
If you may have duplicate types, then you have to add one more step to adda
sequence to the duplicate types:

-- SQL Server 2005
SELECT T.personid, T.person,
* * * *MAX(CASE WHEN T.type = 'business' AND seq = 1 THEN T.phone END) AS
business1,
* * * *MAX(CASE WHEN T.type = 'business' AND seq = 2 THEN T.phone END) AS
business2,
* * * *MAX(CASE WHEN T.type = 'fax' THEN T.phone END) AS fax
FROM
(SELECT P.personid, P.person,
* * * * * *T.type, T.phone,
* * * * * *ROW_NUMBER() OVER(PARTITION BY
* * * * * * * * * *P.personid, T.type
* * * * * * * * * ORDER BY T.phone) AS seq
*FROM Person AS P
*JOIN Phone AS T
* *ON P.personid = T.personid) AS T
GROUP BY T.personid, T.person;

HTH,

Plamen Ratchevhttp://www.SQLStudio.com
Oh thank you so much


Ina


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.