如何从创建的查询中引用列

时间:2014-08-01 19:11:56

标签: sql-server tsql

在此代码中,我想使用我的材料日期(从查询创建的列)作为数字2是否出现在2列中的指示符。在第二列中我想要的伪代码是:   “如果[材料]是空的那么'2'”这是我在上一个案例陈述中所尝试的内容。如果我发布的帖子不正确,我会发出aplogize,这是我在本网站上的第一个SQL作业/帖子之一。

USE [MARKData]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


SELECT DISTINCT
 ord.[OrderID] AS 'Quote #',
 Null As 'T',
 con.[FirstName] +  ' ' + con.[LastName] AS 'Customer Name',
 stat.[StatusDescription] AS 'Current Status',
  '$' + CONVERT(varchar(12), ord.[OrderTotal], 1) AS 'Current Total',
 stath.[UpdateDate] AS 'Last Status Update',
 (DATEDIFF(DAY, GETDATE(), ord.[OrderSubmitDate]) * -1) AS 'Current Elapsed Days',

CASE 
 WHEN stath.[OrderStatusID] = 35
 THEN stath.[CreateDate]
ELSE
 CASE
  WHEN stath.[CreateDate] >= MatPriceRev.[CreateDate]
  THEN MatPriceRev.[CreateDate]
  ELSE NULL  
 END
END AS 'Materials',

CASE 
 WHEN stath.[OrderStatusID] = 34
 THEN stath.[CreateDate]
ELSE
 CASE
  WHEN stath.[CreateDate] >= EngineerRev.[CreateDate]
  THEN EngineerRev.[CreateDate]
  ELSE NULL  
 END
END AS 'Engineer Notified',

 ConSales.[FirstName] +  ' ' + ConSales.[LastName] AS 'Sales Rep', 

CASE
 WHEN ord.[CreateDate] < (CURRENT_TIMESTAMP)
 THEN '1'
END AS '1',

CASE
 WHEN 'Materials' IS NULL
 THEN '2'
END AS '2'


FROM T_Order ord
 LEFT JOIN T_Contact con ON ord.[ContactID] = con.[ContactID]
 LEFT JOIN T_Contact ConSales ON ord.CreateUser = ConSales.[ContactID]
 LEFT JOIN T_OrderStatusHistory stath ON ord.[OrderID] = stath.[OrderID]
 LEFT JOIN T_OrderStatus stat ON stath.[OrderStatusID] = stat.[OrderStatusID]
 LEFT JOIN 
 (Select * From T_OrderStatusHistory Where T_OrderStatusHistory.[OrderStatusID] = 35) AS MatPriceRev 
  ON MatPriceRev.[OrderID] = ord.[OrderID]
 LEFT JOIN 
 (Select * From T_OrderStatusHistory Where T_OrderStatusHistory.[OrderStatusID] = 34) AS EngineerRev 
  ON EngineerRev.[OrderID] = ord.[OrderID]
 LEFT JOIN
 (Select * From T_OrderStatusHistory Where T_OrderStatusHistory.[OrderStatusID] = 41) AS RiskRev 
  ON RiskRev.[OrderID] = ord.[OrderID]



WHERE
 DATEDIFF(DAY, ord.OrderSubmitDate, GETDATE()) >= 0
 AND stath.[CurrentIndicator] = 'True' 
 AND stath.[OrderStatusID] <> 2

ORDER BY 'Quote #' ASC, 'Last Status Update' ASC

2 个答案:

答案 0 :(得分:0)

尝试 -

CASE WHEN 
    CASE WHEN stath.[OrderStatusID] = 35
        THEN stath.[CreateDate]
    WHEN stath.[CreateDate] >= MatPriceRev.[CreateDate]
        THEN MatPriceRev.[CreateDate]
    ELSE NULL  
    END  
 IS NULL THEN '2' END AS '2'

答案 1 :(得分:0)

SQL Server不允许您在同一查询中使用计算列。解决它的一种方法是将查询包装在CTE中,然后允许您在后续查询中引用计算列:

;WITH tmp AS (
SELECT DISTINCT
 ord.[OrderID] AS 'Quote #',
 Null As 'T',
 con.[FirstName] +  ' ' + con.[LastName] AS 'Customer Name',
 stat.[StatusDescription] AS 'Current Status',
  '$' + CONVERT(varchar(12), ord.[OrderTotal], 1) AS 'Current Total',
 stath.[UpdateDate] AS 'Last Status Update',
 (DATEDIFF(DAY, GETDATE(), ord.[OrderSubmitDate]) * -1) AS 'Current Elapsed Days',

CASE 
 WHEN stath.[OrderStatusID] = 35
 THEN stath.[CreateDate]
ELSE
 CASE
  WHEN stath.[CreateDate] >= MatPriceRev.[CreateDate]
  THEN MatPriceRev.[CreateDate]
  ELSE NULL  
 END
END AS 'Materials',

CASE 
 WHEN stath.[OrderStatusID] = 34
 THEN stath.[CreateDate]
ELSE
 CASE
  WHEN stath.[CreateDate] >= EngineerRev.[CreateDate]
  THEN EngineerRev.[CreateDate]
  ELSE NULL  
 END
END AS 'Engineer Notified',

 ConSales.[FirstName] +  ' ' + ConSales.[LastName] AS 'Sales Rep', 

CASE
 WHEN ord.[CreateDate] < (CURRENT_TIMESTAMP)
 THEN '1'
END AS '1',

FROM T_Order ord
 LEFT JOIN T_Contact con ON ord.[ContactID] = con.[ContactID]
 LEFT JOIN T_Contact ConSales ON ord.CreateUser = ConSales.[ContactID]
 LEFT JOIN T_OrderStatusHistory stath ON ord.[OrderID] = stath.[OrderID]
 LEFT JOIN T_OrderStatus stat ON stath.[OrderStatusID] = stat.[OrderStatusID]
 LEFT JOIN 
 (Select * From T_OrderStatusHistory Where T_OrderStatusHistory.[OrderStatusID] = 35) AS MatPriceRev 
  ON MatPriceRev.[OrderID] = ord.[OrderID]
 LEFT JOIN 
 (Select * From T_OrderStatusHistory Where T_OrderStatusHistory.[OrderStatusID] = 34) AS EngineerRev 
  ON EngineerRev.[OrderID] = ord.[OrderID]
 LEFT JOIN
 (Select * From T_OrderStatusHistory Where T_OrderStatusHistory.[OrderStatusID] = 41) AS RiskRev 
  ON RiskRev.[OrderID] = ord.[OrderID]



WHERE
 DATEDIFF(DAY, ord.OrderSubmitDate, GETDATE()) >= 0
 AND stath.[CurrentIndicator] = 'True' 
 AND stath.[OrderStatusID] <> 2
)

SELECT    *,
          CASE WHEN Materials IS NULL THEN 2 END AS [2]
FROM      tmp
ORDER BY  [Quote #], [Last Status Update]