如何让 max() 函数只输出一行
How do I make max() function only out put a single row
Select MAX(end_contract-start_contract), job_description, employer_name
FROM contracts join jobs on contracts_job = jobs_id
join employers on contracts_client = employer_id
WHERE contracts_end is not NULL
GROUP BY jobs_id,jobs_desc,employer_name
该查询当前输出:
MAX(end_contract-start_contract) jobs_desc employer_name
-------------------------------- --------------- ------------------------------
153 Janitor Microsoft
80 Soldier Microsoft
119 UNDEFINED USPS
290 UNDEFINED Microsoft
89 Pilot USNC
119 Cook USNC
232 driver USNC
340 Soldier USMC
我希望输出为:
MAX(end_contract-start_contract) jobs_desc employer_name
-------------------------------- --------------- ------------------------------
340 Soldier USMC
这就是我使用 max() 的原因,但我不明白为什么它不只显示最大值。我对 SQL
很陌生
我 怀疑 聚合在您的代码中没有做一些有用的事情。如果你想要最长期限的合同,你可以 order by
和 fetch
:
select c.contract_end - c.contract_start as contract_days, j.desc, e.emp_name
from contract c
inner join job j on c.contract_job = j.job_id
inner join employer e on c.contract_client = e.emp_id
where c.contract_end is not null
order by contract_days desc
fetch first 1 row with ties
这可能更直观:
Select CONTRACT_END-CONTRACT_START, JOB_DESC, EMP_NAME
FROM CONTRACT join JOB on CONTRACT_JOB = JOB_ID
join EMPLOYER on CONTRACT_CLIENT = EMP_ID
WHERE CONTRACT_END is not NULL
AND CONTRACT_END-CONTRACT_START=(Select MAX(CONTRACT_END-CONTRACT_START) FROM CONTRACT)
Select MAX(end_contract-start_contract), job_description, employer_name
FROM contracts join jobs on contracts_job = jobs_id
join employers on contracts_client = employer_id
WHERE contracts_end is not NULL
GROUP BY jobs_id,jobs_desc,employer_name
该查询当前输出:
MAX(end_contract-start_contract) jobs_desc employer_name
-------------------------------- --------------- ------------------------------
153 Janitor Microsoft
80 Soldier Microsoft
119 UNDEFINED USPS
290 UNDEFINED Microsoft
89 Pilot USNC
119 Cook USNC
232 driver USNC
340 Soldier USMC
我希望输出为:
MAX(end_contract-start_contract) jobs_desc employer_name
-------------------------------- --------------- ------------------------------
340 Soldier USMC
这就是我使用 max() 的原因,但我不明白为什么它不只显示最大值。我对 SQL
很陌生我 怀疑 聚合在您的代码中没有做一些有用的事情。如果你想要最长期限的合同,你可以 order by
和 fetch
:
select c.contract_end - c.contract_start as contract_days, j.desc, e.emp_name
from contract c
inner join job j on c.contract_job = j.job_id
inner join employer e on c.contract_client = e.emp_id
where c.contract_end is not null
order by contract_days desc
fetch first 1 row with ties
这可能更直观:
Select CONTRACT_END-CONTRACT_START, JOB_DESC, EMP_NAME
FROM CONTRACT join JOB on CONTRACT_JOB = JOB_ID
join EMPLOYER on CONTRACT_CLIENT = EMP_ID
WHERE CONTRACT_END is not NULL
AND CONTRACT_END-CONTRACT_START=(Select MAX(CONTRACT_END-CONTRACT_START) FROM CONTRACT)