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
Nassa (nassim.czdashti@gmail.com) writes: [quoted text, click to view] > 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,FK:telNumbers.telbookid=telbook.id > > Teltypes(teltypes:char,fk:int),FK:telnumbers.telnotype=teltypes.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?
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
Hi Erland Sommarskog, Thank you so much. I have to use PIVOT. I found the solution. Cheers, Nassa [quoted text, click to view] Erland Sommarskog wrote: > Nassa (nassim.czdashti@gmail.com) writes: > > 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? > > 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 =3D MAX(CASE WHEN t.teltypes =3D 'Tellphone' > THEN n.telno > END), > Mobile =3D MAX(CASE WHEN t.teltypes =3D 'Mobile' > THEN n.telno > END), > Fax =3D MAX(CASE WHEN t.teltypes =3D 'Fax' > THEN n.telno > END) > FROM TelNumbers n > JOIN Teltypes t ON n.telNotype =3D t.fk > GROUP BY n.telbookid) AS a ON a.telbookid =3D 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 > http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
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] Erland Sommarskog wrote: > Nassa (nassim.czdashti@gmail.com) writes: > > Thank you so much. > > I have to use PIVOT. > > I found the solution. > > 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 > http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Nassa (nassim.czdashti@gmail.com) writes: [quoted text, click to view] > Thank you so much. > I have to use PIVOT. > I found the solution.
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
Nassa (nassim.czdashti@gmail.com) writes: [quoted text, click to view] > 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.
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
[quoted text, click to view] Erland Sommarskog wrote: > Nassa (nassim.czdashti@gmail.com) writes: > > 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. > > 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.
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.
Don't see what you're looking for? Try a search.
|