TheBugSlayer
Programmer
Hello all.
I have a quite complex multi-part query so to speak. The first query is in a CTE that I join to another query to produce my final result set and looks like this:
Case to Specimen relationship is one-to-many. Specimen to SpecimenBlock is also one-to-many. Now I am asked to also add a count of specimen blocks. So for each case, I must list specimen count and specimen block count. I am working on different ways to do this but it is taking me long to come up with a way to retrieve both counts in just ONE statement. Maybe you can see it faster (time is the real issue). Any help is appreciated.
MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)
I have a quite complex multi-part query so to speak. The first query is in a CTE that I join to another query to produce my final result set and looks like this:
Code:
WITH CaseSpecCount_CTE(CaseID, SpecCount)
AS
(
SELECT C.ObjectID, Count(S.ObjectID)
FROM CaseTable C WITH (NOLOCK)
JOIN SpecimenTable S WITH (NOLOCK) ON S.[CASE] = C.ObjectID
WHERE (((C.[Number] LIKE 'ABC%') AND (LOWER(C.[CurrentNeeds]) LIKE 'abcdef%')) OR
(((C.[Number] LIKE 'DEF%') OR (C.[Number] LIKE 'GHI%') OR (C.[Number] LIKE 'KLM%')) AND
((C.[FINALSIGNOUTDATE] ='None') OR (C.[FINALSIGNOUTDATE] IS NULL)) AND (((
SELECT [t2].[Gross]
FROM (
SELECT TOP (1) [t1].[Gross]
FROM [dbo].[SpecimenTable] AS [t1] WITH (NOLOCK)
WHERE ([t1].[CASE] = (C.[ObjectID]))
) AS [t2]
)) IS NOT NULL))) AND (C.[Pathologist] = 0123456789)
GROUP BY C.ObjectID
)
MCP SQL Server 2000, MCTS SQL Server 2005, MCTS SQL Server 2008 (DBD, DBA)