表别名无法识别

时间:2018-10-08 18:15:04

标签: sql tsql alias

我有一些代码正在创建三个别名表(WEYellow,WEGreen和WEPutaway),以返回一个带有黄色,绿色和上架日期的表。我可以毫无问题地获得前两个所需的数据。第三个表需要同时使用第一个表和第二个表的别名,但是不能识别第二个别名。我想我在联接之间丢失了一些东西,但是我不知道是什么。

SELECT 
   WEYellow.[Item No_], 
   WEYellow.[Registering Date] as [Yellow Date],
   WEYellow.[User ID] as [Yellow User],

   --GREEN DATE
   ISNULL(
    (SELECT  MIN([Registering Date]) as [GrnDate]
    FROM [Coverpools 2013 Live].[dbo].[Cover-Pools, Inc_$Warehouse Entry] as WEGreen
    WHERE WEGreen.[Item No_] = WEYellow.[Item No_] 
      AND WEGreen.[Registering Date] > WEYellow.[Registering Date] 
      AND WEGreen.[Location Code] = WEYellow.[Location Code]
      AND WEGreen.[Bin Code] = 'GREEN'
      AND [Qty_ (Base)] > 0), 
    '1900-01-01') as [Green Date],

     --PUTAWAY BIN Date
        ISNULL((SELECT MIN([Registering Date]) as [PutDate]
     FROM [Coverpools 2013 Live].[dbo].[Cover-Pools, Inc_$Warehouse Entry] as WEPutaway
      WHERE WEPutaway.[Item No_] = WEYellow.[Item No_] 
          AND WEPutaway.[Registering Date] > WEYellow.[Registering Date] 
          AND WEPutaway.[Registering Date] > WEGreen.[Registering Date] -- THIS IS THE PROBLEM
          AND WEPutaway.[Location Code] = WEYellow.[Location Code]
         --Next Bin that's not Green, Yellow, or User
         AND WEPutaway.[Bin Code] <> 'GREEN' 
          AND WEPutaway.[Bin Code] <> 'YELLOW' 
         And NOT EXISTS (Select 1 from [Coverpools 2013 Live].[dbo].[Cover-Pools, Inc_$Mobile User Setup] as MU
                                       where MU.[Short User ID] = WEPutaway.[Bin Code])
          AND [Qty_ (Base)] > 0), 
    '') as [Putaway Bin Date]


     FROM [Coverpools 2013 Live].[dbo].[Cover-Pools, Inc_$Warehouse Entry] as WEYellow
      WHERE WEYellow.[Location Code] = '01' 
            AND WEYellow.[Bin Code] = 'YELLOW'
            AND [Qty_ (Base)] > 0

1 个答案:

答案 0 :(得分:0)

您不能在同一主查询中引用另一个子查询中的一个子查询。

您可以做的是用主查询和第一个子查询创建一个公用表表达式,然后从中选择,然后添加第二个子查询:

;WITH CTE AS
(
SELECT 
   WEYellow.[Item No_], 
   WEYellow.[Registering Date] as [Yellow Date],
   WEYellow.[User ID] as [Yellow User],

   --GREEN DATE
   ISNULL(
    (SELECT  MIN([Registering Date]) as [GrnDate]
    FROM [Coverpools 2013 Live].[dbo].[Cover-Pools, Inc_$Warehouse Entry] as WEGreen
    WHERE WEGreen.[Item No_] = WEYellow.[Item No_] 
      AND WEGreen.[Registering Date] > WEYellow.[Registering Date] 
      AND WEGreen.[Location Code] = WEYellow.[Location Code]
      AND WEGreen.[Bin Code] = 'GREEN'
      AND [Qty_ (Base)] > 0), 
    '1900-01-01') as [Green Date],

     --PUTAWAY BIN Date
        ISNULL((SELECT MIN([Registering Date]) as [PutDate]
     FROM [Coverpools 2013 Live].[dbo].[Cover-Pools, Inc_$Warehouse Entry] as WEPutaway
      WHERE WEPutaway.[Item No_] = WEYellow.[Item No_] 
          AND WEPutaway.[Registering Date] > WEYellow.[Registering Date] 
          AND WEPutaway.[Registering Date] > WEGreen.[Registering Date] -- THIS IS THE PROBLEM
          AND WEPutaway.[Location Code] = WEYellow.[Location Code]
         --Next Bin that's not Green, Yellow, or User
         AND WEPutaway.[Bin Code] <> 'GREEN' 
          AND WEPutaway.[Bin Code] <> 'YELLOW' 
         And NOT EXISTS (Select 1 from [Coverpools 2013 Live].[dbo].[Cover-Pools, Inc_$Mobile User Setup] as MU
                                       where MU.[Short User ID] = WEPutaway.[Bin Code])
          AND [Qty_ (Base)] > 0), 
    '') as [Putaway Bin Date]


     FROM [Coverpools 2013 Live].[dbo].[Cover-Pools, Inc_$Warehouse Entry] as WEYellow
      WHERE WEYellow.[Location Code] = '01' 
            AND WEYellow.[Bin Code] = 'YELLOW'
            AND [Qty_ (Base)] > 0
)

SELECT *,
     --PUTAWAY BIN Date
     ISNULL((SELECT MIN([Registering Date]) as [PutDate]
     FROM [Coverpools 2013 Live].[dbo].[Cover-Pools, Inc_$Warehouse Entry] as WEPutaway
     WHERE WEPutaway.[Item No_] = WEYellow.[Item No_] 
     AND WEPutaway.[Registering Date] > WEYellow.[Registering Date] 
     AND WEPutaway.[Registering Date] > WEGreen.[Registering Date] -- THIS IS THE PROBLEM
     AND WEPutaway.[Location Code] = WEYellow.[Location Code]
     --Next Bin that's not Green, Yellow, or User
     AND WEPutaway.[Bin Code] <> 'GREEN' 
     AND WEPutaway.[Bin Code] <> 'YELLOW' 
     And NOT EXISTS (
         Select 1 
         from [Coverpools 2013 Live].[dbo].[Cover-Pools, Inc_$Mobile User Setup] as MU
         where MU.[Short User ID] = WEPutaway.[Bin Code]
     )
     AND [Qty_ (Base)] > 0), 
    '') as [Putaway Bin Date]
FROM CTE