I’ve got a SQL query running a FULL JOIN on multiple database tables. The query is dynamic based on user input but for the example I just included some possible inputs.
This issue I’m having is I need to filter by multiple date ranges and return the query results if they fall within either date range.
The query as I have now is:
SELECT rank=COUNT(*)
FROM [LOM].[dbo].[lom_problem] problem
FULL JOIN [LOM].[dbo].[lom_batch] batch on problem.lom_number = batch.lom_number
FULL JOIN [LOM].[dbo].[lom_specimen] specimen on problem.lom_number = specimen.lom_number
FULL JOIN [LOM].[dbo].[Main_LOM_Form] main on problem.lom_number = main.lom_number
WHERE problem.problem = '102' AND batch.batch IS NULL AND main.practice_code IN('HPMR', 'DOCTORs2')
AND main.occurrence_date >=Convert(datetime,'01/04/2012') AND main.occurrence_date <= Convert(datetime,'01/05/2012')
OR main.received_date>=Convert(datetime,'01/04/2012') AND main.received_date <= Convert(datetime,'01/05/2012')
However, the query returns the results as if my query was the SUM of
SELECT rank=COUNT(*)
FROM [LOM].[dbo].[lom_problem] problem
FULL JOIN [LOM].[dbo].[lom_batch] batch on problem.lom_number = batch.lom_number
FULL JOIN [LOM].[dbo].[lom_specimen] specimen on problem.lom_number = specimen.lom_number
FULL JOIN [LOM].[dbo].[Main_LOM_Form] main on problem.lom_number = main.lom_number
WHERE problem.problem = '102' AND batch.batch IS NULL AND main.practice_code IN('HPMR', 'DOCTORs2')
AND main.occurrence_date >=Convert(datetime,'01/04/2012') AND main.occurrence_date <= Convert(datetime,'01/05/2012')
and
SELECT rank=COUNT(*)
FROM [LOM].[dbo].[lom_problem] problem
FULL JOIN [LOM].[dbo].[lom_batch] batch on problem.lom_number = batch.lom_number
FULL JOIN [LOM].[dbo].[lom_specimen] specimen on problem.lom_number = specimen.lom_number
FULL JOIN [LOM].[dbo].[Main_LOM_Form] main on problem.lom_number = main.lom_number
WHERE main.received_date>=Convert(datetime,'01/04/2012') AND main.received_date <= Convert(datetime,'01/05/2012')
How do I get the query to return the results with this part intact:
SELECT rank=COUNT(*)
FROM [LOM].[dbo].[lom_problem] problem
FULL JOIN [LOM].[dbo].[lom_batch] batch on problem.lom_number = batch.lom_number
FULL JOIN [LOM].[dbo].[lom_specimen] specimen on problem.lom_number = specimen.lom_number
FULL JOIN [LOM].[dbo].[Main_LOM_Form] main on problem.lom_number = main.lom_number
WHERE problem.problem = '102' AND batch.batch IS NULL AND main.practice_code IN('HPMR', 'DOCTORs2')
plus the filtering for either date range on top of that:
AND main.occurrence_date >=Convert(datetime,'01/04/2012') AND main.occurrence_date <= Convert(datetime,'01/05/2012')
OR main.received_date>=Convert(datetime,'01/04/2012') AND main.received_date <= Convert(datetime,'01/05/2012')
Thanks!
Edit:
I need to filter based on a data range of main.occurrence_date and main.received_date. If the data range is present in either one or the other column, I need to return the count. However, the query as I have now returns the sum of the first part of the where statement and then the results from the second part (after OR). I thought it may have been just a syntax/formatting issue but was unsure.
Don’t forget your parenthesis:
EDIT: OK, let’s try again. I believe acermate433s’ suggestion would make it more readable:
Is this what you had in mind? It would count all records that have occurance_date in the specified range and all records that have received_date in the specified range, without counting them twice.