1

クエリから返されたデータをクリーンアップしたいと思います。このクエリ:

select seriesId,
       startDate,
       reportingCountyId,
       countyId,
       countyName,
       pocId,
       pocValue
  from someTable
 where seriesId = 147
   and pocid = 2
   and countyId in (2033,2040)

startDateで注文する

通常、すべての年で2つの郡の一致を返します。

seriesId    startDate   reportingCountyId   countyId    countyName  pocId   pocValue
147 2004-01-01 00:00:00.000 6910    2040    CountyOne       2   828

147 2005-01-01 00:00:00.000 2998    2033    CountyTwo   2   4514
147 2005-01-01 00:00:00.000 3000    2040    CountyOne       2   2446

147 2006-01-01 00:00:00.000 3018    2033    CountyTwo   2   5675
147 2006-01-01 00:00:00.000 4754    2040    CountyOne       2   2265

147 2007-01-01 00:00:00.000 3894    2033    CountyTwo   2   6250
147 2007-01-01 00:00:00.000 3895    2040    CountyOne       2   2127

147 2008-01-01 00:00:00.000 4842    2033    CountyTwo   2   5696
147 2008-01-01 00:00:00.000 4846    2040    CountyOne       2   2013

147 2009-01-01 00:00:00.000 6786    2033    CountyTwo   2   2578
147 2009-01-01 00:00:00.000 6817    2040    CountyTwo   2   1933

147 2010-01-01 00:00:00.000 6871    2040    CountyOne       2   1799
147 2010-01-01 00:00:00.000 6872    2033    CountyTwo   2   4223

147 2011-01-01 00:00:00.000 8314    2033    CountyTwo   2   3596
147 2011-01-01 00:00:00.000 8315    2040    CountyOne       2   1559

ただし、最初のエントリには2004年のCountyOneしか含まれていないことに注意してください。私が行っているグラフのCountyTwoの偽の行を返したいと思います。CountyOneのようにpocValue=0で埋めるだけで十分です。

ありがとう!!!!!!!!

4

1 に答える 1

1

これを試してください(そのcountryidに空白行が必要な場合)

; with CTE AS 

(SELECT 2033 As CountryID UNION SELECT 2040),

CTE2 AS
(
       seriesId, startDate, reportingCountyId, 
       countyId, countyName, pocId, pocValue 
       from someTable where 
       seriesId = 147 and pocid = 2 and countyId in (2033,2040) 
       order by startDate
)

SELECT x1.CountyId, x2.*, IsNull(pocValue,0) NewpocValue FROM CTE x
LEFT OUTER JOIN CTE2 x2 ON x1.CountyId = x2.reportingCountyId
于 2012-08-15T19:31:53.380 に答える