我有一個表像mytrade以下如何在sql查詢中使用派生表列?
tradeid securityid quantity
111 899 12000
112 899 1000
113 788 15000
114 566 -15000
115 566 -1000
所以要收集安全ID我寫下面的查詢總量(我試過到#temptable這也創造不是Temptable,然後選擇下方)
select
tradeid,
securityid,
sum(quantity) OVER(Partition by securityid) as total
from mytrade
這給我像下面的輸出
tradeid securityid total
114 566 -16000
115 566 -16000
113 788 15000
111 899 13000
112 899 13000
現在我想要插入到第二表中的數據,基於「總」數量。
insert secondTable (securityid,quantity,price)
(
select securityid,quantity,101.1 from mydata..mytrade
where #temptable.total = 13000 and securityid = 899
)
,但得到的錯誤:
多部分組成的標識符 「#temptable.total」 無法綁定。
如果我把這整個語句放入#temptable,然後如上分配然後也得到這個錯誤我該如何綁定「總計」列請引導我?
你在哪裏引用from子句中的#tempTable? –