我有一个像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
现在我想根据“总”数量将值插入第二张表格。
insert secondTable (securityid,quantity,price)
(
select securityid,quantity,101.1 from mydata..mytrade
where #temptable.total = 13000 and securityid = 899
)
但收到错误:
无法绑定多部分标识符“#temptable.total”。
如果我将整个语句放入#temptable然后如上所述分配那么也会得到此错误我应该如何绑定“总”列,请指导我?
答案 0 :(得分: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
)
您可以在SQL Fiddle上找到完整的解决方案。
答案 1 :(得分:0)
select securityid,quantity,101.1 from #temptable
where #temptable.total = 13000 and securityid = 899