2012-02-03 37 views
1

所以我有一個查詢過濾一堆記錄刪除行:加入其它表沒有具體記錄

declare @rDate_datepart datetime 
-- this gets rid of any time information, so we have just the date (i.e, right at midnight of that day) 
set @rDate_datepart = convert(datetime,convert(varchar(2),datepart("m",'2/3/2012'))+'/'+convert(varchar(2),datepart("dd",'2/3/2012'))+'/'+convert(varchar(4),datepart("yyyy",'2/3/2012'))) 

select tq_tran_quote_id, MAX_MATURITY_DT.cm_maturity_dt 
from cfo_tran_quote inner join cfo_transaction 
    on tq_tr_transaction_id = tr_transaction_id 
left join com_lock on tr_transaction_id = lk_tr_transaction_id 
LEFT OUTER JOIN 
(
    SELECT cm_tq_tran_quote_id, MAX(cm_maturity_dt) as 'cm_maturity_dt' 
    FROM cfo_component 
    GROUP BY cm_tq_tran_quote_id 
) AS MAX_MATURITY_DT ON MAX_MATURITY_DT.cm_tq_tran_quote_id = tq_tran_quote_id 
where tq_dd_tran_qt_status = 'Matured' and 
    isnull(lk_rv_lock_status, 10104) = 10104 and 
MAX_MATURITY_DT.cm_maturity_dt between @rDate_datepart - 15 and @rDate_datepart 
and tq_dd_product in 
     (select pr_dd_product 
     from cfo_product 
     where pr_dd_product_type = 'Cash Flow') 

現在我基本上要的東西添加到該查詢,說,給我所有的這行查詢帶回這些特定的tq_tran_quote_id的沒有記錄在另一個表cfo_daily_trans_hist其中dh_valuation_total在那個其他表0

這有道理嗎?

我最不善於用SQL ...笑

回答

2

所有你需要做的就是把你的整個查詢並添加:

WHERE tq_tran_quote_id NOT IN (SELECT tq_tran_quote_id FROM MyOtherTable WHERE dh_valuation_total=0)

基本上它回答你的問題:

現在我基本上想添加一些東西到這個查詢說,給我 所有行這個查詢帶回這些特定的 層tq_tran_quote_id的沒有另一個表 cfo_daily_trans_hist記錄,其中他們在其他 表dh_valuation_total爲0

它只是說如果這tq_tran_quote_id是不是在「其他表」,你將要替換MyOtherTable你的表的真實姓名,他們的dh_valuation_total爲0

+0

這是完美的。謝謝! – slandau 2012-02-03 18:38:54

0
select * 
from (your query) 
where tq_tran_quote_id NOT IN (SELECT tq_tran_quote_id FROM MyOtherTable WHERE dh_valuation_total=0) 

好運