0
おはようございます。以前の結果に基づいてSql Setの次の関連レコードを取得する
私はタイヤの変更と車両の点検に基づいて報告を求められました。
投稿の下部にさまざまな部分を貼り付けています。
しかし、私は基本的にそれが置かれた、別のタイヤがその場所など
を取ったときにタイヤが資産との間の差の特定の位置にあったどのくらいの時間を見つけるされなければならないものを私が全くありません私が一般的なSQL Serverを初めて使用しているときに、このクエリをどこから始めるのかを考えてください。 SQLで
私のテーブルは
CREATE TABLE assets_tyre_change_header
(id INT NOT NULL IDENTITY(1,1),
change_date date not null,
asset_id varchar(50) not null,
odometer_hour varchar(50),
completed tinyint,
comment text,
CONSTRAINT assets_tyre_change_pk PRIMARY KEY (id)
);
CREATE TABLE assets_tyre_change_details
(id INT NOT NULL IDENTITY(1,1),
tyre_change_id INT,
tyre_id varchar(50) not null,
wheel_position varchar(50) not null,
tread_depth int not null,
minimum_depth int not null
CONSTRAINT assets_tyre_change_detail_pk PRIMARY KEY (id)
);
create table assets_tyre_inspection_header
(id INT NOT NULL IDENTITY(1,1),
inspection_date date not null,
asset_id varchar(50) not null,
odometer_hour varchar(50),
completed tinyint,
comment text,
CONSTRAINT assets_tyre_inspection_pk PRIMARY KEY (id)
);
CREATE TABLE assets_tyre_inspection_details
(id INT NOT NULL IDENTITY(1,1),
tyre_inspection_id INT,
tyre_id varchar(50) not null,
wheel_position varchar(50) not null,
tread_depth int not null
CONSTRAINT assets_tyre_inspection_detail_pk PRIMARY KEY (id)
);
ある私が持っている次のステップは、結果表として
SQLをこれらの表からベースの詳細を引き出すことです
select * from (
(select 'change' as type,
tyre_id,
asset_id,
wheel_position,
min(convert(int,odometer_hour))as min_oh,
max(convert(int,odometer_hour))as max_oh,
min(tread_depth)as min_depth,
min(change_date) as transdate
from assetsandfuel.dbo.assets_tyre_change_details as c_details
join assetsandfuel.dbo.assets_tyre_change_header as c_header on c_header.id = c_details.tyre_change_id
group by asset_id,tyre_id,wheel_position
)
union
(select 'inspect' as type,
tyre_id,
asset_id,
wheel_position,
min(convert(int,odometer_hour))as min_oh,
max(convert(int,odometer_hour))as max_oh,
min(tread_depth)as min_depth,
min(inspection_date) as transdate
from assetsandfuel.dbo.assets_tyre_inspection_details as i_details
join assetsandfuel.dbo.assets_tyre_inspection_header as i_header on i_header.id = i_details.tyre_inspection_id
group by asset_id,tyre_id,wheel_position
)
)as report_table
order by transdate;
にこの結果を添付次のレコードをテーブル結果として配信します。
type tyre asset position min max depth transdate
change T001 TestV 4 1489 1489 15 2016-04-01
change T002 TestV 6 1489 1489 15 2016-04-01
change F146 Forklift001 3 6900 6900 30 2016-04-02
change F147 Forklift001 2 6900 6900 30 2016-04-02
change T001 TestV 6 2800 2800 12 2016-04-08
change T002 TestV 4 2800 2800 10 2016-04-08
change T003 TestV 12 2800 2800 15 2016-04-08
inspect F146 Forklift001 3 6920 6920 27 2016-04-09
inspect F147 Forklift001 2 6920 6920 15 2016-04-09
inspect T001 TestV 6 3400 3400 9 2016-04-10
inspect T003 TestV 12 3400 3400 12 2016-04-10
change F148 Forklift001 1 6950 6950 30 2016-04-11
change F149 Forklift001 4 6950 6950 30 2016-04-11
ありがとうございます。検査表を挿入するように編集しようとしています。しかし、これまでのところ有望に見えます。結果をお知らせします –