Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
vidhya
Posting Yak Master
108 Posts |
Posted - 2008-10-25 : 01:59:45
|
| hi friends,I am having 3 tables namely kr_reference, kr_category, kr_referencehistory.In kr_reference table i am having the fields refid,catidIn kr_category table i am having the fields catid,catname,parentidIn kr_referencehistory i am having the fields linkid,refid,title,url,avgrating.From the above tables i need to display refid, categoryname, subcategory, titlei used below query to display category,subcategorySELECT t1.name as t1, t2.name as t2 FROM kr_category t1LEFT JOIN kr_category t2 ON t1.catid=t2.parentidWHERE t1.parentid=0i used the below query to display the refid, titleselect h.title as title,r.refid as refid from kr_reference r inner join kr_category c on c.catid=r.catid and r.deleted=0 and c.parentid=6 inner join kr_referencehistory h on h.refid=r.refid and h.status=1 left join kr_referencerating rf on r.refid=rf.refid I need to combine two queries to get my expected outputrefid,title,category,subcategory.Thanks in Advance. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-25 : 02:05:21
|
| did you try method in link i posted earlierhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=113202 |
 |
|
|
malaytech2008
Yak Posting Veteran
95 Posts |
Posted - 2008-10-25 : 02:24:08
|
| Try this:select r.refid as refid, a.catname as categoryname,b.catname as subcategory,rh.title as titlefrom kr_category a inner join kr_category b on a.catid=b.parentidinner join kr_reference r on r.catid=b.catid inner join kr_referencehistory rh on rh.refid=r.refidmalay |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-25 : 02:26:07
|
quote: Originally posted by malaytech2008 Try this:select r.refid as refid, a.catname as categoryname,b.catname as subcategory,rh.title as titlefrom kr_category a inner join kr_category b on a.catid=b.parentidinner join kr_reference r on r.catid=b.catid inner join kr_referencehistory rh on rh.refid=r.refidmalay
your solution will work only for 1 level. what if record has a child which has child and so on... |
 |
|
|
malaytech2008
Yak Posting Veteran
95 Posts |
Posted - 2008-10-25 : 02:37:19
|
| According to you,root category name should show with grand child category name.malay |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-25 : 03:13:55
|
| [code]declare @t table (parentid int,childid int,parent varchar(20), child varchar(20), lev int, fullpath varchar(1000))declare @lev intset @lev = 0--Get Root node(s)insert @t (parentid,childid, parent, child, lev, fullpath)select null,catid,null, catname, @lev, catnamefrom kr_categorywhere parentid=0while @@rowcount > 0begin set @lev = @lev + 1 --Get all children of current level insert @t (parentid, childid,parent,child, lev, fullpath) select c.parentid, c.catid,t.catname,c.catname, @lev, t.fullpath + '.' + c.catname from @t t join kr_category c on c.parentid = t.childid and t.lev = @lev-1 left join @t x on x.parentid = c.parentid and x.childid = c.catidwhere x.parentid is nullendselect * from @t order by fullpathselect * from @t tjoin kr_reference ron t.childid=r.catidand r.deleted=0 and t.parentid=6join kr_referencehistory rhon rh.refid=r.refidand rh.status=1left join kr_referencerating rf on r.refid=rf.refid order by fullpath[/code] |
 |
|
|
vidhya
Posting Yak Master
108 Posts |
Posted - 2008-10-25 : 03:37:02
|
| visakh,i am getting error Server: Msg 207, Level 16, State 3, Line 16Invalid column name 'catname'.catname is in kr_category table but why it show error. |
 |
|
|
vidhya
Posting Yak Master
108 Posts |
Posted - 2008-10-25 : 03:38:24
|
| malay,You are absolutely correct. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-25 : 03:57:00
|
try this small modificationdeclare @t table (parentid int,childid int,parent varchar(20), child varchar(20), lev int, fullpath varchar(1000))declare @lev intset @lev = 0--Get Root node(s)insert @t (parentid,childid, parent, child, lev, fullpath)select null,catid,null, catname, @lev, catnamefrom kr_categorywhere parentid=0while @@rowcount > 0begin set @lev = @lev + 1 --Get all children of current level insert @t (parentid, childid,parent,child, lev, fullpath) select c.parentid, c.catid,t.child,c.catname, @lev, t.fullpath + '.' + c.catname from @t t join kr_category c on c.parentid = t.childid and t.lev = @lev-1 left join @t x on x.parentid = c.parentid and x.childid = c.catidwhere x.parentid is nullendselect * from @t order by fullpathselect * from @t tjoin kr_reference ron t.childid=r.catidand r.deleted=0 and t.parentid=6join kr_referencehistory rhon rh.refid=r.refidand rh.status=1left join kr_referencerating rf on r.refid=rf.refid order by fullpath |
 |
|
|
|
|
|
|
|