2014-12-04 93 views
0
  1. 我想在RequestStatus_hodIN PROCESS
  2. 但查詢似乎顯示所有的數據通過忽略where子句
  3. 當你只顯示數據可以看到,我試圖重複WHERE RequestStatus_hod = 'IN PROCESS'但它只是顯示爲NULL
  4. 我試圖使用臨時列RequestStatus_hods並獲得an invalid column error

SQL語句:是否可以使用臨時列WHERE子句

SELECT DISTINCT 
    A.RequestNumber, A.EmployeeId, 
    STUFF((SELECT ', ' + CAST(B.RequestDetailsId AS VARCHAR(255)) 
      FROM [dbo].[REQUISITION] C 
      JOIN [dbo].[REQUISITION_DETAILS] B ON C.RequestDetailsId = B.RequestDetailsId 
      WHERE C.RequestNumber = A.RequestNumber AND C.EmployeeId = A.EmployeeId -- AND RequestStatus_hod = 'IN PROCESS' 
      FOR XML PATH ('')), 1, 2, '') AS RequestDetailIds, 
    STUFF((SELECT ', ' + CAST(B.StockId AS VARCHAR(255)) 
      FROM [dbo].[REQUISITION] C 
      JOIN [dbo].[REQUISITION_DETAILS] B ON C.RequestDetailsId = B.RequestDetailsId 
      JOIN [dbo].[STOCK] F ON F.StockId = B.StockId 
      WHERE C.RequestNumber = A.RequestNumber AND C.EmployeeId = A.EmployeeId -- AND RequestStatus_hod = 'IN PROCESS' 
      FOR XML PATH ('')), 1, 2, '') AS StockIds, 
    STUFF((SELECT ', ' + CAST(B.RequestQuantity AS VARCHAR(255)) 
      FROM [dbo].[REQUISITION] C 
      JOIN [dbo].[REQUISITION_DETAILS] B ON C.RequestDetailsId = B.RequestDetailsId 
      WHERE C.RequestNumber = A.RequestNumber AND C.EmployeeId = A.EmployeeId -- AND RequestStatus_hod = 'IN PROCESS' 
      FOR XML PATH ('')), 1, 2, '') AS RequestQuantity, 
    STUFF((SELECT ', ' + G.ItemName 
      FROM [dbo].[REQUISITION] C 
      JOIN [dbo].[REQUISITION_DETAILS] B ON C.RequestDetailsId = B.RequestDetailsId 
      JOIN [dbo].[STOCK] F ON F.StockId = B.StockId 
      JOIN [dbo].[ITEM_MASTER] G ON F.ItemId = G.ItemId 
      WHERE C.RequestNumber = A.RequestNumber AND C.EmployeeId = A.EmployeeId -- AND RequestStatus_hod = 'IN PROCESS' 
      FOR XML PATH ('')), 1, 2, '') AS ItemNames, 
    STUFF((SELECT ', ' + CAST(F.StockQuantity AS VARCHAR(255)) 
      FROM [dbo].[REQUISITION] C 
      JOIN [dbo].[REQUISITION_DETAILS] B ON C.RequestDetailsId = B.RequestDetailsId 
      JOIN [dbo].[STOCK] F ON F.StockId = B.StockId 
      WHERE C.RequestNumber = A.RequestNumber AND C.EmployeeId = A.EmployeeId -- AND RequestStatus_hod = 'IN PROCESS' 
      FOR XML PATH ('')), 1, 2, '') AS AvailableQuantity, 
    STUFF((SELECT ', ' + B.RequestStatus_hod 
      FROM [dbo].[REQUISITION] C 
      JOIN [dbo].[REQUISITION_DETAILS] B ON C.RequestDetailsId = B.RequestDetailsId 
      WHERE C.RequestNumber = A.RequestNumber AND C.EmployeeId = A.EmployeeId -- AND RequestStatus_hod = 'IN PROCESS' 
      FOR XML PATH ('')), 1, 2, '') AS RequestStatus_hods -- Temporary Column -- 
FROM 
    [dbo].[REQUISITION] A 
JOIN 
    [dbo].[USER] D ON D.EmployeeId = A.EmployeeId 
JOIN 
    [dbo].[REQUISITION_DETAILS] E ON E.EmployeeId = D.EmployeeId 
WHERE 
    RequestStatus_hods = 'IN PROCESS' -- Col RequestStatus_hod FROM table REQUISITION_DETAILS -- 

結果:

enter image description here

+1

它是** RequestStatus_hods **還是** RequestStatus_hod **,因爲您的臨時列說RequestStatus_hods,但您的where條件說RequestStatus_hod – CodingDefined 2014-12-04 04:39:32

+0

@CodingDefined其實我想過濾臨時列'RequestStatus_hods'。 'RequestStatus_hod'是表'REQUISITION_DETAILS'的原始列 – 2014-12-04 04:40:54

+0

在最後一次選擇的條件中添加條件'B.RequestStatus_hod ='IN PROCESS''。 – CodingDefined 2014-12-04 04:45:09

回答

0

一種解決方案是重複WHERE子句中的臨時列的定義。 SQL Server不支持直接在WHERE中使用別名,但它仍然足夠智能(在大多數情況下)可以確定不需要對錶達式進行兩次評估。因此,不要在WHERE中寫入RequestStatus_hod = 'IN PROCESS',而要將該列的實際定義複製並粘貼到RequestStatus_hods的位置。

但是,更好的選擇可能是將您的列提取到APPLY子句中。這樣,你肯定知道它不會運行兩次。

例如:

SELECT DISTINCT 
    A.RequestNumber, A.EmployeeId, 
    STUFF((SELECT ', ' + CAST(B.RequestDetailsId AS VARCHAR(255)) 
      FROM [dbo].[REQUISITION] C 
      JOIN [dbo].[REQUISITION_DETAILS] B ON C.RequestDetailsId = B.RequestDetailsId 
      WHERE C.RequestNumber = A.RequestNumber AND C.EmployeeId = A.EmployeeId -- AND RequestStatus_hod = 'IN PROCESS' 
      FOR XML PATH ('')), 1, 2, '') AS RequestDetailIds, 
    STUFF((SELECT ', ' + CAST(B.StockId AS VARCHAR(255)) 
      FROM [dbo].[REQUISITION] C 
      JOIN [dbo].[REQUISITION_DETAILS] B ON C.RequestDetailsId = B.RequestDetailsId 
      JOIN [dbo].[STOCK] F ON F.StockId = B.StockId 
      WHERE C.RequestNumber = A.RequestNumber AND C.EmployeeId = A.EmployeeId -- AND RequestStatus_hod = 'IN PROCESS' 
      FOR XML PATH ('')), 1, 2, '') AS StockIds, 
    STUFF((SELECT ', ' + CAST(B.RequestQuantity AS VARCHAR(255)) 
      FROM [dbo].[REQUISITION] C 
      JOIN [dbo].[REQUISITION_DETAILS] B ON C.RequestDetailsId = B.RequestDetailsId 
      WHERE C.RequestNumber = A.RequestNumber AND C.EmployeeId = A.EmployeeId -- AND RequestStatus_hod = 'IN PROCESS' 
      FOR XML PATH ('')), 1, 2, '') AS RequestQuantity, 
    STUFF((SELECT ', ' + G.ItemName 
      FROM [dbo].[REQUISITION] C 
      JOIN [dbo].[REQUISITION_DETAILS] B ON C.RequestDetailsId = B.RequestDetailsId 
      JOIN [dbo].[STOCK] F ON F.StockId = B.StockId 
      JOIN [dbo].[ITEM_MASTER] G ON F.ItemId = G.ItemId 
      WHERE C.RequestNumber = A.RequestNumber AND C.EmployeeId = A.EmployeeId -- AND RequestStatus_hod = 'IN PROCESS' 
      FOR XML PATH ('')), 1, 2, '') AS ItemNames, 
    STUFF((SELECT ', ' + CAST(F.StockQuantity AS VARCHAR(255)) 
      FROM [dbo].[REQUISITION] C 
      JOIN [dbo].[REQUISITION_DETAILS] B ON C.RequestDetailsId = B.RequestDetailsId 
      JOIN [dbo].[STOCK] F ON F.StockId = B.StockId 
      WHERE C.RequestNumber = A.RequestNumber AND C.EmployeeId = A.EmployeeId -- AND RequestStatus_hod = 'IN PROCESS' 
      FOR XML PATH ('')), 1, 2, '') AS AvailableQuantity, 
    xx.RequestStatus_hods -- Temporary Column -- 
FROM 
    [dbo].[REQUISITION] A 
JOIN 
    [dbo].[USER] D ON D.EmployeeId = A.EmployeeId 
JOIN 
    [dbo].[REQUISITION_DETAILS] E ON E.EmployeeId = D.EmployeeId 
CROSS APPLY 
    (SELECT STUFF((SELECT ', ' + B.RequestStatus_hod 
      FROM [dbo].[REQUISITION] C 
      JOIN [dbo].[REQUISITION_DETAILS] B ON C.RequestDetailsId = B.RequestDetailsId 
      WHERE C.RequestNumber = A.RequestNumber AND C.EmployeeId = A.EmployeeId 
      FOR XML PATH ('')), 1, 2, '') as RequestStatus_hods) xx 
WHERE 
    xx.RequestStatus_hods = 'IN PROCESS' -- Col RequestStatus_hod FROM table REQUISITION_DETAILS -- 
+0

Theres兩個' xx.RequestStatus_hods':'CROSS APPLY'後的'STUFF'上無法綁定多部分標識符** AND **:'無效的對象名稱STUFF' – 2014-12-04 07:47:18

+0

@AthirahHazira對不起,錯過了一小塊!它。請再試一次,請 – Ruslan 2014-12-04 07:53:07

+1

好的,工作!謝謝 :') – 2014-12-04 08:04:01

0

嘗試

WHERE E.RequestStatus_hod = 'IN PROCESS'

+0

將無法​​工作要麼 – 2014-12-04 05:45:38

0

完全取出WHERE條款,並嘗試這而不是

JOIN 
[dbo].[REQUISITION_DETAILS] E 
ON E.EmployeeId = D.EmployeeId 
AND E.RequestStatus_hod = 'IN PROCESS' 
+0

仍然不會工作 – 2014-12-04 05:44:38

0

好吧首先,讓我們叫的東西與他們的名字,RequestStatus_hods是不是「臨時列」,這是擺在查詢的結果集的列與計算別名「RequestStatus_hods」。

我並不真正理解那些涉及CROSS JOINS的解決方案,所以我無法評估它們,但我估計在這些情況下性能並不是最好的。

我對這類問題有兩種不同的方法 1.-而不是在where子句中的「RequestStatus_hods」,使用完全相同的表達式,您正在使用該表達式來構建列中的值。它會工作,但如果結果集包含大量記錄,則性能將受到影響。 2:我包圍查詢如下

SELECT * FROM (<YOUR-QUERY>) AS 
RESULTSET WHERE RESULTSET.[RequestStatus_hods] = 'IN PROCESS' 

所以您的查詢變爲內,由臨時表(取決於誰的引擎解析查詢),但效果是,你必須要與表你想要的領域。這種方法的表現通常不是很糟糕。

希望這會有所幫助。

相關問題