我正在尝试使用从另一个表格(Yahoostockdata)
)中提取的信息更新表格incomestatement
,具体取决于日期。
Yahoostockdata
包含每天的雅虎价格,我想根据incomestatement
针对该特定日期提供的最实际信息,添加每个日期的份额数。
当运行查询(没有更新)时,我得到了我想要的所有数字。除了在添加UPDATE
语句时,我收到错误消息: suquery返回的值超过1个。
消息512,级别16,状态1,行1 子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。该声明已被终止。
有人可以帮我解决这个问题吗?我已经看过相关的问题,但找不到答案。先感谢您。在我正在使用的代码下面:
Update YahooStockData
Set NumberOfShares = (Select [Weighted average shares outstanding (Diluted)]
from (Select *,ROW_NUMBER() over (partition by derived_1.MorningstarTicker,
derived_1.[date] order by derived_1.AsOfDate desc) as RN
from (Select Yahoostockdata.YahooTicker, IncomeStatement.MorningstarTicker,
yahoostockdata.[Date], AsOfDate,
Incomestatement.[Weighted average shares outstanding (Diluted)]
from IncomeStatement INNER JOIN
YahooStockData ON IncomeStatement.MorningstarTicker = YahooStockData.MorningstarTicker
Where YahooStockData.[Date] >= AsOfDate) as derived_1) as derived_2
Where RN = 1)
答案 0 :(得分:1)
这可以进一步清理,但我相信这就是你所追求的:
;WITH cte AS (Select MorningstarTicker,[Weighted average shares outstanding (Diluted)]
from (Select *,ROW_NUMBER() over (partition by derived_1.MorningstarTicker,
derived_1.[date] order by derived_1.AsOfDate desc) as RN
from (Select Yahoostockdata.YahooTicker, IncomeStatement.MorningstarTicker,
yahoostockdata.[Date], AsOfDate,
Incomestatement.[Weighted average shares outstanding (Diluted)]
from IncomeStatement
INNER JOIN YahooStockData ON IncomeStatement.MorningstarTicker = YahooStockData.MorningstarTicker
Where YahooStockData.[Date] >= AsOfDate
) as derived_1
) as derived_2
Where RN = 1)
)
UPDATE a
SET a.NumberofShares = b.[Weighted average shares outstanding (Diluted)]
FROM YahooStockData a
JOIN cte b
ON a.MorningstarTicker = b.MorningstarTicker
我已将MorningstartTicker
添加到您的子查询中并将整个位置放入cte中,以使UPDATE
更容易看到JOIN
。
原始查询的问题在于您尝试使用一个值更新每条记录,因为它们与子查询无关。
答案 1 :(得分:0)
试试这个
UPDATE A
SET NumberOfShares = B.[Weighted average shares outstanding (Diluted)]
FROM YahooStockData A
JOIN (SELECT [Weighted average shares outstanding (Diluted)],
derived_2.MorningstarTicker
FROM (SELECT *,
Row_number() OVER ( partition BY derived_1.MorningstarTicker, derived_1.[date]
ORDER BY derived_1.AsOfDate DESC) AS RN
FROM (SELECT Yahoostockdata.YahooTicker,
IncomeStatement.MorningstarTicker,
yahoostockdata.[Date],
AsOfDate,
Incomestatement.[Weighted average shares outstanding (Diluted)]
FROM IncomeStatement
INNER JOIN YahooStockData
ON IncomeStatement.MorningstarTicker = YahooStockData.MorningstarTicker
WHERE YahooStockData.[Date] >= AsOfDate) AS derived_1) AS derived_2
WHERE RN = 1) B
ON A.MorningstarTicker = B.MorningstarTicker