即時通訊使用子查詢返回兩個日期之間的平均金額和總金額,當返回平均金額我必須使用子查詢中選定的列。如何在使用sql server的子查詢中使用選定的列?
以下是我試過的。
tbl_batch_charges
merchant_id total_transaction_amount settlement_date
1 33.65 2014-07-01
1 33.65 2014-07-10
2 39.36 2014-07-15
merchant_application_form
merchant_id active_date
1 2013-10-20
2 2014-01-10
Resulted Table
merchant_id weekly_transaction total_turnover
1 xx 67.3
2 xx 39.36
select tbl_batch.merchant_id,
tbl_merchant.merchant_application_form_active_date,
GETDATE() as today_date,
(select sum(tbl_batch.total_transactions_amount) from Borgun.dbo.tbl_batch_charges as tbl_batch
where tbl_batch.settlement_borgun_date between '2014-07-01' and '2014-10-20' and
tbl_batch.merchant_id='9947230') as weekly_total_amount,
(select (sum(tbl_batch.total_transactions_amount)/(ceiling(convert(float, abs(datediff(day, active_date, today_date)))/7)))) as total_turn_over
from Borgun.dbo.tbl_batch_charges as tbl_batch
left join Borgun.dbo.tbl_merchant_application_form as tbl_merchant on tbl_merchant.merchant_application_form_merchant_id=tbl_batch.merchant_id
where tbl_batch.merchant_id='9947230' group by tbl_batch.merchant_id;
**我希望商家從他加入當天的日期開始平均每週平均翻倍。 **
你能告訴我我將如何解決在sql服務器。
在此先感謝。
樣本數據和預期的結果將真正幫助。 –
編輯的問題,請檢查一次 – user3668438