Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL query with multiple date ranges and Full Join
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>The query as I have now is:</p> <pre><code>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 &gt;=Convert(datetime,'01/04/2012') AND main.occurrence_date &lt;= Convert(datetime,'01/05/2012') OR main.received_date&gt;=Convert(datetime,'01/04/2012') AND main.received_date &lt;= Convert(datetime,'01/05/2012') </code></pre> <p>However, the query returns the results as if my query was the SUM of </p> <pre><code>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 &gt;=Convert(datetime,'01/04/2012') AND main.occurrence_date &lt;= Convert(datetime,'01/05/2012') </code></pre> <p>and</p> <pre><code>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&gt;=Convert(datetime,'01/04/2012') AND main.received_date &lt;= Convert(datetime,'01/05/2012') </code></pre> <p>How do I get the query to return the results with this part intact:</p> <pre><code>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') </code></pre> <p>plus the filtering for either date range on top of that:</p> <pre><code> AND main.occurrence_date &gt;=Convert(datetime,'01/04/2012') AND main.occurrence_date &lt;= Convert(datetime,'01/05/2012') OR main.received_date&gt;=Convert(datetime,'01/04/2012') AND main.received_date &lt;= Convert(datetime,'01/05/2012') </code></pre> <p>Thanks!</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload