2016-04-25 6 views
0

UPDATEステートメントを使用して情報を更新する必要がある正確な母集団をカウントするSQL照会があります。しかし、いくつかの複雑な結合が含まれており、UPDATEステートメントでそのステートメントを再作成する方法がわかりません。どんな助け?SELECTステートメントを対応するUPDATEステートメントに変換します

私のクエリはここにある:ここ

select distinct c.id 
from implementation.tt_ngma_exclude_emails nee 
join customer c on nee.util_account_id = c.util_internal_id 
join customer_notification_contact cnc on cnc.customer_id = c.id 
left join customer_notification_contact_audit cnca on cnca.customer_id = c.id 
where cnc.changed_on = '2015-11-15 12:30:02'; 

目標は、私はから選択されていない実装テーブル、customer_notification_contactテーブル内の特定のフィールドを更新することです。私はcnc.customer_id = c.id

はここに私の試みだが、動作するようには思えないことをどこでもNULLにCNCテーブルにemailフィールドを設定します:

UPDATE customer_notification_contact cnc 
(select distinct cnc.customer_id opower_customer_id 
from implementation.tt_ngma_exclude_emails nee 
join customer c on nee.util_account_id = c.util_internal_id 
join customer_notification_contact cnc on cnc.customer_id = c.id 
where cnc.changed_on = '2015-11-15 12:30:02' 
) T2 
SET cnc.email = NULL 
WHERE cnc.customer_id = T2.opower_customer_id; 
+1

ジョインは同じままです。 'select ... from ...'を 'update ...'に変更してください。 –

+0

申し訳ありません。元の質問はあいまいでした。私はcustomer_notification_contactを更新したいと思います。 implementation.tt_ngma_exclude_emailsではありません。 – Asif

答えて

1

これを試してみてください。 T1.SOME_COLUMN(2番目の最後の行)を、更新する実際の列名に置き換えてください。私はGROUP BY CNC.CUSTOMER_IDを追加しました。なぜなら、複数の行を更新しているときに、カウントがどの顧客に属しているかを知る必要があるからです。あなたがやろうとしていないと仮定している同じカウントですべての行を更新しようとしている場合を除きます。

UPDATE customer_notification_contact T1, 
(
select count(distinct c.id) AS MY_COUNT,CNC.CUSTOMER_ID 
from implementation.tt_ngma_exclude_emails nee 
join customer c on nee.util_account_id = c.util_internal_id 
join customer_notification_contact cnc on cnc.customer_id = c.id 
left join customer_notification_contact_audit cnca on cnca.customer_id = c.id 
where cnc.changed_on = '2015-11-15 12:30:02' 
GROUP BY CNC.CUSTOMER_ID 
)T2 
SET T1.SOME_COLUMN = T2.MY_COUNT 
WHERE T1.CUSTOMER_ID = T2.CUSTOMER_ID 

UPDATE更新質問を見た後、このクエリでなければなりません。

UPDATE customer_notification_contact T1, 
(
select distinct c.id 
    from implementation.tt_ngma_exclude_emails nee 
    join customer c on nee.util_account_id = c.util_internal_id 
    join customer_notification_contact cnc on cnc.customer_id = c.id 
    left join customer_notification_contact_audit cnca on cnca.customer_id = c.id 
    where cnc.changed_on = '2015-11-15 12:30:02' 
)T2 
SET T1.EMAIL = NULL 
WHERE T1.CUSTOMER_ID = T2.id 
+0

これは私が必要とするものに本当に近いと思います。私はもう少し明確になるように私の質問を更新しました。 – Asif

+0

次に、T2サブクエリを持っているように変更した後、最後の2行目の最後に 'T1.email = NULL'を追加してみてください。 –

+0

更新された答えを一番下に表示します –