我目前正在尝试在select语句中使用select语句。到目前为止,这很有效。但我希望它根据某些条件选择不同的东西。我的问题是将SQL语句子查询放在CASE语句的THEN部分而不是WHEN部分。所以我的例子是here。所以我想在CASE语句中放置一个select语句,该语句全部包含在子查询中。我也在使用SQL Server 2008。
以下是我原来的内容:
SELECT DISTINCT
WORK_ORDER.PART_ID,
OPERATION.SEQUENCE_NO,
OPERATION.RESOURCE_ID,
OPERATION.SETUP_HRS,
OPERATION.RUN_HRS,
OPERATION.OPERATION_TYPE,
OPERATION.RUN AS PCS_HR,
REPLACE(CONVERT( VARCHAR(150), CONVERT(BINARY(150), OPERATION_BINARY.BITS)), CHAR(0), '') AS Specs_OPR,
OPERATION.WORKORDER_BASE_ID,
OPERATION.WORKORDER_LOT_ID,
OPERATION.WORKORDER_SPLIT_ID,
OPERATION.WORKORDER_SUB_ID,
OPERATION.LOAD_SIZE_QTY,
OPERATION.CALC_START_QTY,
OPERATION.COMPLETED_QTY,
(CEILING(OPERATION.CALC_START_QTY / OPERATION.LOAD_SIZE_QTY)) AS NUM_O_LOADS,
((OPERATION.CALC_START_QTY - OPERATION.COMPLETED_QTY) / OPERATION.LOAD_SIZE_QTY) AS LOADS_REMAINING,
-- SubQuery
(
SELECT DISTINCT
descriptions = STUFF(
(
SELECT ', '+CHAR(13)+PART.DESCRIPTION
FROM dbo.PART
JOIN REQUIREMENT ON PART.ID = REQUIREMENT.PART_ID
WHERE REQUIREMENT.WORKORDER_BASE_ID = '026877'
AND REQUIREMENT.OPERATION_SEQ_NO = '30'
FOR XML PATH(''), TYPE
).value('.[1]', 'nvarchar(max)'), 1, 2, '')
FROM(REQUIREMENT R
JOIN PART P ON R.PART_ID = P.ID)
) AS PART_DESCRIPTION
-- End of SubQuery
FROM OPERATION
INNER JOIN WORK_ORDER ON OPERATION.WORKORDER_TYPE = WORK_ORDER.TYPE
AND OPERATION.WORKORDER_BASE_ID = WORK_ORDER.BASE_ID
AND OPERATION.WORKORDER_LOT_ID = WORK_ORDER.LOT_ID
AND OPERATION.WORKORDER_SPLIT_ID = WORK_ORDER.SPLIT_ID
AND OPERATION.WORKORDER_SUB_ID = WORK_ORDER.SUB_ID
INNER JOIN OPERATION_BINARY ON OPERATION.WORKORDER_TYPE = OPERATION_BINARY.WORKORDER_TYPE
AND OPERATION.WORKORDER_BASE_ID = OPERATION_BINARY.WORKORDER_BASE_ID
AND OPERATION.WORKORDER_LOT_ID = OPERATION_BINARY.WORKORDER_LOT_ID
AND OPERATION.WORKORDER_SPLIT_ID = OPERATION_BINARY.WORKORDER_SPLIT_ID
AND OPERATION.WORKORDER_SUB_ID = OPERATION_BINARY.WORKORDER_SUB_ID
AND OPERATION.SEQUENCE_NO = OPERATION_BINARY.SEQUENCE_NO
JOIN REQUIREMENT ON REQUIREMENT.WORKORDER_BASE_ID = OPERATION.WORKORDER_BASE_ID
WHERE(REPLACE(CONVERT(VARCHAR(8000), CONVERT(BINARY(8000), OPERATION_BINARY.BITS)), CHAR(0), '') NOT LIKE '%Gupta%')
AND OPERATION.WORKORDER_BASE_ID = '026877'
AND WORK_ORDER.BASE_ID = '026877';
这就是我现在所拥有的但是我的Case在我的3行结果中创建了第4行的一些原因:
SELECT DISTINCT
WORK_ORDER.PART_ID,
OPERATION.SEQUENCE_NO,
OPERATION.RESOURCE_ID,
OPERATION.SETUP_HRS,
OPERATION.RUN_HRS,
OPERATION.OPERATION_TYPE,
OPERATION.RUN AS PCS_HR,
REPLACE(CONVERT( VARCHAR(150), CONVERT(BINARY(150), OPERATION_BINARY.BITS)), CHAR(0), '') AS Specs_OPR,
OPERATION.WORKORDER_BASE_ID,
OPERATION.WORKORDER_LOT_ID,
OPERATION.WORKORDER_SPLIT_ID,
OPERATION.WORKORDER_SUB_ID,
OPERATION.LOAD_SIZE_QTY,
OPERATION.CALC_START_QTY,
OPERATION.COMPLETED_QTY,
(CEILING(OPERATION.CALC_START_QTY / OPERATION.LOAD_SIZE_QTY)) AS NUM_O_LOADS,
((OPERATION.CALC_START_QTY - OPERATION.COMPLETED_QTY) / OPERATION.LOAD_SIZE_QTY) AS LOADS_REMAINING,
-- SubQuery Inside Case
CASE
WHEN REQUIREMENT.OPERATION_SEQ_NO = '10'
AND OPERATION.SEQUENCE_NO = '10'
THEN
(
SELECT DISTINCT
descriptions = STUFF(
(
SELECT ', '+CHAR(13)+PART.DESCRIPTION
FROM dbo.PART
JOIN REQUIREMENT ON PART.ID = REQUIREMENT.PART_ID
WHERE REQUIREMENT.WORKORDER_BASE_ID = '026877'
AND REQUIREMENT.OPERATION_SEQ_NO = '10'
FOR XML PATH(''), TYPE
).value('.[1]', 'nvarchar(max)'), 1, 2, '')
FROM(REQUIREMENT R
JOIN PART P ON R.PART_ID = P.ID)
)
END AS DESCRIPTION
-- End of Subquery
FROM OPERATION
INNER JOIN WORK_ORDER ON OPERATION.WORKORDER_TYPE = WORK_ORDER.TYPE
AND OPERATION.WORKORDER_BASE_ID = WORK_ORDER.BASE_ID
AND OPERATION.WORKORDER_LOT_ID = WORK_ORDER.LOT_ID
AND OPERATION.WORKORDER_SPLIT_ID = WORK_ORDER.SPLIT_ID
AND OPERATION.WORKORDER_SUB_ID = WORK_ORDER.SUB_ID
INNER JOIN OPERATION_BINARY ON OPERATION.WORKORDER_TYPE = OPERATION_BINARY.WORKORDER_TYPE
AND OPERATION.WORKORDER_BASE_ID = OPERATION_BINARY.WORKORDER_BASE_ID
AND OPERATION.WORKORDER_LOT_ID = OPERATION_BINARY.WORKORDER_LOT_ID
AND OPERATION.WORKORDER_SPLIT_ID = OPERATION_BINARY.WORKORDER_SPLIT_ID
AND OPERATION.WORKORDER_SUB_ID = OPERATION_BINARY.WORKORDER_SUB_ID
AND OPERATION.SEQUENCE_NO = OPERATION_BINARY.SEQUENCE_NO
JOIN REQUIREMENT ON REQUIREMENT.WORKORDER_BASE_ID = OPERATION.WORKORDER_BASE_ID
WHERE(REPLACE(CONVERT(VARCHAR(8000), CONVERT(BINARY(8000), OPERATION_BINARY.BITS)), CHAR(0), '') NOT LIKE '%Gupta%')
AND OPERATION.WORKORDER_BASE_ID = '026877'
AND WORK_ORDER.BASE_ID = '026877';
答案 0 :(得分:1)
我找到了解决我特定问题的方法。它只是我的where子句导致添加行。
但是要回答最初的问题,你可以在CASE中放置一个子查询选择语句。是的,你可以做到。
CASE
WHEN condition
THEN (select column1, from tbl join tbl2 on tbl1.column = tbl2.column)
ELSE
(whatever)
END