I am trying to write a query with some fancy joins. I know this can be done, but I am not remembering how to do it.
Here is my query as it is now:
SELECT parentLink.SourceWorkItemID, parentLink.TargetWorkItemID,
childLink.SourceWorkItemID, childLink.TargetWorkItemID,
childLink.WorkItemLinkTypeSK--,childLinkType.LinkName
FROM dbo.FactWorkItemLinkHistory parentLink
JOIN dbo.DimWorkItemLinkType parentLinkType (NOLOCK)
ON parentLink.WorkItemLinkTypeSK = parentLinkType.WorkItemLinkTypeSK
LEFT JOIN dbo.FactWorkItemLinkHistory childLink (NOLOCK)
ON parentLink.TargetWorkItemID = childLink.SourceWorkItemID
AND childLink.RemovedByPersonSK IS NULL
AND childLink.WorkItemLinkTypeSK IN
(SELECT sublink.WorkItemLinkTypeSK
FROM dbo.DimWorkItemLinkType sublink
WHERE sublink.LinkName = 'Child'
)
--LEFT JOIN dbo.DimWorkItemLinkType childLinkType (NOLOCK)
-- ON childLink.WorkItemLinkTypeSK = childLinkType.WorkItemLinkTypeSK
-- AND childLinkType.LinkName = 'Child'
WHERE parentLink.SourceWorkItemID = 1917
AND parentLink.RemovedByPersonSK IS NULL
AND parentLinkType.LinkName = 'Releases Story'
I have tried to get this to run without the select clause in the from part of my query, but it keeps eluding me.
The result I am looking for is something this:
1917 1915 1915 1916 2
1917 1913 1913 1914 2
1917 1913 1913 4349 2
1917 1921 1921 1922 2
1917 1918 NULL NULL NULL
1917 1920 NULL NULL NULL
I get that result using the select in the from clause, but I would rather not use a select there if I can avoid it.
I tried making these changes:
- Uncomment the last join (and
childLinkType.LinkNamein the main select clause) - Remove the unwanted
selectclause (the line above the last join).
When I do I get this:
1917 1915 1915 1916 2 Child
1917 1915 1915 1917 20 NULL
1917 1915 1915 1919 7 NULL
1917 1915 1915 1911 4 NULL
1917 1913 1913 1914 2 Child
1917 1913 1913 1917 20 NULL
1917 1913 1913 1911 4 NULL
1917 1913 1913 4349 2 Child
1917 1921 1921 1922 2 Child
1917 1921 1921 1917 20 NULL
1917 1918 1918 1919 7 NULL
1917 1918 1918 1912 4 NULL
1917 1918 1918 1917 20 NULL
1917 1920 1920 1911 4 NULL
1917 1920 1920 1917 20 NULL
That is too many rows. And the I don’t want nulls matching on the DimWorkItemLinkType table. So I make the last join an inner join. Then I get this:
1917 1915 1915 1916 2 Child
1917 1913 1913 1914 2 Child
1917 1913 1913 4349 2 Child
1917 1921 1921 1922 2 Child
That has removed the items that don’t have a “child” (1918 and 1920 from the first data set). I still need them in my results.
Any ideas how I can remove the select (in the from clause) and still get this to give me the first set of results?
NOTE: I am running this against the TFS_Warehouse database on TFS 2010 server (using the tables that are approved for reporting purposes).
Well, I won’t pretend I really understand your question — as others have said, it might be worth trying to break it down and simplify it — and I don’t have a TFS_Warehouse, but I’m going to take a chance and guess that you’re looking for a structure something like this (untested, obviously):