向T-SQL查询添加多个筛选条件

时间:2012-08-21 21:02:56

标签: tsql

我需要协助过滤以下查询的结果。具体来说,我只需要返回where dbo.pertot_all.sort_method = 2中where子句中指定的所有日期的结果。目前,使用下面提供的内容,它将返回其他sort_methods。我不熟悉AND / OR运算符的顺序/分组。有人可以提供一些指导吗?非常感谢提前。

 SELECT     dbo.SiteDescription.Site, dbo.SiteDescription.Description AS SiteDescription, dbo.SiteDescription.LocationGroup, dbo.chart_all.acct, dbo.pertot_all.sf2, 
                  dbo.SSRS_sf2UnitCodes.sf2description, dbo.pertot_all.sf4, dbo.SSRS_sf4UnitCodes.sf4description, dbo.chart_all.description AS AccountDescription, 
                  dbo.pertot_all.amt, dbo.pertot_all.per_start, dbo.pertot_all.sort_method
 FROM         dbo.SSRS_sf2UnitCodes INNER JOIN
                  dbo.pertot_all ON dbo.SSRS_sf2UnitCodes.sf2UnitCode = dbo.pertot_all.sf2 INNER JOIN
                  dbo.SSRS_sf4UnitCodes ON dbo.pertot_all.sf4 = dbo.SSRS_sf4UnitCodes.sf4UnitCode RIGHT OUTER JOIN
                  dbo.chart_all LEFT OUTER JOIN
                  dbo.SiteDescription ON dbo.chart_all.site_ref = dbo.SiteDescription.Site ON dbo.pertot_all.site_ref = dbo.SiteDescription.Site AND 
                  dbo.pertot_all.sf1 = dbo.chart_all.acct
WHERE     (1 = 1) AND (ISNULL(dbo.pertot_all.per_start, '2012-01-01 00:00:00.000') = '2012-01-01 00:00:00.000') AND (ISNULL(dbo.pertot_all.sort_method, 2) = 2) OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-02-01 00:00:00.000') = '2012-02-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-03-01 00:00:00.000') = '2012-03-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-04-01 00:00:00.000') = '2012-04-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-05-01 00:00:00.000') = '2012-05-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-06-01 00:00:00.000') = '2012-06-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-07-01 00:00:00.000') = '2012-07-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-08-01 00:00:00.000') = '2012-08-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-09-01 00:00:00.000') = '2012-09-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-10-01 00:00:00.000') = '2012-10-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-11-01 00:00:00.000') = '2012-11-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-12-01 00:00:00.000') = '2012-12-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-01-01 00:00:00.000') = '2011-01-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-02-01 00:00:00.000') = '2011-02-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-03-01 00:00:00.000') = '2011-03-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-04-01 00:00:00.000') = '2011-04-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-05-01 00:00:00.000') = '2011-05-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-06-01 00:00:00.000') = '2011-06-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-07-01 00:00:00.000') = '2011-07-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-08-01 00:00:00.000') = '2011-08-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-09-01 00:00:00.000') = '2011-09-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-10-01 00:00:00.000') = '2011-10-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-11-01 00:00:00.000') = '2011-11-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-12-01 00:00:00.000') = '2011-12-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-01-01 00:00:00.000') = '2010-01-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-02-01 00:00:00.000') = '2010-02-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-03-01 00:00:00.000') = '2010-03-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-04-01 00:00:00.000') = '2010-04-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-05-01 00:00:00.000') = '2010-05-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-06-01 00:00:00.000') = '2010-06-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-07-01 00:00:00.000') = '2010-07-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-08-01 00:00:00.000') = '2010-08-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-09-01 00:00:00.000') = '2010-09-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-10-01 00:00:00.000') = '2010-10-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-11-01 00:00:00.000') = '2010-11-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-12-01 00:00:00.000') = '2010-12-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2009-12-01 00:00:00.000') = '2009-12-01 00:00:00.000')

2 个答案:

答案 0 :(得分:0)

    SELECT     dbo.SiteDescription.Site, dbo.SiteDescription.Description AS SiteDescription, dbo.SiteDescription.LocationGroup, dbo.chart_all.acct, dbo.pertot_all.sf2, 
                  dbo.SSRS_sf2UnitCodes.sf2description, dbo.pertot_all.sf4, dbo.SSRS_sf4UnitCodes.sf4description, dbo.chart_all.description AS AccountDescription, 
                  dbo.pertot_all.amt, dbo.pertot_all.per_start, dbo.pertot_all.sort_method
 FROM         dbo.SSRS_sf2UnitCodes INNER JOIN
                  dbo.pertot_all ON dbo.SSRS_sf2UnitCodes.sf2UnitCode = dbo.pertot_all.sf2 INNER JOIN
                  dbo.SSRS_sf4UnitCodes ON dbo.pertot_all.sf4 = dbo.SSRS_sf4UnitCodes.sf4UnitCode RIGHT OUTER JOIN
                  dbo.chart_all LEFT OUTER JOIN
                  dbo.SiteDescription ON dbo.chart_all.site_ref = dbo.SiteDescription.Site ON dbo.pertot_all.site_ref = dbo.SiteDescription.Site AND 
                  dbo.pertot_all.sf1 = dbo.chart_all.acct
WHERE     (1 = 1) AND ((ISNULL(dbo.pertot_all.per_start, '2012-01-01 00:00:00.000') = '2012-01-01 00:00:00.000') AND (ISNULL(dbo.pertot_all.sort_method, 2) = 2) OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-02-01 00:00:00.000') = '2012-02-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-03-01 00:00:00.000') = '2012-03-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-04-01 00:00:00.000') = '2012-04-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-05-01 00:00:00.000') = '2012-05-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-06-01 00:00:00.000') = '2012-06-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-07-01 00:00:00.000') = '2012-07-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-08-01 00:00:00.000') = '2012-08-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-09-01 00:00:00.000') = '2012-09-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-10-01 00:00:00.000') = '2012-10-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-11-01 00:00:00.000') = '2012-11-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2012-12-01 00:00:00.000') = '2012-12-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-01-01 00:00:00.000') = '2011-01-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-02-01 00:00:00.000') = '2011-02-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-03-01 00:00:00.000') = '2011-03-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-04-01 00:00:00.000') = '2011-04-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-05-01 00:00:00.000') = '2011-05-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-06-01 00:00:00.000') = '2011-06-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-07-01 00:00:00.000') = '2011-07-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-08-01 00:00:00.000') = '2011-08-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-09-01 00:00:00.000') = '2011-09-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-10-01 00:00:00.000') = '2011-10-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-11-01 00:00:00.000') = '2011-11-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2011-12-01 00:00:00.000') = '2011-12-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-01-01 00:00:00.000') = '2010-01-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-02-01 00:00:00.000') = '2010-02-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-03-01 00:00:00.000') = '2010-03-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-04-01 00:00:00.000') = '2010-04-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-05-01 00:00:00.000') = '2010-05-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-06-01 00:00:00.000') = '2010-06-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-07-01 00:00:00.000') = '2010-07-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-08-01 00:00:00.000') = '2010-08-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-09-01 00:00:00.000') = '2010-09-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-10-01 00:00:00.000') = '2010-10-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-11-01 00:00:00.000') = '2010-11-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2010-12-01 00:00:00.000') = '2010-12-01 00:00:00.000') OR
                  (ISNULL(dbo.pertot_all.per_start, '2009-12-01 00:00:00.000') = '2009-12-01 00:00:00.000')) AND dbo.pertot_all.sort_method = 2;

这应该有用。

答案 1 :(得分:0)

您可以使用in语句大大简化查询。请考虑以下事项:

select 1 where cast('1/1/2000' as datetime) in ('1/1/2000', '1/2/2000')

如果datetime在列表中,查询将返回一行。

您的查询可以重写为以下内容:

SELECT     fields
  FROM     tables
 WHERE     (ISNULL(dbo.pertot_all.sort_method, 2) = 2) AND
           (ISNULL(dbo.pertot_all.per_start, '2012-01-01 00:00:00.000') in ('2012-01-01 00:00:00.000', '2012-02-01 00:00:00.000', '2012-03-01 00:00:00.000', etc.))

您必须至少使用ISNULL()一次日期,因此您不会遇到NULL问题。但您可以使用in语句比较所有日期。