0

我需要帮助过滤下面的查询结果。具体来说,我只需要返回dbo.pertot_all.sort_method = 2where 子句中指定的所有日期的结果 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')
4

2 回答 2

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;

这应该有效。

于 2012-08-21T22:12:07.103 回答
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语句比较所有日期。

于 2012-08-21T22:16:19.503 回答