0

私は次のような1つのテーブルmytradeを持っています

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

ここで、「合計」数量に基づいて値をsecondtableに挿入します。

insert secondTable (securityid,quantity,price)
(
select securityid,quantity,101.1 from mydata..mytrade
where #temptable.total = 13000 and securityid = 899
)

しかし、エラーが発生します:

マルチパート識別子「#temptable.total」をバインドできませんでした。

このステートメント全体を#temptableに入れてから、上記のように割り当てると、このエラーも発生します。「合計」列をどのようにバインドする必要がありますか?

4

2 に答える 2

4

これを試して:

INSERT secondTable (securityid,quantity,price)
(
SELECT securityid,quantity,101.1 FROM (
  SELECT
tradeid,
securityid,
sum(quantity) OVER(Partition BY securityid) AS total,
quantity
FROM mytrade)T
WHERE total = 13000 AND securityid = 899
)

SQLFiddleで完全に機能するソリューションを見つけることができます。

于 2012-10-13T18:02:05.960 に答える
0
select securityid,quantity,101.1 from #temptable
where #temptable.total = 13000 and securityid = 899
于 2012-10-13T18:01:22.993 に答える