dylanone
asked on
Simple WITH syntax, Recrusive query and Pivot why are they better?
I am currently starting to look at SQL Server 2005 in depth and three advantages I kept reading about are the WITH syntax, Recrusive query and Pivot - can someone body tell me the practical instances of using a WITH, Recrusive or Pivot - All the examples below work - however I don't see what all the hype is about -
what makes these methods better and when would I use them in say a web based application?
Example of a WITh query:
WITH SalesInJan AS (
SELECT * FROM SalesbyMonth
WHERE month = 'jan' )
SELECT * FROM SalesinJan
-------------------------- ---------- ---------
Recrusive query example:
WITH pbbActionHeirarchy As (
SELECT pbbActions.action_id, pbbActions.parentid
FROM pbbActions
WHERE parentid = 1
UNION ALL
SELECT pbbActions.action_id, pbbActions.parentid
FROM pbbActions
INNER JOIN pbbActionHeirarchy On pbbActions.parentid =
pbbActionHeirarchy.action_ id
)
-------------------------- ---------- --------
PIVOT example:
SELECT Year,[Jan],[Feb],[Mar],[Ap r],[May],[ Jun],
[Jul],[Aug],[Sep],[Oct],[N ov],[Dec]
FROM (
SELECT year, amount, month
FROM salesbyMonth ) AS salesbyMonth
PIVOT ( SUM(amount) FOR month IN
([Jan],[Feb],[Mar],[Apr],[ May],[Jun] ,
[Jul],[Aug],[Sep],[Oct],[N ov],[Dec])
) As ourPivot
Order by Year
what makes these methods better and when would I use them in say a web based application?
Example of a WITh query:
WITH SalesInJan AS (
SELECT * FROM SalesbyMonth
WHERE month = 'jan' )
SELECT * FROM SalesinJan
--------------------------
Recrusive query example:
WITH pbbActionHeirarchy As (
SELECT pbbActions.action_id, pbbActions.parentid
FROM pbbActions
WHERE parentid = 1
UNION ALL
SELECT pbbActions.action_id, pbbActions.parentid
FROM pbbActions
INNER JOIN pbbActionHeirarchy On pbbActions.parentid =
pbbActionHeirarchy.action_
)
--------------------------
PIVOT example:
SELECT Year,[Jan],[Feb],[Mar],[Ap
[Jul],[Aug],[Sep],[Oct],[N
FROM (
SELECT year, amount, month
FROM salesbyMonth ) AS salesbyMonth
PIVOT ( SUM(amount) FOR month IN
([Jan],[Feb],[Mar],[Apr],[
[Jul],[Aug],[Sep],[Oct],[N
) As ourPivot
Order by Year
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorta helps - a lot of what I do is web based so your saying the WITh and Recursive queries only help if I need to call them over and over again?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
note: you can easily avoid using WITH by creating temp tables or table variables in case you need to write scripts that need to work on sql 2000 also.
for recursive queries, WITH CTE is a "requirement", or otherwise you have to write a recursive function to solve the problem.
for PIVOT, I honestly have to admit that I doubt the usefulness of the current syntax, as it is still limited in the number of columns (as you still have to specify the possible values).