相关文章推荐
玩足球的爆米花  ·  SQL Server ...·  1 周前    · 
精明的白开水  ·  Neodynamic.SDK.ZPLPrin ...·  1 年前    · 
健壮的日光灯  ·  Pyspark 读 DataFrame ...·  1 年前    · 
沉着的火车  ·  Android MediaPlayer ...·  1 年前    · 

带有多个日期范围和全连接的SQL查询

0 人关注

我有一个SQL查询,在多个数据库表中运行一个FULL JOIN。该查询是基于用户输入的动态查询,但对于这个例子,我只包括一些可能的输入。

我遇到的这个问题是,我需要按多个日期范围进行过滤,如果查询结果属于任一日期范围,则返回查询结果。

我现在的疑问是。

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')

然而,查询返回的结果就像我的查询是 "SUM "一样。

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')
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')

我如何让查询在返回结果时完整地保留这部分内容。

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')
 我需要根据main.occurrence_date和main.received_date的数据范围进行过滤。如果数据范围出现在其中一列或另一列,我需要返回计数。然而,我现在的查询返回where语句第一部分的总和,然后是第二部分的结果(OR后)。我想这可能只是一个语法/格式的问题,但不确定。

1 个评论
你能不能把问题说得更详细一点? 另外,是否有必要使用FULL JOIN,或者使用INNER JOIN?
sql
sql-server-2008
jordanhill123
jordanhill123
发布于 2012-02-03
1 个回答
Olaf
Olaf
发布于 2012-02-03
已采纳
0 人赞同

不要忘记你的括号。

 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'))

编辑:好的,让我们再试一下。我相信acermate433s的建议会让它更有可读性。