这是我到目前为止为我的查询编写的代码:
select
workorderkey,
WarrantyRoutingStep,
StepOrder
from tblFAWOWarrantyRoutingsOEM WarrantyRoutings
where
workorderkey=220870 and --fyi i do not want to do this
StepCompleteDate IS NULL
结果就是这个
如何让它看起来像这样,所以它只返回StepOrder列中具有最小值的行)?请注意,我不仅仅想要workorderkey 220870的结果;我希望它能够为每个workorderkey分组并返回一个结果 - 只需输入该过滤器进行测试。
请原谅图片而不是桌面结果的文字。第一次海报,长达一个月的观众。几周前刚刚开始编写SQL。
答案 0 :(得分:1)
如果您的dbms支持窗口函数,那么很容易:
SELECT *
FROM
(
select
workorderkey,
WarrantyRoutingStep,
StepOrder,
ROW_NUMBER() OVER (PARTITION BY workorderkey ORDER BY StepOrder) as RowNumber
from tblFAWOWarrantyRoutingsOEM WarrantyRoutings
where StepCompleteDate IS NULL
) t
WHERE
t.RowNumber = 1
没有窗口功能,你仍然可以:
SELECT wr2.*
FROM
(
select
workorderkey,
MIN(StepOrder) as MinStepOrder
from tblFAWOWarrantyRoutingsOEM WarrantyRoutings
where StepCompleteDate IS NULL
GROUP BY
workorderkey
) t
INNER JOIN tblFAWOWarrantyRoutingsOEM wr2
On t.workorderkey = wr2.workorderkey
AND StepCompleteDate IS NULL
但如果步骤顺序不是每个工单的唯一,那么后一种方法仍然可以结束。
答案 1 :(得分:-2)
select
min(StepOrder),
workorderkey,
WarrantyRoutingStep
from tblFAWOWarrantyRoutingsOEM WarrantyRoutings
where StepCompleteDate IS NULL
GROUP BY workorderkey, WarrantyRoutingStep
这适用于你