K
K
Karina2018-05-11 10:38:03
SQL
Karina, 2018-05-11 10:38:03

How to get rid of duplicate records when join?

There is an order table.
There is a timeline (there is fk - order_id).

SELECT o.id,
           o.status,
           order_payment_at,
           order_cancel_at,
           ....
          FROM orders AS o
          INNER JOIN partners p ON p.id = o.partner_id
          ....
          INNER JOIN(
            SELECT
              DISTINCT order_id, 
              CASE WHEN data LIKE '%{"status":"paid"}%' THEN created_at END AS order_payment_at,
              CASE WHEN data LIKE '%{"status":"cancel"}%' THEN created_at END AS order_cancel_at
              ....
              FROM timeline
          ) timeline ON timeline.order_id = o.id
         GROUP BY o.id and long list.......

a problem in what - to order concerns dofigishcha timeline.
and the query outputs to me:
id (meaning order)
1 which refers to timeline
1 which refers to timeline
1 which refers to timeline
the query displays everything correctly, but it is necessary that ALL data from the timeline be in ONE line.
I understand that I need GROUP BY order_id, but postgres does not want to be limited to only order_id - it also wants to add
both data and created_at
Thank you all for your answers

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
d-stream, 2018-05-11
@d-stream

somehow use distinct or group by

M
MAGratsk, 2018-05-13
@MAGratsk

the request correctly displays everything, but it is necessary that ALL data from the timeline be in ONE line.

So is it correct or not? It's not entirely clear what you want to get as an output.
So that there is no more than one timeline for each order? Or that for each order data is displayed for all timelines, but in some kind of aggregated form? If so, the question is in what form.
One way or another, when after the join you need to discard duplicates and leave only one resulting row per source row, the construction is great:
Dedup = ROW_NUMBER() OVER (PARTITION BY o.id ORDER BY ...).
The next step - by subrequest or via CTE - sets the filter to Dedup = 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question