【问题标题】:Not able to use temporary column created using Cross apply in Left Join SQL无法使用在左连接 SQL 中使用交叉应用创建的临时列
【发布时间】:2018-05-11 05:16:15
【问题描述】:

我已使用 Cross apply 将数据从 Wide 转换为 Tall 格式。使用以下问题的答案来实现Using PIVOT to Flip Data from Wide to Tall

我创建了临时列Sector。我想将此列用于与另一个表的左连接。但我在这条线上V.Sector = S.[Sector name] 收到错误V.Sector = S.[Sector name]

SELECT Date,
      Country,
      Sector,
      PE,
      PX_BOOK
      S.Sector_level
FROM [Economic_Data].[dbo].[Sector_Valuations] V
CROSS APPLY 
(
  VALUES
    ('Large Cap Equity',[Large Cap Equity_PE],[Large Cap Equity_book]),
    ('Mid Cap Equity',[Mid Cap Equity_PE],[Mid Cap Equity_book]),
    ('Small Cap Equity',[Small Cap Equity_PE],[Small Cap Equity_book]),
    ('Value Index',[Value Index_PE],[Value Index_book]),
    ('Growth Index',[Growth Index_PE],[Growth Index_book]),
    )  x (Sector, PE, PX_BOOK)  
 Left join [Economic_Data].[dbo].[Sector level] S
 on  V.Sector = S.[Sector name] 

谁能帮我解决这个问题。谢谢!

【问题讨论】:

    标签: sql sql-server join cross-apply


    【解决方案1】:

    你需要参考cross apply别名:

    [Economic_Data].[dbo].[Sector level] S
    on V.Sector = x.[Sector name] 
    --------------^
    

    【讨论】:

      【解决方案2】:

      看起来您需要删除最后一个逗号。

      SELECT Date,
            Country,
            Sector,
            PE,
            PX_BOOK
            S.Sector_level
      FROM [Economic_Data].[dbo].[Sector_Valuations] V
      CROSS APPLY 
      (
        VALUES
          ('Large Cap Equity',[Large Cap Equity_PE],[Large Cap Equity_book]),
          ('Mid Cap Equity',[Mid Cap Equity_PE],[Mid Cap Equity_book]),
          ('Small Cap Equity',[Small Cap Equity_PE],[Small Cap Equity_book]),
          ('Value Index',[Value Index_PE],[Value Index_book]),
          ('Growth Index',[Growth Index_PE],[Growth Index_book])--, <------
          )  x (Sector, PE, PX_BOOK)  
       Left join [Economic_Data].[dbo].[Sector level] S
       on  V.Sector = S.[Sector name] 
      

      【讨论】:

        猜你喜欢
        • 2021-09-16
        • 2013-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-03
        • 2015-06-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多