Hello I am trying to run the following query to select one of each host_id, but filter out the other rows if the host_id is the same. However, even when using distinct, I get all of the rows. Can someone help me understand why that is? How would I be able to change this so that only the host_id is unique? Thank you.
SELECT DISTINCT host_id hostid, host.description customer_name, data_template_data.data_source_path file, host.hostname host_name
FROM data_local
JOIN data_template_data ON data_local.id = data_template_data.local_data_id
LEFT JOIN host ON data_local.host_id = host.id
WHERE data_template_data.data_template_id = 41
AND host.disabled=''
AND data_template_data.data_source_path IS NOT NULL
You need aggregation or window functions. Possibly the simplest method is:
with h as (
<your query here>
)
select h.*
from (select h.*,
row_number() over (partition by host_id order by host(id) as seqnum
from h
) h
where seqnum = 1;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.