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 |
Mickybee
Starting Member
29 Posts |
Posted - 2007-08-07 : 13:40:38
|
Hi all,Ive got a bit of a problem,I have two tables:CATEGORYidnameparent_CATEGORY_idRECORDidCATEGORY_idStock_Held_Numberps. Ive stripped out any non relevant fieldsI also have the following query (again ive stripped out the non-relevant fields)SELECT TOP (100) PERCENT SUM(dbo.RECORD.Stock_Held_Number) AS TotalStock, CATEGORY.Name AS FundNameFROM CATEGORY LEFT OUTER JOIN dbo.RECORD ON CATEGORY.ID = dbo.RECORD.CATEGORY_idGROUP BY CATEGORY.Name, RECORD.Stock_Held_NumberAt the moment its grouping all the CATEGORIES and giving me a sum total for each which is great.The problem I have is the CATEGORY table, there is an optional join to parent CATEGORY records on the table.What Im trying to do is to provide a fully ordered result within the CATEGORIES and I don't have a clue.For example:The CATEGORY table has the following valuesID Name Parent_Category_id1 Pens 2 Animals3 Rocks4 Horses 25 Dogs 2When I currently run the query I get:Name TotalStockPens 20 Animals 30Rocks 40Horses 50Dogs 60It's technically correct because I don't want the parent to calculate the total value of the childrenWhat Im really trying to do is order them within the hierarchy though and indent (if possible) the result so I would getName TotalStockAnimals 30---Dogs 60---Horses 50Pens 20 Rocks 40Does anybody have any pointers as to how I can achieve this.Many thanksMike |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-08-07 : 14:51:40
|
Indenting is an application isssue, not something you'd do inside T-SQL. So if you want indents, put that code in your front-end application. Not sure about the ordering thing, I've never worked with hierarchies.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-08 : 02:34:04
|
As said, you can do this in front end application. But you need to generate a level number to properly indent the result. Refer thishttp://www.nigelrivett.net/SQLTsql/RetrieveTreeHierarchy.htmlYou can simulate the indents in your front end application the same way described in that linkMadhivananFailing to plan is Planning to fail |
 |
|
Mickybee
Starting Member
29 Posts |
Posted - 2007-08-08 : 12:29:22
|
Thanks both,The indents are a nice to have type thing, what I am not sure about if how to order the results. At the moment they are simply ordered alphabetically but i would like to order them alphabetically within the hierarchy order. |
 |
|
|
|
|