0

if someone could help to add some statement in this query that would eliminate duplicate if their status are both canceled. Highligted in the image below of the sample that i don't want to appear in my query results.

with cte0 AS 
(
    SELECT * FROM efleet_copy AS e1
    INNER JOIN
    (SELECT mta_id, COUNT(mta_id) FROM efleet_copy WHERE EXTRACT(MONTH FROM service_date) = 02 AND mta_id LIKE '_________'
     GROUP BY mta_id HAVING COUNT(mta_id) > 1) AS e2
    ON e1.mta_id = e2.mta_id
    WHERE EXTRACT(MONTH FROM service_date) = 02
    ORDER BY e1.mta_id ASC, e1.ride_id ASC, e1.trip_number ASC
)
SELECT * FROM cte0 c
WHERE NOT EXISTS (
    SELECT 1 FROM cte0 ci
    WHERE c.ride_id = ci.ride_id
    AND status = 'Approved'
    GROUP BY ride_id 
    HAVING SUM(trip_total_amount) = 0::money
)

sample data

4
  • 1
    Eliminate both or eliminate dups (keeping one)? Also, how to know if it is dup? Can you provide a mcve ? (it has no sense to force people to work with your data, make some effort to generalize your question for all future users ) Feb 21, 2019 at 14:39
  • They are duplicates when they have exactly the same data except trip_total_amount. Like what is highlighted in the image both transaction is canceled so i don't need them in my results but i don't want to completely eliminate all canceled because sometimes there is some duplicates which is a canceled and approved.
    – jpabiado
    Feb 21, 2019 at 14:49
  • Can you post the simplest sample. With minimum columns but enough to extrapolate a possible answer to your scenario? With an easy semantic for everyone, like order number, order status, ... Feb 21, 2019 at 14:52
  • It would help to have sample data and results for the scenarios you mentioned @jpabiado Feb 21, 2019 at 15:00

3 Answers 3

1

You could try the DISTINCT clause, so your query looks like this:

SELECT DISTINCT
    rate, 
    status,
    ride_id,
    trip_number,
    mta_id,
    service_date,
    service_start,
    driver_id,
    cab_number,
    trip_total_amount
FROM cte0 c
WHERE NOT EXISTS (
    SELECT 1 FROM cte0 ci
    WHERE c.ride_id = ci.ride_id
    AND status = 'Approved'
    GROUP BY ride_id 
    HAVING SUM(trip_total_amount) = 0::money
);
2
  • I tried this also but what it does is they just stacked all the canceled transactions at the lower portion of the query results. It helps somehow but i need something more solid. Thanks anyway.
    – jpabiado
    Feb 21, 2019 at 15:26
  • What do you mean stack all cancelled transactions at the lower portion? Are the records you are expecting are there? If the issue is with where the records are placed, then you can use ORDER BY. Feb 21, 2019 at 15:28
0

The DISTINCT clause allows you to remove the duplicate rows.

try SELECT DISTINCT

3
  • But they are not exactly duplicates, their are some field of data which they have difference with each other like for example in the image 'trip_total_amount'
    – jpabiado
    Feb 21, 2019 at 15:02
  • try (regexp_replace(trip_total_amount, '\D','','g'), '')::numeric AS result to remove non-numeric characters from 'trip_total_amount' @jpabiado
    – Azi_bel
    Feb 21, 2019 at 15:30
  • \D being the class shorthand for "not a digit". And you need the 4th parameter 'g' (for "globally") to replace all occur
    – Azi_bel
    Feb 21, 2019 at 15:33
0

I think you can do:

SELECT c.*
FROM cte0 c
WHERE NOT EXISTS (SELECT 1
                  FROM cte0 ci
                  WHERE c.ride_id = ci.ride_id AND
                        status = 'Approved'
                  GROUP BY ride_id 
                  HAVING SUM(trip_total_amount) = 0::money
                 ) OR
      (c.status = 'Canceled' AND
       EXISTS (SELECT 1
               FROM cte0 ci
               WHERE c.ride_id = ci.ride_id AND
                     ci.status = 'Canceled' AND
                     ci.trip_total_amount = - c.trip_total_amount
              )
      )

The problem is a little bit unclear. I would approach all the logic using window functions, but you have already started with exists subqueries, so this follows in that spirit.

4
  • thanks for your inputs but when i tried this it runs but the query results still shows those canceled duplicate transactions
    – jpabiado
    Feb 21, 2019 at 15:18
  • @jpabiado . . . Did you spell "Canceled" correctly? Feb 21, 2019 at 21:24
  • the data i am using uses that word that's why i use it in my queries
    – jpabiado
    Feb 21, 2019 at 21:48
  • @jpabiado . . . My original answer spelled it incorrectly. Feb 21, 2019 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.