0

正常に実行されたクエリの結果を別のテーブルに入力するにはどうすればよいですか?

これが私のクエリです

select  

isnull (start_charge.account,'No account at start') Start_charge_account
,Finish_charge.account start_charge_account   
,case when start_charge.account is null then finish_charge.account else       start_charge.account end "account" 
,isnull (start_charge.Branch,99999999999) Start_charge_Branch
,Finish_charge.Branch Finish_charge_Branch
,case when start_charge.branch is null then finish_charge.branch else  start_charge.branch end " branch"
,isnull (start_charge. site_number,99999999999) Start_charge_site_number
,Finish_charge. site_number Finish_charge_site_number
,case when start_charge.site_number is null then finish_charge.site_number else start_charge.site_number end "site_number"
,isnull (start_charge. service_code, 'No account at start') Start_charge_service_code
,Finish_charge. service_code finish_charge_service_code
,isnull (start_charge. item_code,'No account at start') Start_charge_item_code
,Finish_charge. item_code finish_charge_item_code
,isnull (start_charge. why_code, 'No account at start') Start_charge_why_code
,Finish_charge. why_code finish_charge_why_code
,isnull (start_charge. why_type, 'No account at start') Start_charge_why_type
,Finish_charge. why_type finish_charge_why_type

,Start_charge.Start_charge
,isnull(Finish_charge.Finish_charge,0) Finish_charge
,isnull(Start_charge.Start_charge,0)-isnull(Finish_charge.Finish_charge,0) Variance


from

(
select

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
,sum(charge)
 "Start_Charge"

from

cannon_commercial

where

1=1
and record_type = 'PS'
and period = ' 02/07/2013'

group by

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
) start_charge 



    full outer join 



( select

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
,concat(account,site_number) "account - site"
,ISNULL(sum(charge),0) "Finish_Charge"

from

cannon_commercial

where

1=1
and record_type= 'PS'
and period = ' 04/06/2013'

group by

account
,branch
,site_number
,service_code
,item_code
,why_code
,why_type
)
Finish_charge

on  Start_charge.Account = Finish_charge.Account
and Start_charge.Branch = Finish_charge.Branch
and Start_charge.site_number = Finish_charge.site_number
and Start_charge.item_code = Finish_charge.item_code
and Start_charge.service_code = Finish_charge.service_code
and Start_charge.why_code = Finish_charge.why_code
and Start_charge.why_type = Finish_charge.why_type
4

2 に答える 2

0

これに対処するには、次の 2 つの方法があります。

CREATE TABLE mytable AS
  (SELECT ... )

クエリの結果を含む新しいテーブルが作成されます。

INSERT INTO mytable (field1, field2, ...)
  (SELECT ... )

クエリの結果を既存のテーブルに挿入します。当然、列リストが一致していることを確認する必要があります。クエリが既存のテーブルと正確に一致する列リストを返す場合、フィールド リストを指定する必要はなく、ステートメントは単純になります。

INSERT INTO mytable (SELECT ... )
于 2013-09-11T09:21:14.350 に答える