March 16, 2019 at 1:35 pm
March 16, 2019 at 2:17 pm
After thinking it over, I've come up with one possible solution - though I'm certainly still open to others, as well as commentary on my proposed solution. Essentially what I'm doing is, for each row I want to test, I check to see how many elements higher up in the tree (including itself) need to be tested, and, how many elements higher up in the tree (including itself) have at least one Term entry with valid dates. If those two are equal, then the record becomes valid:
SELECT
targetOccupant.ID,
targetOccupant.Hierarchy.ToString(),
targetOccupant.OccupantTypeID,
targetOccupant.ItemID,
AncestorTermCount,
ValidTermCount
FROM #Occupant targetOccupant
OUTER APPLY
(
SELECT COUNT(*) As AncestorTermCount, ISNULL(SUM(ValidTermCount), 0) AS ValidTermCount
FROM #Occupant occupantAncestry
OUTER APPLY
(
SELECT MAX(1) AS ValidTermCount
FROM #Term
WHERE #Term.OccupantID = occupantAncestry.ID
AND GETDATE() BETWEEN StartDate AND EndDate
) o2
WHERE targetOccupant.Hierarchy.IsDescendantOf(occupantAncestry.Hierarchy) = 1
AND occupantAncestry.Hierarchy.GetAncestor(1) != @Root
) o1
WHERE targetOccupant.ItemID = 1
AND targetOccupant.OccupantTypeID = 1
AND AncestorTermCount = ValidTermCount
March 17, 2019 at 12:00 pm
Another update. After thinking about this some more, I've realised that the problem boils down to taking a given node, and finding if the tree going upwards to the root passes whatever test criteria is being used at each level. The total number of successful tests, then, must equal the level of the node being tested. So here's a new stab at the same problem:
SELECT
targetOccupant.ID,
targetOccupant.Hierarchy.ToString(),
targetOccupant.OccupantTypeID,
targetOccupant.ItemID,
targetOccupant.Hierarchy.GetLevel() AS HierarchyLevel,
ValidTermCount
FROM #Occupant targetOccupant
CROSS APPLY
(
SELECT SUM(ISNULL(ValidTermCount, 0)) AS ValidTermCount
FROM #Occupant occupantAncestry
OUTER APPLY
(
-- Test whether the given parent node passes the test criteria
SELECT MAX(1) AS ValidTermCount
FROM #Term
WHERE #Term.OccupantID = occupantAncestry.ID
AND GETDATE() BETWEEN StartDate AND EndDate
) validParent
WHERE targetOccupant.Hierarchy.IsDescendantOf(occupantAncestry.Hierarchy) = 1
) parentTree
WHERE targetOccupant.ItemID = 1
AND targetOccupant.OccupantTypeID = 1
-- -1 because of the special case in this example, that the second level is a seat without the possibility of having a term to test
AND targetOccupant.Hierarchy.GetLevel() - 1 = ValidTermCount
March 18, 2019 at 11:44 pm
my solution.
it's not good, but it can help you
;
With h As (
Select
o.Hierarchy
, o.ItemID
, StartDate = IsNull(t.StartDate, '19000101')
, EndDate = IsNull(t.EndDate, '30000101')
, o.ID
, o.OccupantTypeID
-- , ot.Name
From
#Occupant o
Left Join #Term t On t.OccupantID = o.ID
-- Left Join #OccupantType ot On ot.ID = o.OccupantTypeID
),
res As (
Select
h.*
, cnt = Count(*) Over (Partition By h.ID)
, sm = Sum(Case When GetDate() BetWeen hp.StartDate And hp.EndDate Then 1 Else 0 End) Over (Partition By h.ID)
From
h h
Join h hp On h.Hierarchy.IsDescendantOf(hp.Hierarchy) = 1
)
Select
r.ID
, r.ItemID
, r.StartDate
, r.EndDate
, r.Hierarchy.ToString()
From
res r
Where
r.ItemID = 1
And r.OccupantTypeID = 1
And r.sm = r.cnt
I Have Nine Lives You Have One Only
THINK!
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply