1

オブジェクトのステータスがいつから有効になったかを示す 2 つのテーブルがあります。それらを結合/マージ/結合する必要があるため、特定の変更で両方の状態が1つのテーブルで何であるかがわかります。

表1:

Object  status1 date
Obj1    Open    1998-01-01 00:00:00.000
Obj1    Closed  2000-06-01 00:00:00.000
Obj1    Open    2008-03-06 00:00:00.000
Obj1    Closed  2013-01-01 00:00:00.000

表 2:

Object  status2 date
Obj1    ACTIVE  1999-01-01 00:00:00.000
Obj1    INACTIV 2000-08-29 00:00:00.000
Obj1    UNKNOWN 2004-05-06 00:00:00.000
Obj1    check   2014-05-01 00:00:00.000

結果として、私はそのようなものが必要になります:

Object  status1 status2 date
Obj1    Open    NULL    1998-01-01 00:00:00.000
Obj1    Open    ACTIVE  1999-01-01 00:00:00.000
Obj1    Closed  ACTIVE  2000-06-01 00:00:00.000
Obj1    Closed  INACTIV 2000-08-29 00:00:00.000
Obj1    Closed  UNKNOWN 2004-05-06 00:00:00.000
Obj1    Open    UNKNOWN 2008-03-06 00:00:00.000
Obj1    Closed  UNKNOWN 2013-01-01 00:00:00.000
Obj1    Closed  check   2014-05-01 00:00:00.000

ソーステーブルを作成するスクリプトは次のとおりです。

CREATE TABLE [dbo].[table2](
[Object] [nvarchar](1000) NULL,
[status2] [nvarchar](1000) NULL,
[date] [datetime] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[table2] ([Object], [status2], [date]) VALUES (N'Obj1', N'ACTIVE', CAST(0x00008D3F00000000 AS DateTime))
INSERT [dbo].[table2] ([Object], [status2], [date]) VALUES (N'Obj1', N'INACTIV', CAST(0x00008F9D00000000 AS DateTime))
INSERT [dbo].[table2] ([Object], [status2], [date]) VALUES (N'Obj1', N'UNKNOWN', CAST(0x000094DF00000000 AS DateTime))
INSERT [dbo].[table2] ([Object], [status2], [date]) VALUES (N'Obj1', N'check', CAST(0x0000A31E00000000 AS DateTime))
go
CREATE TABLE [dbo].[table1](
[Object] [nvarchar](1000) NULL,
[status1] [nvarchar](1000) NULL,
[date] [datetime] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[table1] ([Object], [status1], [date]) VALUES (N'Obj1', N'Open', CAST(0x00008BD200000000 AS DateTime))
INSERT [dbo].[table1] ([Object], [status1], [date]) VALUES (N'Obj1', N'Closed', CAST(0x00008F4400000000 AS DateTime))
INSERT [dbo].[table1] ([Object], [status1], [date]) VALUES (N'Obj1', N'Open', CAST(0x00009A5700000000 AS DateTime))
INSERT [dbo].[table1] ([Object], [status1], [date]) VALUES (N'Obj1', N'Closed', CAST(0x0000A13900000000 AS DateTime))

誰にもアイデアがありますか?トメック

4

2 に答える 2

0

これはうまくいきますか?

select 
  isnull(t1.object,t2.object) Object 
  ,status1
  ,status2
  ,isnull(t1.date,t2.date) date
from table1 t1
  full join table2 t2
    on t1.Object = t2.Object
      and t1.date = t2.date
order by 4
于 2013-10-16T19:19:20.433 に答える