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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Damned impossible procedure, HELP!

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:

nTerritoryID
cTerritoryName
nParentID
nDepth
nLeft
nRight

I 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 level

nDealID
nTerritoryID
nData

What i need to do is produce a nice bit of text saying what territories are selected.

Say we have the following

World
--Europe
----England
----Italy
----Spain
--Asia
----China
----Japan
----India

If 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 display

Europe
Asia excluding India

If they just chose a couple of countries I want to display them eg

England
France
Japan

My method has been to use the concatenating update that was posted here recently, get the top parent against missing children into a temp table

#t
nTerritoryID
nMissingChildID
cDescription


Basically, 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 children

If the user selects World, then de-selects Asia I want

World excluding Asia

at the moment i get

World excluding Asia, China, Japan, India

I hope this is clear

I'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_GetDescription
go
create procedure ap_DealTerritory_GetDescription
@nDealID int,
@cTerritoryIDs varchar(1000) output,
@cTerritoryNames varchar(8000) output
as

set ansi_warnings off
set nocount on

create 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.cTerritoryDescription
from
(
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
) A
join Territory T
on T.nLeft between A.nLeft and A.nRight
left join DealTerritory DT
on DT.nDealID = @nDealID
and T.nTerritoryID = DT.nTerritoryID
and DT.nTerritoryID <> A.nTerritoryID
where DT.nTerritoryID is NULL
order by T.nLeft


declare
@list varchar(8000),
@IDlist varchar(1000),
@lasti int

select
@list = '',
@lasti = -1

--here is the meat of the work
update
#t
set
@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 = nTerritoryID

select @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 + ',', '') + cTerritoryIDList
from
(
select max(cTerritoryList) as 'cTerritoryList'
from #t
group by nTerritoryID
) A
join #t
on A.cTerritoryList = #t.cTerritoryList
order by #t.i

go




Go to Top of Page
   

- Advertisement -