0

このデータを考えると:

ID      FirstDate       LastDate       ItemId
12A     05-11-2011      05-11-2011        0
12A     12-19-2011      12-19-2011        3
12A     01-04-2012      01-04-2012        3
12A     01-19-2012      01-19-2012       12
64B     06-15-2010      06-15-2010        0
64B     08-19-2011      08-19-2011        3

見たい:

ID     FirstDate    FirstItemId     LastDate     LastItemId
12A    05-11-2011       0           01-19-2012       12
64B    06-15-2010       0           08-19-2011        3
4

2 に答える 2

1

ウィンドウ関数を組み合わせて使用​​すると、次の結果が得られます。

select id,
  max(case when FirstRowNumber= 1 then firstdate end) firstdate,
  max(case when FirstRowNumber= 1 then itemid end) firstitemId,
  max(case when LastRowNumber= 1 then lastdate end) lastdate,
  max(case when LastRowNumber= 1 then itemid end) lastitemId
from 
(
  select id, firstdate, lastdate, itemid,
    row_number() over(partition by id order by firstdate) FirstRowNumber,
    row_number() over(partition by id order by lastdate desc) LastRowNumber
  from yourtable
) x
where FirstRowNumber= 1
  or LastRowNumber= 1
group by id

SQL FiddlewithDemoを参照してください。

このソリューションrow_numberは、ASC/DESCの両方の日付順でレコードにを割り当てます。次に、が存在するレコードのみを気にしrow_number = 1ます。次に、集計とCASEステートメントを値に適用して、正しい結果を取得しました。

または、非常に醜い解決策UNPIVOTを使用できます。PIVOT

select *
from 
(
  select id,
    val,
    case when firstrownumber = 1 and col = 'firstdate'
          then 'firstdate'
        when firstrownumber = 1 and col = 'itemid'
          then 'firstitemid'
        when LastRowNumber = 1 and col = 'lastdate'
          then 'lastdate'
        when LastRowNumber = 1 and col = 'itemid'
          then 'lastitemid'
        else '' end col
  from
  (
    select id, 
      convert(varchar(10), firstdate, 120) firstdate, 
      convert(varchar(10), lastdate, 120) lastdate, 
      cast(itemid as varchar(10)) itemid,
      row_number() over(partition by id order by firstdate) FirstRowNumber,
      row_number() over(partition by id order by lastdate desc) LastRowNumber
    from yourtable
  ) x
  unpivot
  (
    val for col in (firstdate, lastdate, itemid)
  ) u
) x1
pivot
(
  max(val)
  for col in ([firstdate], [firstitemid], 
              [lastdate], [lastitemid])
) p

SQL FiddlewithDemoを参照してください

于 2012-10-08T18:40:35.637 に答える
0

最も基本的な形式では、おそらくSQLmingroup by機能を組み合わせたいと思うでしょう:

select ID
    , min(FirstDate) as FirstDate
    , min(ItemId) as FirstItemId
    , max(LastDate) as LastDate
    , max(ItemId) as LastItemId
from MyTable
group by ID

ただし、データがたまたまそのようでない限り、これは各列の絶対的な最小値と最大値を返すことに注意してください。FirstDate などに対応する ItemId とは限りません。最初/最後の日付に基づいて ItemID を取得するための代替手段の 1 つを次に示します。

-- Get ItemIDs that correspond to First/Last Dates
select ID
    , FirstDate
    , (select min(ItemID) from Mytable where ID = a.ID and FirstDate = a.FirstDate) as FirstItemID
    , LastDate
    , (select max(ItemID) from Mytable where ID = a.ID and LastDate = a.LastDate) as LastItemID
from (
    select ID
        , min(FirstDate) as FirstDate
        , max(LastDate) as LastDate
    from Mytable
    group by ID
) as a

相関サブクエリは、多くの場合、ウィンドウ関数よりも高速である (そしておそらく理解しやすい) ことがわかりますが、データを使用して環境でテストする必要があります。

于 2012-10-08T18:18:22.657 に答える