Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


recursive query


recursive query

Author
Message
tspot
tspot
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 5
Hi,

I have these tables :

TABLE1
ID NAME PARENTID
=== ========= ========
1 SALES -1
2 MIS -1
3 LANG 1
4 RAMA 3
5 ANDRE 2
6 ROY 5


TABLE2
DATE ID QTY
===== === =====
01/01 3 1
01/01 5 2
02/01 6 6



How to make a query for below result:

DATE DEPT QTY
--------------------
01/01 SALES 1
01/01 MIS 2
02/01 MIS 6

Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44983 Visits: 39870
That won't be a recurrsive query... it'll be a simple join between the two tables joined on ID.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
tspot
tspot
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 5
But if you see the data carefully the name that i want to display is department name
krayknot
krayknot
Old Hand
Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)Old Hand (348 reputation)

Group: General Forum Members
Points: 348 Visits: 534
tspot (11/5/2008)
Hi,

I have these tables :

TABLE1
ID NAME PARENTID
=== ========= ========
1 SALES -1
2 MIS -1
3 LANG 1
4 RAMA 3
5 ANDRE 2
6 ROY 5


TABLE2
DATE ID QTY
===== === =====
01/01 3 1
01/01 5 2
02/01 6 6



How to make a query for below result:

DATE DEPT QTY
--------------------
01/01 SALES 1
01/01 MIS 2
02/01 MIS 6


What is the criteria of joining between these two tables, i mean how can we relate these two tables

kshitij kumar
kshitij@krayknot.com
www.krayknot.com
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2090 Visits: 22778
WITH CTE AS (
SELECT ID,NAME AS DEPT
FROM TABLE1
WHERE PARENTID=-1
UNION ALL
SELECT t.ID,c.DEPT
FROM CTE c
INNER JOIN TABLE1 t ON t.PARENTID=c.ID)
SELECT t.DATE,c.DEPT,t.QTY
FROM CTE c
INNER JOIN TABLE2 t ON t.ID=c.ID
ORDER BY t.DATE,t.QTY



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




tspot
tspot
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 5
The ID in table2 is the same ID with Table1->ID (not parent)
BTW, Mark has solved this issue
tspot
tspot
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 5
Hi Mark,

It works...thank you very much for the answer and your time

rgds,
richard
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search