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 |
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-04-28 : 07:56:23
|
| Hii tried on CTE but iam not gettng correct result plz help meDeclare @TMaster Table (MasterId int, Type varchar(100), ParentMasterId int)Insert into @TmasterSelect 1, 'Fixed', 14 Union AllSelect 2, 'Flexible',14 union AllSelect 3, 'Others',14 union AllSelect 13, 'Retirement benefits',1 union AllSelect 14, 'PBHeads', 0 Select * From @TmasterDeclare @TDetails Table ( MasterId Int, Description varchar(100))Insert into @TDetailsSelect 1, 'Basic' union allSelect 1,'Conveyance' union allSelect 1,'HRA' union allSelect 1,'Special Allowance' union allSelect 2, 'Children Education' union allSelect 2,'Travel Allowance' union allSelect 2,'Medical Expenses' union allSelect 2,'Variable Spl Pay' union allSelect 3, 'Project Allowance' union allSelect 3,'Quarterly Incentive' union allSelect 3,'Shift Allowance' union allSelect 3,'Annual Component' union allSelect 13,'Provident Fund' union allSelect 13,'Gratuity' union allSelect 13,'Super annuation' Select * From @TDetailsin group list we have to show fixed, flexible, otherin subgroups(L1) list under fixed we have to showBasic salary, HRA, Conveyance, Special Allowance,Retirement benefitsin subgroup(L2) we have to show Gratuity, providedfund, Super annuation they are subgroups of retirementbenefitsoutput :Group Subgroup(L1) Subgroup(L2)Fixed Basic salary HRA Conveyance Special Allowance Retirement benefits Provident Fund Gratuity Super annuation Flexible reimbursements LTA/Group Travel Medical expenses Children education variable special pay Others Project allowance Performance incentive Medical insurance |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-28 : 08:00:40
|
See Books Online for recursive CTE. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-04-28 : 08:03:05
|
| i tried but not getting the records correctly;WITH Modules ( ParentMasterId, Type, MasterId, Descriptions) AS(SELECT LM.ParentMasterId,LM.Type, LM.MasterId, LD.DescriptionFROM @TMaster LMINNER JOIN @TDetails ld on LM.MasterId = LD.MasterId--WHERE ParentMasterId = @MasterId UNION ALL SELECT LM.ParentMasterId,LM.Type, LM.MasterId, LD.DescriptionFROM @TDetails ld INNER JOIN @TMaster LM ON LM.MasterId = LD.MasterIdINNER JOIN Modules m ON M.MasterId = LM.ParentMasterId --INNER JOIN Modules m ON M.parentMasterId = LM.MasterId )SELECT Distinct * FROM Modules |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-28 : 08:25:28
|
| [code];With CTE (Level,ID,Type,Descp,Parent) AS(SELECT 0,tm.MasterId,tm.[Type],td.[Description],tm.ParentMasterIdFROM @TMaster tmLEFT JOIN @TDetails tdON td.MasterId=tm.MasterIdWHERE tm.ParentMasterId=0UNION ALLSELECT c.Level+1,tm.MasterId,tm.[Type],td.[Description],tm.ParentMasterIdFROM @TMaster tmINNER JOIN @TDetails tdON td.MasterId=tm.MasterIdINNER JOIN CTE cON c.ID=tm.ParentMasterId)select Level,Type,DescpFROM CTE c[/code] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-28 : 08:27:02
|
For more efficiency, join the details table last, outside the cte. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-04-28 : 08:42:14
|
| No My output not like thatfor the type Fixed you have to show BasicConveyanceHRASpecial AllowanceRetirement benefits in Description column |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-28 : 08:45:51
|
Please post proper output based on the sample data given about for @TDetails and @TMaster tables. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-04-28 : 08:56:51
|
| parentid type masterid description14 Fixed 1 Basic14 Fixed 1 Conveyance14 Fixed 1 HRA14 Fixed 1 Special Allowance1 fixed 13 Retirement benefits14 Flexible 2 Children Education14 Flexible 2 Medical Expenses14 Flexible 2 Travel Allowance14 Flexible 2 Variable Spl Pay14 Others 3 Annual Component14 Others 3 Other Allowance Test14 Others 3 Project allowance14 Others 3 Quarterly Incentive14 Others 3 Shift Allowance1 Retirement benefits 13 Gratuity1 Retirement benefits 13 Provident Fund1 Retirement benefits 13 Super annuation |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-28 : 09:15:17
|
[code]-- Prepare sample dataDECLARE @Master TABLE (MasterID INT, Type VARCHAR(100), ParentMasterID INT)INSERT @MasterSELECT 1, 'Fixed', 14 UNION ALLSELECT 2, 'Flexible', 14 UNION ALLSELECT 3, 'Others', 14 UNION ALLSELECT 13, 'Retirement benefits', 1 UNION ALLSELECT 14, 'PBHeads', 0DECLARE @Details TABLE (MasterID INT, Description VARCHAR(100))INSERT @DetailsSELECT 1, 'Basic' UNION ALLSELECT 1, 'Conveyance' UNION ALLSELECT 1, 'HRA' UNION ALLSELECT 1, 'Special Allowance' UNION ALLSELECT 2, 'Children Education' UNION ALLSELECT 2, 'Travel Allowance' UNION ALLSELECT 2, 'Medical Expenses' UNION ALLSELECT 2, 'Variable Spl Pay' UNION ALLSELECT 3, 'Project Allowance' UNION ALLSELECT 3, 'Quarterly Incentive' UNION ALLSELECT 3, 'Shift Allowance' UNION ALLSELECT 3, 'Annual Component' UNION ALLSELECT 13, 'Provident Fund' UNION ALLSELECT 13, 'Gratuity' UNION ALLSELECT 13, 'Super annuation'-- Prepare CTE;WITH Yak (MasterID, ParentMasterID, Type)AS ( SELECT MasterID, ParentMasterID, Type FROM @Master WHERE ParentMasterID = 0 UNION ALL SELECT m.MasterID, m.ParentMasterID, m.Type FROM @Master AS m INNER JOIN Yak AS y ON y.MasterID = m.ParentMasterID)-- Show the expected outputSELECT y.ParentMasterID AS ParentID, y.Type, y.MasterID, d.DescriptionFROM Yak AS yINNER JOIN @Details AS d ON d.MasterID = y.MasterIDORDER BY y.MasterID, d.Description[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-04-28 : 09:33:31
|
thanks peso, my output and u r output are same but my result not like thatmy output is as follows |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-28 : 09:39:27
|
You will have to use DYNAMIC SQL for this, since the number of columns are not fixed but related to number of levels in your hierarchy tree. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-04-28 : 09:52:13
|
| can you give me an exalmple related my problem |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-28 : 09:53:13
|
You will have to use DYNAMIC SQL for this, since the number of columns are not fixed but related to number of levels in your hierarchy tree. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|
|