SQL Clone
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 Guru
SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)SSC Guru (212K reputation)

Group: General Forum Members
Points: 212565 Visits: 41977
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

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
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1854 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
SSCertifiable
SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6359 Visits: 25530
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