all groups > sql server programming > december 2006 >
You're in the

sql server programming

group:

PIVOT


PIVOT Nassa
12/29/2006 10:46:38 PM
sql server programming: Hello Everyone,

I have 3 tables:
Telbook(Id:int,Name:char,address:char,comment:ntext,owneruserid:int),
PK:id
TelNumbers(telbookid:int,telno:char,telNotype:int,syscode:int),PK:syscode,F=
=ADK:telNumbers.telbookid=3Dtelbook.id

Teltypes(teltypes:char,fk:int),FK:telnumbers.telnotype=3Dteltypes.fk


The question is here that I can create a query which results are:
(id,Name,telno,telnotype,teltypes,address,comment)
(4,nassa,091463738,2,Mobile,XXX,Null)
(4,nassa,071163738,1,Tellphone,XXX,Nul)


But,I want a query which shows the results in a way below:
(id,Name,tellephone,mobile,Fax,e-mail,address,comment)
(4,nassa,071163738,091463738,Null,Null,XXX,Null)

would you mind telling me how to solve it with PIVOT?

Thanks,
Nassa
Re: PIVOT Erland Sommarskog
12/30/2006 4:52:40 PM
Nassa (nassim.czdashti@gmail.com) writes:
[quoted text, click to view]

From a post that I did in the same thread in comp.databases.ms-sqlserver:

SELECT b.id, b.Name, a.Tellphone, a.Mobile, a.Fax, b.address, b.comment
FROM Telbook b
JOIN (SELECT n.telbookid,
Tellphone = MAX(CASE WHEN t.teltypes = 'Tellphone'
THEN n.telno
END),
Mobile = MAX(CASE WHEN t.teltypes = 'Mobile'
THEN n.telno
END),
Fax = MAX(CASE WHEN t.teltypes = 'Fax'
THEN n.telno
END)
FROM TelNumbers n
JOIN Teltypes t ON n.telNotype = t.fk
GROUP BY n.telbookid) AS a ON a.telbookid = b.id

This is, in my opinion, *the* way to write a pivot query. It's uses
ANSI SQL, so it has a chance of being portable. And once you have learnt
the principle, it's easy to remember.

The trick is the use of MAX. Each CASE expression will return at most
one non-NULL value. So whether we use MAX or MIN does not matter

The syntax with the PIVOT keyword introduced in SQL 2005, on the other
hand, is useless in my opinion. The syntax is not any more compact than
the above, nor even any easier to use or remember. And top of that it's
propritary and not portable. Possibly, it helps the optimizer so a query
with PIVOT could execute faster than using CASE and GROUP BY. Personally,
I haven't even bothered to learn the PIVOT/UNPIVOT syntax.

And since you insist on using ntext, PIVOT is going to be difficult
anyway.

--
Erland Sommarskog, SQL Server MVP, esquel@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
Re: PIVOT Nassa
12/31/2006 9:19:26 PM
Hi Erland Sommarskog,

Thank you so much.
I have to use PIVOT.
I found the solution.

Cheers,
Nassa
[quoted text, click to view]
Re: PIVOT Nassa
1/1/2007 2:41:20 AM
Hi Sommarskog,

Thank you so much.
I need high performance, but the query cost of the query that u
write(80%) is higher than the query cost of the PIVOT(20%) that I
found.

Thanks,
Nassa

[quoted text, click to view]
Re: PIVOT Erland Sommarskog
1/1/2007 10:11:30 AM
Nassa (nassim.czdashti@gmail.com) writes:
[quoted text, click to view]

You have to use PIVOT? That can only mean one thing, this is a class
assignment. Well, tell you teacher that what counts is the solution, not
the keyword used. You could recommend him to read Iztik Ben-Gan's book
"Inside SQL Server 2005: T-SQL Querying", where he also notes that pivot
queries are best done without the PIVOT keyword.

--
Erland Sommarskog, SQL Server MVP, esquel@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
Re: PIVOT Erland Sommarskog
1/1/2007 11:50:28 AM
Nassa (nassim.czdashti@gmail.com) writes:
[quoted text, click to view]

Interesting. One would expect the optimizer to be able to do better
with a tailored construct, that's true. But when it comes to get good
performance, there is all reason to try several different ways to write
the query, and of course also review indexing.




--
Erland Sommarskog, SQL Server MVP, esquel@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
Re: PIVOT Shuurai
1/2/2007 8:52:18 AM

[quoted text, click to view]

Personally, I have found the PIVOT to be one of the nicest new toys in
2005. It's easy to use, and the performance improvement can be
extremely good at times.
AddThis Social Bookmark Button