1

使用 SQL Server 2008

问题 我继承了一些包含带有连接的嵌套子查询的代码。我现在需要添加一个输出列,该列对表中已有的两列执行计算。(子查询、连接、列别名)> Me 的组合。感谢帮助!

当前代码

SELECT TABLE1.externalid, TABLE1.startdate, TABLE1.enddate, TABLE1.Price,
    DATEDIFF(D,TABLE1.startdate,TABLE1.enddate) AS [Days],
    TABLE2.UserDefinedIndex, TABLE2.commodity, 


     (SELECT TOP 1 DATE 
        FROM (SELECT TOP
                (SELECT CASE COUNT(*) WHEN 0 THEN 1 ELSE COUNT(*) END -1 
                FROM TABLE3 WHERE UserDefinedIndex = 'L'  AND DATE < TABLE1.startdate
                    AND  DATE BETWEEN CAST(Table1.startdate as datetime)-6 AND TABLE1.startdate 
            )* 
            FROM TABLE3 WHERE UserDefinedIndex = 'L' AND DATE < TABLE1.startdate 
                AND DATE BETWEEN CAST(TABLE1.startdate as datetime)-6 AND TABLE1.startdate
                ORDER BY DATE
            ) AS A ORDER BY DATE DESC
        )     AS Final_Pricing_Date,

     (SELECT TOP 1 PRICE 
        FROM (SELECT TOP 
                (SELECT CASE COUNT(*) WHEN 0 THEN 1 ELSE COUNT(*) END -1 
                FROM TABLE3 WHERE UserDefinedIndex = 'L' AND DATE < TABLE1.startdate
                    AND DATE BETWEEN CAST(Table1.startdate as datetime)-6 AND TABLE1.startdate  
                )*
             FROM TABLE3  WHERE UserDefinedIndex = 'L' AND DATE < TABLE1.startdate 
                AND DATE BETWEEN CAST(TABLE1.startdate as datetime)-6 AND TABLE1.startdate ORDER BY DATE
            )   AS A ORDER BY DATE DESC
        )/100   AS Final_Price

FROM  TABLE1 INNER JOIN TABLE2
    ON TABLE1.underlying= TABLE2.underlying

期望的输出 我需要一些可以让我返回 [Final_Price] 计算的东西。当前输出列有:externalid、startdate、enddate、Price、Days、UserDefinedIndex、commercial、Final_Pricing_Date、Final_Price

我想在末尾添加列 [PriceDelta],即 TABLE1.Price - Final_Price

谢谢!

4

1 回答 1

2

不完全重写查询的最简单方法是使用另一个子查询,包装整个现有查询,然后使用内部查询的列进行计算。

SELECT  A.*,
        A.Price - A.[Final_Price] AS [PriceDelta]
FROM (
        SELECT TABLE1.externalid, TABLE1.startdate, TABLE1.enddate, TABLE1.Price,
            DATEDIFF(D,TABLE1.startdate,TABLE1.enddate) AS [Days],
            TABLE2.UserDefinedIndex, TABLE2.commodity, 


             (SELECT TOP 1 DATE 
                FROM (SELECT TOP
                        (SELECT CASE COUNT(*) WHEN 0 THEN 1 ELSE COUNT(*) END -1 
                        FROM TABLE3 WHERE UserDefinedIndex = 'L'  AND DATE < TABLE1.startdate
                            AND  DATE BETWEEN CAST(Table1.startdate as datetime)-6 AND TABLE1.startdate 
                    )* 
                    FROM TABLE3 WHERE UserDefinedIndex = 'L' AND DATE < TABLE1.startdate 
                        AND DATE BETWEEN CAST(TABLE1.startdate as datetime)-6 AND TABLE1.startdate
                        ORDER BY DATE
                    ) AS A ORDER BY DATE DESC
                )     AS Final_Pricing_Date,

             (SELECT TOP 1 PRICE 
                FROM (SELECT TOP 
                        (SELECT CASE COUNT(*) WHEN 0 THEN 1 ELSE COUNT(*) END -1 
                        FROM TABLE3 WHERE UserDefinedIndex = 'L' AND DATE < TABLE1.startdate
                            AND DATE BETWEEN CAST(Table1.startdate as datetime)-6 AND TABLE1.startdate  
                        )*
                     FROM TABLE3  WHERE UserDefinedIndex = 'L' AND DATE < TABLE1.startdate 
                        AND DATE BETWEEN CAST(TABLE1.startdate as datetime)-6 AND TABLE1.startdate ORDER BY DATE
                    )   AS A ORDER BY DATE DESC
                )/100   AS Final_Price

        FROM  TABLE1 INNER JOIN TABLE2
            ON TABLE1.underlying= TABLE2.underlying
    ) AS A
于 2013-09-19T22:48:21.063 回答