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 |
|
Teroman
Posting Yak Master
115 Posts |
Posted - 2002-11-15 : 11:02:10
|
| Hi, this is a proc I've been working on for a bit, and it has me tearing my hair out.I have a heirarchical table of territory (good start eh?)Looks like this:nTerritoryIDcTerritoryNamenParentIDnDepthnLeftnRightI hope you are familiar with the Super-Celko-Lefty-Right-Method, or whatever its called, if not I'm sure someone helpful can post a link...Any-hoo, I also have Deals, that contain multiple territories, so i have a DealTerritory table, that also contains data at a detail levelnDealIDnTerritoryIDnDataWhat i need to do is produce a nice bit of text saying what territories are selected.Say we have the followingWorld--Europe----England----Italy----Spain--Asia----China----Japan----IndiaIf the user selects the world there is an entry in DealTerritory for each territory below it (to store the detail data)I only want to display World.If they chose Europe and Asia, the de-selected India I want to displayEuropeAsia excluding IndiaIf they just chose a couple of countries I want to display them egEnglandFranceJapanMy method has been to use the concatenating update that was posted here recently, get the top parent against missing children into a temp table#tnTerritoryIDnMissingChildIDcDescriptionBasically, for each branch I need the highest member, and then say "excluding" followed by children that do not exist in the DealTerritory table.I have a query that does this, but the problem is it lists all childrenIf the user selects World, then de-selects Asia I wantWorld excluding Asiaat the moment i getWorld excluding Asia, China, Japan, IndiaI hope this is clearI'll put what I have so far in the next post in a moment, thinking hats on people!<edit>I can't even spell "procedure" in the title, what hope is there for me?</edit>Edited by - teroman on 11/15/2002 11:20:29 |
|
|
Teroman
Posting Yak Master
115 Posts |
Posted - 2002-11-15 : 11:06:39
|
ok, here is what i have, forgot to mention it bundles some IDs together too, but you can ignore that if you like, its not important.As you can see the strings are passed out of the proc.the inner select in the first big query gets the highest level in each branch, the next bit gets the children, but it gets them all :( drop procedure ap_DealTerritory_GetDescriptiongocreate procedure ap_DealTerritory_GetDescription @nDealID int, @cTerritoryIDs varchar(1000) output, @cTerritoryNames varchar(8000) outputasset ansi_warnings offset nocount oncreate table #t( -- just to keep stuff in order i int identity(1,1) primary key clustered, nTerritoryID int not null, nChildTerritoryID int not null, cTerritoryIDList varchar(1000) null, cTerritory varchar(100) not null, cTerritoryList varchar(5000) null)insert into #t (nTerritoryID, nChildTerritoryID, cTerritory)select A.nTerritoryID, T.nTerritoryID, T.cTerritoryDescriptionfrom( select T.nTerritoryID, T.cTerritoryDescription, T.nLeft, T.nRight from DealTerritory DT join Territory T on DT.nTerritoryID = T.nTerritoryID join Territory T_Parent on T.nLeft between T_Parent.nLeft and T_Parent.nRight left join DealTerritory DT_Parent on T_Parent.nTerritoryID = DT_Parent.nTerritoryID and DT.nDealID = DT_Parent.nDealID and DT_Parent.nTerritoryID <> T.nTerritoryID where DT.nDealID = @nDealID group by T.nTerritoryID, T.cTerritoryDescription, T.nLeft, T.nRight having sum(DT_Parent.nTerritoryID) is NULL) Ajoin Territory Ton T.nLeft between A.nLeft and A.nRightleft join DealTerritory DTon DT.nDealID = @nDealIDand T.nTerritoryID = DT.nTerritoryIDand DT.nTerritoryID <> A.nTerritoryIDwhere DT.nTerritoryID is NULLorder by T.nLeftdeclare @list varchar(8000), @IDlist varchar(1000), @lasti intselect @list = '', @lasti = -1--here is the meat of the workupdate #tset @list = cTerritoryList = case when @lasti <> nTerritoryID then cTerritory + ' excluding ' else @list + cTerritory + ', ' end, @IDlist = cTerritoryIDList = case when @lasti <> nTerritoryID then convert(varchar, nChildTerritoryID) else @IDlist + ',' + convert(varchar, nChildTerritoryID) end, @lasti = nTerritoryIDselect @cTerritoryNames = isnull(@cTerritoryNames + '<br>', '') + case when right(#t.cTerritoryList, 2) = ', ' then substring(#t.cTerritoryList, 1, len(#t.cTerritoryList) - 1) else #t.cTerritory end, @cTerritoryIDs = isnull(@cTerritoryIDs + ',', '') + cTerritoryIDListfrom( select max(cTerritoryList) as 'cTerritoryList' from #t group by nTerritoryID) Ajoin #ton A.cTerritoryList = #t.cTerritoryListorder by #t.igo |
 |
|
|
|
|
|
|
|