2016-06-20 16 views
0

我有三個表:三個表聯接是不給期望的輸出

1:Station_Details(主數據表格)
2:RF_Details
3:WL_Details

如提及在下面的圖像。 我需要一個從所有三個表中的數據到輸出表 來自Station_details的主數據和來自RF和WL表的其他數據。 如果RF_Details和WL_Details表具有相同的工作站ID和相同的DateTime,則在輸出表中,兩行的詳細信息將顯示在一行中。 如果DateTime不同,它會出現在不同的行中。

我試過這個SQL查詢,但我沒有得到像OUTPUT表一樣的輸出。

select rf.StationID, st.stationname, st.state,rf.rf,rf.cum-rf,wl.wl,DataTime 
    from [RF_Details] rf 
    join [WL_Details] wl 
    join Station_Details st 
    on rf.StationID = wl.StationId and 
     rf.DataRecieved=wl.DataRecieved and 
     st.stationid =rf.stationid and 
     st.stationid = wl.stationid; 

但它沒有給出正確的行數和輸出。 請幫助我一樣。

enter image description here

+0

您的查詢不匹配的樣本數據(DataReceived查詢不在數據)。另外,輸出的問題是什麼? –

+0

如果'ON'條件的位置很重要,那麼你應該從'RF_Details'和'WL_Details'的'CROSS JOIN'中得到結果,你似乎沒有這個結果。 –

+0

Jeff Siver是正確的。輸出與示例數據不匹配 – Sam

回答

2

你應該始終把連接條件沿加入本身。此外,添加INNER是我遵循的做法,以確保不會返回額外的記錄。

SELECT rf.StationID, st.stationname, st.state, wl.DataRecieved, wl.waterlevel1, 
    rf.dailyrainfall, rf.cumrainfall 
FROM [RF_Details] rf 
INNER JOIN [WL_Details] wl 
ON rf.StationID = wl.StationId AND 
    rf.DataRecieved=wl.DataRecieved 
INNER JOIN Station_Details st 
ON st.stationid =rf.stationid AND 
    st.stationid = wl.stationid; 
+0

RF表有156098行和WL_Deatails ahs 488266行,但連接返回81851行。 – vim

+0

「inner」的使用不提供與「join」不同的功能。你應該放棄'inner'並且使用'join'。 – Yobik

+1

@vim查詢將只返回滿足條件的記錄。只需檢查計數就不會告訴你是否得到正確的輸出。在你的主表中可以有某些記錄,在其他表中沒有任何子記錄 –

0

您應該重新設計您的數據庫:現在您在RF_Details和WL_Details - DateTime中有一個輔助鍵。它們之間起着外鍵的作用。這是不好的,每當你需要加入這些表格或收集相應的數據時,都會讓你感到困惑。

應該有另外一個表,如Station_Records,它將爲該工作站的每條記錄存儲一行:(id, station_id, record_date_time)RFWL行,如果有的話應該引用該表,而不是參照Station_Detailsstation_id,以及另一個是datetime

使用當前結構,您需要完全連接RFWL以同時獲得:按日期時間 - 在同一行中匹配,不匹配 - 在單獨的行中。

select sd.station_name, Station_Records.* 
from Station_Details sd 
inner join 
(
    select 
    IsNull(rf.station_id, wl.station_id) station_id, 
    IsNull(rf.DataRecieved, wl.DataRecieved) DataRecieved, 
    rf.rf, rf.cum-rf, wl.wl 
    from [RF_Details] rf 
    full join [WL_Details] wl 
    on wl.station_id = rf.station_id 
    and wl.DataRecieved = rf.DataRecieved 
) Station_Records 
on Station_Records.station_ud = sd.station_id 

具體實施可能包括OUTER APPLY,甚至是沒有任何子查詢 - 它並不真正的問題目前。

修改你的表結構,你將永遠知道所有匹配的記錄:

select 
    sd.station_id, sd.station_name, 
    sr.DataRecieved 
    rf.rf, rf.cum-rf, 
    wl.wl 
from Station_Details sd 
inner join Station_Records sr 
on sr.station_id = sd.station_id 
left join RF_Details rf 
on rf.record_id = sr.record_id 
left join WL_Details wl 
on wl.record_id = sr.record_id 
1
declare @station_details table(id int, station_id varchar(10),station_name varchar(10),state varchar(10)) 
declare @rf_details table (id int, station_id varchar(10),rf int, cum_rf int, dt dateTIME) 
declare @wl_details table (id int, station_id varchar(10),wl int,dt datetime) 

insert into @station_details values 
(1,'DEL-NDL','NDL','DEL'), 
(2,'UP-LKO','LKO','UP'), 
(3,'MP-BHP','BHP','MP'), 
(4,'MHR-MUM','MUM','MHR') 

INSERT INTO @RF_DETAILS VALUES 
(1,'DEL-NDL',42,435,'2016-06-13 05:15:00'), 
(2,'UP-LKO',0,501,'2016-06-13 05:15:00'), 
(3,'MP-BHP',20,350,'2016-06-13 05:15:00'), 
(4,'MHR-MUM',30,200,'2016-06-13 05:15:00'), 
(5,'MHR-MUM',15,100,'2016-06-14 05:15:00'), 
(6,'UP-LKO',50,350,'2016-06-13 05:15:00') 

INSERT INTO @WL_DETAILS VALUES 
(1,'DEL-NDL',25,'2016-06-13 05:15:00'), 
(2,'UP-LKO',35,'2016-06-13 05:30:00'), 
(3,'MP-BHP',46,'2016-06-13 05:45:00'), 
(4,'MHR-MUM',20,'2016-06-13 05:15:00'), 
(5,'MHR-MUM',15,'2016-06-14 05:15:00'), 
(6,'UP-LKO',60,'2016-06-13 05:15:00') 

;with cte as 
(
SELECT case 
     when rf.dt = wl.dt then 'Y' 
     else 'N' 
     end as matched, 
     rf.id as id,rf.station_id as stationid,rf.rf as rf , rf.cum_rf as cumrf , rf.dt as rfdt, 
     wl.id as wlid, wl.station_id ,wl.wl ,wl.dt as wldte, 
     rf.station_id as station,rf.dt as rfdte 
FROM @RF_DETAILS RF 
JOIN  @WL_DETAILS WL ON rf.id = wl.id and RF.STATION_ID = WL.STATION_ID 
) 
select row_number() over (order by s.id) newid, 
     s.id,s.station_id,sd.station_name,sd.state,s.rf,s.cumrf,s.wl, 
     case 
     when s.srce = 'L' then s.rfdte 
     else s.wldte 
     end as 'Date' 
from 
(
select 'L' as srce,cte.id,cte.station_id,cte.rf,cte.cumrf, cte.wl as wl, cte.rfdte,cte.wldte from cte where cte.matched = 'Y' 
union 
select 'L' as srce,cte.id,cte.station_id,cte.rf,cte.cumrf, null as wl, cte.rfdte,cte.wldte from cte where cte.matched = 'N' 
union all 
select 'R' as srce,cte.id * 10,cte.station_id,null,null, cte.wl as wl, cte.rfdte,cte.wldte from cte where cte.matched = 'N' 
) s 
join @station_details sd on sd.station_id = s.station_id 
order by s.id 
+0

爲什麼這些臨時表和insertion.these是示例data.actual表具有400000行在wl_details表和150000行在rf_details表 – vim

+0

輸入表匹配您的樣品數據和輔助測試。我認爲你需要改進你的問題和結果,說明如何在rf和wl之間匹配項目,以及如果項目存在於rf而不是wl以及如果項目存在於wl而不是rf中會發生什麼。 –