我需要有一个查询来选择KitchenBlinkSound='Y'
加top 1
KitchenBlinkSound='N'
的所有记录,这是我的表结构。
我要求的结果将是OrderNo 225,226,227
。基本上它包含所有KitchenBlinkSound='Y'
条记录以及top 1
条记录的KitchenBlinkSound='N'
。
答案 0 :(得分:0)
SELECT * FROM TABLE WHERE KitchenBlinkSound='Y'
UNION ALL
SELECT TOP 1 * FROM TABLE WHERE KitchenBlinkSound='N'
ORDER BY ORDERNO
<强>更新强>
SELECT TOP 3 * FROM
(
SELECT * FROM #TABLE1 WHERE KitchenBlinkSound='Y'
UNION ALL
SELECT ORDERNO,KITCHENSTATUS,KitchenBlinkSound FROM
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY KitchenBlinkSound ORDER BY ORDERNO ASC) RNO
FROM #TABLE1 WHERE KitchenBlinkSound='N'
)TAB
WHERE TAB.RNO=1
)MAIN
ORDER BY
,因此会添加ROW_NUMBER(),以确保您按升序选择TOP 1
ORDERNO
。如果您想按降序将ASC
更改为DESC
。答案 1 :(得分:0)
Select * from [your table name] where KitchenBlickSound = 'Y'
UNION
Select TOP 1 * from [your table name] where KitchenBlickSound = 'N'