How to remove duplicate data in this query?

Asked

Viewed 756 times

3

Eai personal blz?

Next I’m using this query:

select 
    products.id as product_id, 
    offers.id as offer_id, 
    companies.id as company_id, 
    products.title, 
    (ST_Distance(companies.location, 'POINT(-48.030322 -15.839689)':: geography) / 1000)  as distance

from offers 

inner join products on offers.product_id = products.id 
inner join categories on products.category_id = categories.id 
inner join offer_company on offers.id = offer_company.offer_id 
inner join companies on offer_company.company_id = companies.id 

where 
    offers.start_at <= '2016-09-02 13:07:31' and
    offers.deadline >= '2016-09-02 13:07:31' and
    products.category_id in (2) and
    ST_DWithin(companies.location, ST_SetSRID(ST_Point(-48.030322,-15.839689), 4326), 30000) 
order by 1,2,5

and the result is coming like this:

inserir a descrição da imagem aqui

How do I limit only to the nearest company? This way:

inserir a descrição da imagem aqui

I need to somehow display the nearest offer, however it may be in one or more stores (company_id).

  • 1

    You want the first row of duplicate data?

  • yeah. but that’s right

3 answers

0

Use the ROW_NUMBER, grouped by the field that Voce wants to use to differentiate and ordered by the value you want to use to pick up the first one (in the example that Voce placed would be the smallest Distance) After that, do a SELECT on this last command, taking only those with ROW_NUMBER = 1

SELECT * FROM
(
SELECT ROW_NUMBER() OVER(PARTITION BY product_id ORDER BY distance) as ID, *resto do select*
) TABELA
 WHERE ID = 1

0


Using the clause ROW_NUMBER next to OVER you can do it this way:

SELECT * FROM 
(
select 
    products.id as product_id, 
    offers.id as offer_id, 
    companies.id as company_id, 
    products.title, 
    (ST_Distance(companies.location, 'POINT(-48.030322 -15.839689)':: geography) / 1000)  as distance,
    ROW_NUMBER() OVER(PARTITION BY products.id,offers.id,companies.id) as n_row
from offers 

inner join products on offers.product_id = products.id 
inner join categories on products.category_id = categories.id 
inner join offer_company on offers.id = offer_company.offer_id 
inner join companies on offer_company.company_id = companies.id 

where 
    offers.start_at <= '2016-09-02 13:07:31' and
    offers.deadline >= '2016-09-02 13:07:31' and
    products.category_id in (2) and
    ST_DWithin(companies.location, ST_SetSRID(ST_Point(-48.030322,-15.839689), 4326), 30000) 
order by 1,2,5
) t WHERE n_row = 1

ROW_NUMBER will count the number of rows returned, using in conjunction with the clause OVER with the parameter PARTITION BY it will count the lines from the informed fields.

  • I understood the logic and did not know this feature, I’m reading about it here, but in all lines the n_row is returning 1 yet.

  • What do you mean? Did you see that your query has become a subquery and only get the results of it where the line is equal to 1? Is it returning repeated?

  • Yes.. but if I run the subquery, it is returning exactly as before.

  • You can paste the result into a Pastebin or something?

  • So I think I confused you.. The images that are in the initial post are already from the results. It’s not the table structure. got it?

  • I understood, but I spoke of the result returned with my answer.

  • Your answer was right but I used it in the following way row_number() OVER(PARTITION BY offers.id) as n_row, so yes it worked

  • Well, at least you understand how it works haha (:

Show 3 more comments

0

See if it helps.

 select 
    products.id as product_id, 
    offers.id as offer_id, 
    companies.id as company_id, 
    products.title, 
    ST_Distance(companies.location, 'POINT(-48.030322 -15.839689)':: geography) / 1000)as distance

from offers 
inner join products on offers.product_id = products.id 
inner join categories on products.category_id = categories.id 
inner join offer_company on offers.id = offer_company.offer_id 
inner join companies on offer_company.company_id = companies.id
(select * from companies where offer_company.company_id = companies.id limit 1) as companies 
on offer_company.company_id = companies.id 
where 
    offers.start_at <= '2016-09-02 13:07:31' and
    offers.deadline >= '2016-09-02 13:07:31' and
    products.category_id in (2) and
    ST_DWithin(companies.location, ST_SetSRID(ST_Point(-48.030322,-15.839689), 4326), 30000) 
order by 1,2,5
  • Did you remove Join from the company?? I guess that’s it.. but I need him..

  • Truth didn’t see it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.