select
TG003 as 銷貨日
,rtrim(TG004) as 客代
,rtrim(MA002) as 客戶簡稱
,('') as 銷貨單 --用sql()去產生多銷貨單號
,(select TOP(1) rtrim(TG114) from COPTG
where TG023 <> 'V' and TG003 = '${銷貨日}' and TG004 = '${客代}' and rtrim(TG114) <> '' )
as 報關行
,sum(TG045+TG046) as 銷貨金額
,(select TOP(1) rtrim(UDF07) from COPTG
where TG023 <> 'V' and TG003 = '${銷貨日}' and TG004 = '${客代}' and rtrim(UDF07) <> '' )
as 報關費用
,(select TOP(1) rtrim(TG115) from COPTG
where TG023 <> 'V' and TG003 = '${銷貨日}' and TG004 = '${客代}' and rtrim(TG115) <> '' )
as 備註
,sum(TH037+TH038) as 實際報關費用
from COPTG G
inner join COPMA A on TG004 = MA001
where TG023 <> 'V' and TG003 = '${銷貨日}' and TG004 = '${客代}'
left join COPTH H on TG001 = TH001 and TG002 = TH002
where rtrim(TH004) like 'PS%'
group by TG001 , TG002, TG003 ,rtrim(TG004) ,rtrim(MA002)
SQL 問題
請先至db fiddle建好資料表及內容資料
再來問會比較有效率