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 |
khanewal
Starting Member
33 Posts |
Posted - 2010-09-20 : 01:50:01
|
Doc_id ref1 null2 null3 14 15 2Required Results:Doc_ID total1 22 1how I can achieve these results ?many thanks, |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-09-20 : 02:00:19
|
Try this:Select ref as doc_id, count(1) from yourtablegroup by refRegards,BohraI am here to learn from Masters and help new bees in learning. |
 |
|
PavanKK
Starting Member
32 Posts |
Posted - 2010-09-20 : 02:23:51
|
Hi Khanewal,If am not wrong you are trying to get the total recursive count of all children under a parent node. If that is the case you can try the following. If am wrong let us know in detail to help you better.-----DECLARE @T TABLE (Doc_id INT, ref INT)INSERT @t VALUES(1,null)INSERT @t VALUES(2,null)INSERT @t VALUES(3,1)INSERT @t VALUES(4,1)--INSERT @t VALUES(6,3)INSERT @t VALUES(5,2);WITH Cte AS(SELECT t2.Doc_id,t2.ref,t1.Doc_id AS ParentFROM @T t1JOIN @T t2 ON t1.Doc_id = t2.ref AND t1.ref IS NULLUNION ALLSELECT t.Doc_id,t.ref,C.ParentFROM @T tJOIN Cte c ON C.Doc_id = t.ref)SELECT Parent AS Doc_ID,COUNT(Doc_id) AS TotalFROM CteGROUP BY Parent-------KK |
 |
|
|
|
|
|
|