0

以下のクエリの結果をフィルタリングするための支援が必要です。具体的に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')
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')

がリストにある場合、クエリは1行を返します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.))

日付には少なくとも1回はを使用する必要があるISNULL()ため、NULLの問題は発生しません。inただし、ステートメントを使用してすべての日付を比較できます。

于 2012-08-21T22:16:19.503 に答える