- 我想在
RequestStatus_hod
爲IN PROCESS
- 但查詢似乎顯示所有的數據通過忽略where子句
- 當你只顯示數據可以看到,我試圖重複
WHERE RequestStatus_hod = 'IN PROCESS'
但它只是顯示爲NULL
- 我試圖使用臨時列
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 --
結果:
它是** RequestStatus_hods **還是** RequestStatus_hod **,因爲您的臨時列說RequestStatus_hods,但您的where條件說RequestStatus_hod – CodingDefined 2014-12-04 04:39:32
@CodingDefined其實我想過濾臨時列'RequestStatus_hods'。 'RequestStatus_hod'是表'REQUISITION_DETAILS'的原始列 – 2014-12-04 04:40:54
在最後一次選擇的條件中添加條件'B.RequestStatus_hod ='IN PROCESS''。 – CodingDefined 2014-12-04 04:45:09