簡體   English   中英

SQL Server - 使用 Case 或 IsNull 查詢聯接

[英]SQL Server - Query Joins using Case or IsNull

我有一個需要編寫查詢的場景。

我有一個Query1 ,它根據一些連接返回帶有這四個列名( batch_no, batch_name, class_no, class_name )的數百條記錄。 我需要編寫一個最終查詢 ( Query2 ) 以從使用Query1返回的class_nobatch_no列值的其他表中返回列。

我的最終查詢 ( query2 ) 應該為query1返回的所有記錄返回這些列:

Batch_global_Id (in table global_details), 
batch_no, batch_name, class_no, class_name, 
Start_year, End_year (from table time_details)

Global_detailsclass_no列,因此我們可以將 query1 的結果與Global_details以獲取最終查詢 ( Query2 ) 中Batch_global_Id列的值。

但這里的問題是從表time_details獲取Start_yearEnd_year值。 我們需要根據class_no, batch_no query1 返回的所有記錄的兩個字段值。

這是獲取這些值的條件 ( Start_year, End_year )。

  • 如果time_details表中有一個帶有class_no的記錄,我們需要在最終輸出查詢中從該記錄中獲取Start_yearEnd_year

  • 如果在沒有記錄time_details與該表class_no ,然后拿到Start_yearEnd_year匹配從記錄值batch_notime_details表。

  • 如果既存在(否記錄time_details與匹配表class_nobatch_no ),然后得到Start_yearEnd_year從在所述記錄的值time_detailsbatch_no = null

有人可以幫我寫這個查詢嗎?

根據評論編輯

Select 
    batch_no, batch_name, class_no, class_name 
into 
    #temptable 
from 
    ... (query1) ...

預期查詢

Select 
    gd.Batch_global_Id, 
    tt.batch_no, tt.batch_name, tt.class_no, tt.class_name, 
    td.Start_year, td.End_year 
from 
    Global_details gd 
inner join 
    time_details td on gd.class_no = td.class_no 
inner join 
    **something like to get values from time_details table based on the above condition//**

沒有樣本數據很難具體說明,但這應該會讓你走上正軌。 根據表的大小,選擇start_yearend_year其中batch_no is null首先到變量中,然后在合並的末尾插入這些變量,而不是添加最后一個連接可能會更快。

Select a.batch_no, a.batch_name, a.class_no, a.class_name
    , b.Batch_global_Id 
    , coalesce(c.Start_year, d.Start_year, e.Start_year) as StartYear
    , coalesce(c.End_year, d.End_year, e.End_year) as EndYear
from [Query1Results] a
left join Global_details b
on a.class_no = b.class_no
left join time_details c
on a.class_no = c.class_no
left join time_details d
on a.batch_no = d.batch_no
left join time_details e
on e.batch_no is null

請試試這個

Select 
    gd.Batch_global_Id, 
    tt.batch_no, tt.batch_name, tt.class_no, tt.class_name, 
    td.Start_year, td.End_year 
from 
    Global_details gd, time_details td, temp_table tt
where tt.class_no = gd.class_no
and tt.class_no = td.class_no

union

Select 
    gd.Batch_global_Id, 
    tt.batch_no, tt.batch_name, tt.class_no, tt.class_name, 
    td.Start_year, td.End_year 
from 
    Global_details gd, time_details td, temp_table tt
where tt.class_no = gd.class_no
and tt.batch_no = td.batch_no
and tt.class_no not in (Select tt.class_no from 
    Global_details gd, time_details td, temp_table tt
where tt.class_no = gd.class_no
and tt.class_no = td.class_no)

union 

Select 
    gd.Batch_global_Id, 
    tt.batch_no, tt.batch_name, tt.class_no, tt.class_name, 
    td.Start_year, td.End_year 
from 
    Global_details gd, time_details td, temp_table tt
where tt.class_no = gd.class_no
and td.batch_no is null
and tt.batch_no not in (Select td.batch_no 
from 
    Global_details gd, time_details td, temp_table tt
where tt.class_no = gd.class_no
and tt.batch_no = td.batch_no
and tt.class_no not in (Select tt.class_no from 
    Global_details gd, time_details td, temp_table tt
where tt.class_no = gd.class_no
and tt.class_no = td.class_no))

這是我的邏輯

我根據您的要求將查詢分為三個部分

第一個查詢給出帶有 class_no 條件的結果

第二個查詢給出 batch_no 條件並排除來自第一個查詢的記錄

以同樣的方式使用 batch_no nu 條件進行第三次查詢,並排除來自第二次查詢的記錄。

如果你能給我提供數據,那對測試會更好。

我知道當你的記錄更多時這會導致性能問題,但你現在可以嘗試這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM