2011-12-15 5 views
0

私の現在の役割では、SQLに新たに何が追加されているのですか?クエリでこれを行うには(例)?

次のクエリを実行するにはどうすればよいですか?

select * 
from property.lease_period lp 
where lp.lease_current_stop_date < getdate() and (lp.lease_status = 'Active' or lp.lease_status = 'Overholding') 
and lp.period_id = 263  --- Period ID 
and lp.building_id = 40000 --- Building ID 
and not (SELECT * 
      FROM property.lease_period lp 
      inner join lease_deal.lease ld on lp.suite_id = ld.tenancy_reference 
      where lp.lease_current_stop_date < getdate() and (lp.lease_status = 'Active' or lp.lease_status = 'Overholding') 
      and lp.period_id = 263 
      and lp.building_id = 40000) 

は基本的に私はからの結果を表示する:基本的な質問のための

SELECT * 
FROM property.lease_period lp 
inner join lease_deal.lease ld on lp.suite_id = ld.tenancy_reference 
where lp.lease_current_stop_date < getdate() and (lp.lease_status = 'Active' or lp.lease_status = 'Overholding') 
and lp.period_id = 263 
and lp.building_id = 40000 

申し訳ありません:

select * 
from property.lease_period lp 
where lp.lease_current_stop_date < getdate() and (lp.lease_status = 'Active' or lp.lease_status = 'Overholding') 
and lp.period_id = 263  --- Period ID 
and lp.building_id = 40000 --- Building ID 

一致する結果を含めずに!また、私のSQLをより良くフォーマットするためのこれ以外のヒントもあれば幸いです!

編集

私はこれは私が探しているものに溶液であってもよいと考えている:

select 
    * 
from 
    property.lease_period lp 
where 
    lp.lease_current_stop_date < getdate() and (lp.lease_status = 'Active' or lp.lease_status = 'Overholding') 
    and lp.period_id = 263  --- Period ID 
    and lp.building_id = 40000 --- Building ID 
    and not exists 
    (
     select 1 
     from lease_deal.lease 
     where lp.suite_id = tenancy_reference 
    ) 
order by period_id desc 

これを書くためのより良い方法を聞き間違い興味を持って!

+2

私はあなたが探している文は:例外です。 2つのステートメントの間でそれを叩き、魔法が起こるのを見てください。)...私はあなたが何とか1つのクエリで求める結果を得ることができるかと疑っています。 –

答えて

1

これは(私の個人的なフォーマットの好みと一緒に)それを処理する簡単な方法かもしれません:

SELECT 
    * 
FROM 
    property.lease_period lp 
WHERE 
    lp.lease_current_stop_date < GETDATE() 
    AND 
    (lp.lease_status = 'Active' or lp.lease_status = 'Overholding') 
    AND 
    lp.period_id = 263  --- Period ID 
    AND 
    lp.building_id = 40000 --- Building ID 
    AND 
    lp.suite_id NOT IN (SELECT tenancy_reference FROM lease_deal.lease) 
1

あなたの例外テーブルに参加し、左のみの参加とはヌルの一致を受け入れることができます。

select lp.* 
from property.lease_period lp 
left join lease_deal.lease ld on lp.suite_id = ld.tenancy_reference 
where lp.lease_current_stop_date < getdate() 
and (lp.lease_status = 'Active' or lp.lease_status = 'Overholding') 
and lp.period_id = 263  --- Period ID 
and lp.building_id = 40000 --- Building ID 
and ld.tenancy_reference is null 
関連する問題