我试图在MS SQL Server的后续列中使用计算中一列的结果。
下面是我的SQL代码:
SELECT
(SELECT 'EURUSD' AS a) AS symbol_name,
(SELECT TOP 1 ROUND(p.BidLast,5) FROM mt5_prices p WHERE p.Symbol LIKE 'EURUSD%') AS Bid,
(SELECT SUM(m.Volume)
FROM mt5_positions m
WHERE m.Symbol LIKE 'EURUSD%' AND m.Login IN (SELECT u.Login FROM mt5_users u WHERE u."Group" LIKE 'real%') AND m."Action" = 0) AS TotalBuy,
(SELECT CASE
WHEN 'EURUSD' LIKE 'USOIL%' OR 'EURUSD' LIKE 'UKOIL%' OR 'EURUSD' LIKE 'Bund%' THEN ROUND(TotalBuy/10*Bid,2)
WHEN 'EURUSD' LIKE 'NGAS%' OR 'EURUSD' LIKE 'Copper%' THEN ROUND(TotalBuy/1*Bid,2)
ELSE ROUND(TotalBuy/100*Bid,2) END) AS ExpositionBuyLots
当然,返回的错误是:
SQL错误(207):列名无效' TotalBuy'
列名称无效'出价'。
我的问题是:如何使用已经(或者说,#34;即时")来自列的计算结果TotalBuy
和Bid
?我读到了有关计算列的信息,但不幸的是,无法修改数据库。
MS SQL Server 2012版。
编辑:以上功能在MySQL中可用,我希望在MS SQL中也可以实现。 如果有人对解决方案感兴趣(我选择了@Damien,因为它最接近原始MySQL查询(是的,它是查询,从MySQL重写为MSSQL),即只需要小的SQL修改):SELECT * FROM
(SELECT 'EURUSD' AS symbol_name) symbol_name
CROSS apply
(SELECT TOP 1 ROUND(p.AskLast,5) AS Ask FROM mt5_prices p WHERE p.Symbol LIKE 'EURUSD%') Ask
CROSS apply
(SELECT TOP 1 ROUND(p.BidLast,5) AS Bid FROM mt5_prices p WHERE p.Symbol LIKE 'EURUSD%') Bid
CROSS apply
(SELECT SUM(m.Volume) AS TotalBuy
FROM mt5_positions m
WHERE m.Symbol LIKE 'EURUSD%' AND m.Login IN (SELECT u.Login FROM mt5_users u WHERE u."Group" LIKE 'real%') AND m."Action" = 0) TotalBuy
CROSS apply
(SELECT SUM(m.Volume) AS TotalSell
FROM mt5_positions m
WHERE m.Symbol LIKE 'EURUSD%' AND m.Login IN (SELECT u.Login FROM mt5_users u WHERE u."Group" LIKE 'real%') AND m."Action" = 1) TotalSell
CROSS apply
(SELECT (CASE
WHEN 'EURUSD' LIKE 'USOIL%' OR 'EURUSD' LIKE 'UKOIL%' OR 'EURUSD' LIKE 'Bund%' THEN ROUND(TotalBuy/10*Bid,2)
WHEN 'EURUSD' LIKE 'NGAS%' OR 'EURUSD' LIKE 'Copper%' THEN ROUND(TotalBuy/1*Bid,2)
ELSE ROUND(TotalBuy/100*Bid,2) END) AS ExpositionBuyLots) ExpositionBuyLots
答案 0 :(得分:3)
您可以使用其他子查询块来实现此目的:
SELECT (
CASE
WHEN 'EURUSD' LIKE 'USOIL%'
OR 'EURUSD' LIKE 'UKOIL%'
OR 'EURUSD' LIKE 'Bund%'
THEN ROUND(TotalBuy / 10 * Bid, 2)
WHEN 'EURUSD' LIKE 'NGAS%'
OR 'EURUSD' LIKE 'Copper%'
THEN ROUND(TotalBuy / 1 * Bid, 2)
ELSE ROUND(TotalBuy / 100 * Bid, 2)
END
) AS ExpositionBuyLots, *
FROM (
SELECT ( SELECT 'EURUSD' AS a ) AS symbol_name
,( SELECT TOP 1 ROUND(p.BidLast, 5)
FROM mt5_prices p
WHERE p.Symbol LIKE 'EURUSD%'
) AS Bid
,( SELECT SUM(m.Volume)
FROM mt5_positions m
WHERE m.Symbol LIKE 'EURUSD%'
AND m.LOGIN IN (
SELECT u.LOGIN
FROM mt5_users u
WHERE u."Group" LIKE 'real%'
)
AND m."Action" = 0
) AS TotalBuy
) AS R
答案 1 :(得分:3)
您可以使用cross apply
(或子查询)来引入其他select
子句。计算每个select
子句"好像"所有表达式都是并行计算的 - 这就是为什么单个select
子句中的表达式之间不存在依赖关系的原因。
E.g:
select
*
from
sys.objects
cross apply
(select object_id + 10 as mega_object_id) u
cross apply
(select mega_object_id / 30 as little_object_id) w
答案 2 :(得分:0)
您应该使用APPLY
运算符。您可以使用YourTable
APPLY
的值
SELECT
ExpositionBuyLots = CASE
WHEN 'EURUSD' LIKE 'USOIL%' OR 'EURUSD' LIKE 'UKOIL%' OR 'EURUSD' LIKE 'Bund%' THEN ROUND(ComputedValues.TotalBuy/10*ComputedValues.Bid,2)
WHEN 'EURUSD' LIKE 'NGAS%' OR 'EURUSD' LIKE 'Copper%' THEN ROUND(ComputedValues.TotalBuy/1*ComputedValues.Bid,2)
ELSE ROUND(ComputedValues.TotalBuy/100 * ComputedValues.Bid,2) END
FROM YourTable
OUTER APPLY (
SELECT
symbol_name = 'EURUSD',
Bid = (SELECT TOP 1 ROUND(p.BidLast,5) FROM mt5_prices p WHERE p.Symbol LIKE 'EURUSD%') ,
TotalBuy = (SELECT SUM(m.Volume)
FROM mt5_positions m
WHERE m.Symbol LIKE 'EURUSD%' AND m.Login IN (SELECT u.Login FROM mt5_users u WHERE u."Group" LIKE 'real%') AND m."Action" = 0
)
) ComputedValues