Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple WITH syntax, Recrusive query and Pivot why are they better?

Posted on 2007-03-25
4
Medium Priority
?
281 Views
Last Modified: 2010-03-19
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],[Apr],[May],[Jun],
[Jul],[Aug],[Sep],[Oct],[Nov],[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],[Nov],[Dec])
) As ourPivot
Order by Year


0
Comment
Question by:dylanone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
David Todd earned 100 total points
ID: 18790454
Hi,

The key problem is that the CTE using the With clause - the example above is too simple.

It needs to be something more complex like
select *
from SalesInJan
inner join SalesInJan
 on oncondition
where SalesInJan ... etc

That is, where the expression is repeated several times in the main query, the CTE simplifies the writing of the query is all.

HTH
  David
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18790674
the first sample is indeed too simplistic, but as dtodd indicates, the power only get's seen when you have to use that "subquery" several times.
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).
0
 

Author Comment

by:dylanone
ID: 18794654
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?
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 18794820
>so your saying the WITh and Recursive queries only help if I need to call them over and over again?
no, at least not how you understood it.

the WITH is only "helpful" in regards to performance when you have 1 query, where you would use the same subquery several times. the WITH then simplified the writing of the query AND can improve performance.

calling the same query over and over again can be optimized by using a parametrized and prepared query, using stored procedures/functions etc. that can be combine with WITH (by using that inside the procedure for example).
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question