Click here to Skip to main content
15,922,630 members
Home / Discussions / Database
   

Database

 
QuestionOrder by by using levels using sql Script Pin
indian14322-Feb-16 10:55
indian14322-Feb-16 10:55 
AnswerRe: Order by by using levels using sql Script Pin
Mathi Mani22-Feb-16 14:37
Mathi Mani22-Feb-16 14:37 
GeneralRe: Order by by using levels using sql Script Pin
CHill6025-Feb-16 5:07
mveCHill6025-Feb-16 5:07 
AnswerRe: Order by by using levels using sql Script Pin
CHill6025-Feb-16 5:05
mveCHill6025-Feb-16 5:05 
QuestionRecursive Common Table Expressions Pin
Amr.Mohammad8720-Feb-16 9:10
Amr.Mohammad8720-Feb-16 9:10 
SuggestionRe: Recursive Common Table Expressions Pin
CHill6020-Feb-16 9:43
mveCHill6020-Feb-16 9:43 
GeneralRe: Recursive Common Table Expressions Pin
Amr.Mohammad8720-Feb-16 11:21
Amr.Mohammad8720-Feb-16 11:21 
GeneralRe: Recursive Common Table Expressions Pin
CHill6020-Feb-16 23:57
mveCHill6020-Feb-16 23:57 
You have already largely described what will happen "behind the scenes".

The Anchor member - i.e. the bit of the query after AS and before UNION ALL, is going to return a set of data in which you are interested.

The Recursive member - i.e. the bit after the UNION ALL, keeps calling itself until it has used all of the data from the anchor.

Recursion is usually used when there is a hierarchy or a sequence involved. Perhaps the simplest example I've seen is this SQL SERVER - Simple Example of Recursive CTE - Journey to SQL Authority with Pinal Dave[^]

However, looking at your query and from your description it strikes me that all you need is a simple join rather than a recursive CTE.

[EDIT] - I gave this some more thought and would like to offer this example of what is going on under the covers of an rCTE.

Imagine a simple table (I'm using a cut down version of the table in the Northwind sample database)
SQL
use Sandbox
if exists (select * from sysobjects where id = object_id('dbo.Employees') )
	drop table "dbo"."Employees"
GO
CREATE TABLE "Employees" (
	"EmployeeID" "int" IDENTITY (1, 1) NOT NULL ,
	"LastName" nvarchar (20) NOT NULL ,
	"FirstName" nvarchar (10) NOT NULL ,
	"Title" nvarchar (30) NULL ,
	"ReportsTo" "int" NULL 
)

I've populated this with the same data as the sample database...
SQL
INSERT "Employees"  VALUES('Davolio','Nancy','Sales Representative',2),
		('Fuller','Andrew','Vice President, Sales',NULL),
		('Leverling','Janet','Sales Representative',2),
		('Peacock','Margaret','Sales Representative',2),
		('Buchanan','Steven','Sales Manager',2),
		('Suyama','Michael','Sales Representative',5),
		('King','Robert','Sales Representative',5),
		('Callahan','Laura','Inside Sales Coordinator',2),
		('Dodsworth','Anne','Sales Representative',5)

And created this rCTE query to traverse the hierarchy
SQL
;WITH Emp_CTE AS (
	-- Anchor member - initialise the Level of recursion to 1 
	-- and start the Manager-to-Employee column MtoE
	SELECT EmployeeID, ReportsTo, LastName, FirstName, Title, 1 as RLevel
	,MtoE = CAST(isnull(ReportsTo,0) AS VARCHAR(MAX)) + '/' + CAST(EmployeeID AS VARCHAR(MAX))
	FROM Employees WHERE ReportsTo IS NULL

	UNION ALL

	-- Recursive member - and add on to MtoE the paths we are walking
	SELECT e.EmployeeID, e.ReportsTo, e.LastName, e.FirstName, e.Title, RLevel + 1
	,MtoE=MtoE + '/' + CAST(e.EmployeeID AS VARCHAR(MAX))
	FROM Employees e
	INNER JOIN Emp_CTE ecte ON ecte.EmployeeID = e.ReportsTo
)
SELECT EC.EmployeeID, EC.ReportsTo, EC.LastName, EC.FirstName, EC.Title, RLevel, MtoE
FROM Emp_CTE EC

If you examine the results
Emp.Id  Manager LastName    FirstName   Job Title                Level  RecursionPath
2	NULL	Fuller	    Andrew	Vice President, Sales	 1	0/2
1	2	Davolio	    Nancy	Sales Representative	 2	0/2/1
3	2	Leverling   Janet	Sales Representative	 2	0/2/3
4	2	Peacock	    Margaret	Sales Representative	 2	0/2/4
5	2	Buchanan    Steven	Sales Manager	         2	0/2/5
8	2	Callahan    Laura	Inside Sales Coordinator 2	0/2/8
6	5	Suyama	    Michael	Sales Representative	 3	0/2/5/6
7	5	King	    Robert	Sales Representative	 3	0/2/5/7
9	5	Dodsworth   Anne	Sales Representative	 3	0/2/5/9
Those last two columns sort of describe the goings-on in the background. You can add something similar to your own query to see what is going on (I haven't used your query because there is no sample data for me to set up the appropriate tables)

modified 21-Feb-16 11:50am.

GeneralRe: Recursive Common Table Expressions Pin
Amr.Mohammad8721-Feb-16 7:04
Amr.Mohammad8721-Feb-16 7:04 
GeneralRe: Recursive Common Table Expressions Pin
Jörgen Andersson21-Feb-16 7:31
professionalJörgen Andersson21-Feb-16 7:31 
GeneralRe: Recursive Common Table Expressions Pin
CHill6021-Feb-16 8:04
mveCHill6021-Feb-16 8:04 
GeneralRe: Recursive Common Table Expressions Pin
Jörgen Andersson21-Feb-16 21:34
professionalJörgen Andersson21-Feb-16 21:34 
GeneralRe: Recursive Common Table Expressions Pin
CHill6021-Feb-16 22:19
mveCHill6021-Feb-16 22:19 
GeneralRe: Recursive Common Table Expressions Pin
Jörgen Andersson21-Feb-16 22:30
professionalJörgen Andersson21-Feb-16 22:30 
GeneralRe: Recursive Common Table Expressions Pin
Amr.Mohammad8722-Feb-16 10:28
Amr.Mohammad8722-Feb-16 10:28 
QuestionEntity Pin
mohsen47319-Feb-16 21:03
mohsen47319-Feb-16 21:03 
QuestionSQLPrepare error: [Microsoft][ODBC Microsoft Access Driver] Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'. Pin
Member 1233569519-Feb-16 17:48
Member 1233569519-Feb-16 17:48 
Questionhow to set/retrieve data directory and index directory of a table of mysql database Pin
CanadaProgrammer16-Feb-16 23:37
CanadaProgrammer16-Feb-16 23:37 
AnswerRe: how to set/retrieve data directory and index directory of a table of mysql database Pin
Richard MacCutchan16-Feb-16 23:54
mveRichard MacCutchan16-Feb-16 23:54 
QuestionHow to setup a local database for my application Pin
Theo Buys16-Feb-16 22:55
Theo Buys16-Feb-16 22:55 
AnswerRe: How to setup a local database for my application Pin
Richard MacCutchan16-Feb-16 23:52
mveRichard MacCutchan16-Feb-16 23:52 
GeneralRe: How to setup a local database for my application Pin
Theo Buys17-Feb-16 5:31
Theo Buys17-Feb-16 5:31 
GeneralRe: How to setup a local database for my application Pin
Richard MacCutchan17-Feb-16 5:42
mveRichard MacCutchan17-Feb-16 5:42 
GeneralRe: How to setup a local database for my application Pin
Theo Buys17-Feb-16 7:24
Theo Buys17-Feb-16 7:24 
QuestionWhat is wrong with my query? Pin
samflex11-Feb-16 4:57
samflex11-Feb-16 4:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.