| Author |
Topic  |
|
|
JPAucamp
Starting Member
49 Posts |
Posted - 11/16/2012 : 15:56:49
|
Sorry just trying to learn some sql. Can someone please explain the p to me after the From clause. Thanks
USE AdventureWorks
SELECT VendorID, [164] AS Emp1, [198] AS Emp2, [223] AS Emp3,
[231] AS Emp4, [233] AS Emp5
FROM (SELECT
PurchaseOrderID, EmployeeID, VendorID
FROM Purchasing.PurchaseOrderHeader) p
PIVOT ( COUNT (PurchaseOrderID)
FOR EmployeeID IN ( [164], [198], [223], [231], [233] ) ) AS pvt
ORDER BY VendorID
|
|
|
Lamprey
Flowing Fount of Yak Knowledge
3831 Posts |
Posted - 11/16/2012 : 16:27:02
|
The "p" is a table (or this case a derived-table) alias. It is better to use the AS operator to help indentify the alias. Here is some more information: http://msdn.microsoft.com/en-us/library/ms187455(v=sql.90).aspx
|
Edited by - Lamprey on 11/16/2012 16:47:42 |
 |
|
|
JPAucamp
Starting Member
49 Posts |
Posted - 11/16/2012 : 16:38:14
|
| Thanks. So the select statement is creating a table which has an alias of p? Also am i right in saying that in this example in doesn't serve much purpose as the alias in not used again where normally it would save you time not having to write the select again. |
 |
|
|
Lamprey
Flowing Fount of Yak Knowledge
3831 Posts |
Posted - 11/16/2012 : 16:50:57
|
| Well in this case you HAVE to alias a derived table or the SQL will not parse. |
 |
|
|
Jeff Moden
Aged Yak Warrior
USA
643 Posts |
Posted - 11/16/2012 : 18:29:09
|
quote: Originally posted by Lamprey
The "p" is a table (or this case a derived-table) alias. It is better to use the AS operator to help indentify the alias. Here is some more information: http://msdn.microsoft.com/en-us/library/ms187455(v=sql.90).aspx
I guess it's a matter of personal choice because I don't actually believe that using "AS" is better. |
 |
|
| |
Topic  |
|