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 |
|
venkatakondareddy
Starting Member
17 Posts |
Posted - 2009-01-06 : 05:16:40
|
Hi all,i want to know how to write a hierarchical query with two table. Here im givig two sample tables which i have taken.Manager table-------------Emp TableMgrNo---MgrId ----- EmpNo--MgrNo---Sal 1 -- NULL ------ 1 - 2 - 8000 2 -- 1 ------ 2 - 2 - 8000 3 -- 2 ------ 3 - 4 - 7000 4 -- 2 ------ 4 - 10 - 6500 5 -- 3 ------ 5 - 7 - 4000 6 -- 4 ------ 6 - 2 - 7000 7 -- 3 ------ 7 - 2 - 6500 8 -- 5 ------ 8 - 2 - 6500 9 -- 6 ------ 9 - 3 - 6000 10 -- 4 ----- 10 - 5 - 5000 11 -- 8 ------ 12 -- 9 ------ 13 -- 2 ------ Now i want to sum of sal in the tree of MgrNo=2 and also count of EmpNo's of MgrNo=2 From Emp table and Manager table.My Expected Output isMgrNo(Manager table)--MgrId--Sum(sal)--Count(EmpNo). In this format.Pleae can any one help me.RegardsVenkat. |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-01-06 : 05:36:25
|
| don't post duplicate it is already post in the new sql server |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-01-06 : 07:13:30
|
| Use Common Table Expressions To get hierarchy resultsJai Krishna |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-01-06 : 07:16:25
|
| SELECT MgrNo,SUM(sal),COUNT(EmpNo) FROM Managertable MtINNER JOIN EmpTable ON Et(Mt.Mgrno = Et.MgrNo) GROUP BY MgrNoJai Krishna |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-01-06 : 07:58:38
|
| Try Thisdeclare @table table(MgrNo int,MgrId int,empno int ,sal numeric(18,2));with cte(MgrNo,MgrId,empno,sal)as( select m.mgrno,m.mgrid,e.empno,e.sal from manager m left join emp e on (m.mgrno = e.mgrno) where m.mgrid is null union all select m.mgrno,m.mgrid,e.empno,e.sal from manager m inner join cte c on (m.mgrid = c.mgrno) inner join emp e on (m.mgrno = e.mgrno))insert into @tableselect distinct * from cte order by mgrnoselect mgrno,count(distinct empno) as EmployeeCount,sum(sal) as SalarySum from @table group by mgrnoJai Krishna |
 |
|
|
venkatakondareddy
Starting Member
17 Posts |
Posted - 2009-01-07 : 00:51:13
|
| Thanks Jai Krishna for your response,I got output, but not expected.I got this outputMgrNo-- EmpCount-- salarysum 1 ----- 0 ----- NULL 2 ------ 6 ----- 42000.00 3 ------ 2 ----- 12500.00 4 ------ 2 ----- 12500.00 5 ------ 1 ----- 7000.00 7 ------ 1 ----- 7000.00 10------ 1 ----- 4000.00 This is not expected output.Expected is MgrNo-- EmpCount-- Sal 1 -- 10 -- 64500.00 2 -- 10 -- 64500.00 3 -- 3 -- 15000.00 4 -- 2 -- 13500.00 5 -- 1 -- 5000.00 7 -- 1 -- 4000.00 10 -- 1 -- 6500.00I will explain how 1, 2 and 3 are having, 1 is parent for all so all emps comes under 1, 2 is child for 1 and parent for remaining all, 2 already having 5 EmpNo's directly in emp table and their childs are 3,4,5,7 and 10.3 is child for 2 and 1, also parent for 5 and 7. Here 3 directly having 1 EmpNo and child EmpNos are 2. sum of sal also same as EmpNo. ThanksVenkat |
 |
|
|
venkatakondareddy
Starting Member
17 Posts |
Posted - 2009-01-09 : 04:31:26
|
| hi all, here im giving same as above but tables are differ and also query which i have written.Asset table1AssetId AstType ParentAssetId1 1 NULL2 2 13 3 24 4 35 4 36 4 37 4 38 5 49 7 410 5 411 6 812 6 813 6 814 6 815 8 916 8 917 9 1518 9 1519 7 520 7 521 7 522 7 523 5 624 5 625 5 626 5 627 7 728 5 729 5 730 10 1931 10 1932 10 1933 10 1934 10 1935 10 1936 10 1937 10 2038 10 2039 10 2040 10 2041 10 2042 10 2043 10 2044 10 2045 10 2046 10 2047 10 2048 10 2249 10 2250 10 2251 10 2252 10 2253 8 2254 10 2255 10 2256 8 2257 10 2258 10 2259 10 2260 10 2261 8 2262 10 2263 10 2264 10 2265 6 2466 6 2467 6 2468 6 2469 6 2470 6 2471 6 2472 6 2473 6 2474 6 2475 10 2776 10 2777 10 2778 8 2779 10 2780 10 2781 10 2782 10 2783 10 2784 10 2785 10 2786 10 2787 10 2788 10 2789 10 2790 10 2791 10 2792 10 2793 6 2894 6 2895 6 2896 6 2897 6 2898 6 2899 6 28100 6 28101 6 28102 6 28103 6 29104 6 29105 6 29106 9 78107 9 78108 9 78109 3 2122 3 2124 4 122126 4 109127 5 126130 4 122131 4 109133 3 2135 4 133138 4 133139 3 2140 4 139141 4 139143 16 11144 16 11145 16 11146 16 11147 12 143148 12 143149 13 152150 13 144151 16 13152 16 13153 16 13154 16 13155 16 13156 13 144157 13 152158 4 139159 4 139Issue tbale is:IssueId AssetId5 27 12212 213 214 215 217 13818 219 13520 222 225 426 827 14931 1033 140Now i want to Count of IssueId in the tree of AssetId=2 From Asset table and Issue table.I will explain how 1, 2 and 3 are having, 1 is parent for all AssetId comes under 1, 2 is child for 1 and parent for remaining all, 2 already having 8 IssueIds directly in Issue table and their childs are 4, 8, 10, 122, 135, 138, 140 and 149. 4 is child for 2 and 1, its having directly one issue, and also parent for 8, 10, 149. like we need to count all issues from IssueTable and which has to map with asset table.Expected Output is:AssetId CountOfIssueId1 162 163 44 45 06 07 08 29 010 111 012 013 114 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 0100 0101 0102 0103 0104 0105 0106 0107 0108 0109 0122 1124 0126 0127 0130 0131 0133 2135 1138 1139 1140 1141 0143 0144 0145 0146 0147 0148 0149 1150 0151 0152 1153 0154 0155 0156 0157 0158 0159 0My query is:WITH REPORTS1(AssetId, ParentAssetId, IssueId)AS(SELECT Asset.AssetId, ParentAssetId, IssueId FROM Asset LEFT OUTER JOIN IssueON Asset.AssetId= Issue.AssetId ),REPORTS2AS(SELECT AssetId AS Asset, AssetId AS Asset2, IssueId FROM REPORTS1 UNION ALL SELECT R2.Asset, R1.AssetId, R1.IssueId FROM REPORTS2 R2 JOIN REPORTS1 R1 ONR2.Asset2=R1.ParentAssetId)SELECT Asset, COUNT(IssueId) AS CountIssues FROM REPORTS2GROUP BY Asset;this query given output is first 2 rows 76,76 and remaining all same.Please any one can help me.. Thanks Venkat.. |
 |
|
|
|
|
|
|
|