How to delete rows using 2 different collumns as condition in a SQL QUERY?

jaymzleutz :

My table is something like this:

Name       Order      Goods       Date
Michael     1         Shoes          2019/04/05
Michael     2         Groceries      2019/05/28
Michael     3         Caps           2019/03/02
Lucas       4         Shoes          2019/02/30
Lucas       5         Caps           2019/03/31
Mary        6         Shoes          2018/04/22 
Mary        7         Shoes          2018/03/25
Mary        8         Groceries      2017/08/22
Mary        9         Caps           2019/01/01

How to define a Query so that I can delete rows obbeying the following conditions?

  1. First and Foremost I want to group everything by the Name collumn;

  2. "Shoes" is my reference. I need to check If any customer has bought "Shoes" and will keep any other Goods registered if and only if the buying date is earlier than the "Shoes" buying date of that customer (grouped by the Name collumn)(i.e if any good other than Shoes has been bought after the Shoes buying date, this any other good row will be deleted).

  3. I keep only the first buying date of Shoes to compare. Newer dates are deleted too. Only the first one (older one) is kept.

So, I will have a table like below:

Michael 1 Shoes      2019/04/05
Michael 3 Caps       2019/03/02
Lucas   4 Shoes      2019/02/30
Mary    7 Shoes      2018/03/25
Mary    8 Groceries  2017/08/22

Thank you

GMB :

You can join an aggregate query that computes the first date when each customer ordered shoes, and use that information to filter the rows to delete:

delete t
from mytable t
inner join (
    select name, min(date) min_date 
    from mytable 
    where goods = 'Shoes' 
    group by name
) t1 on t.name = t1.name and t.date > t1.min_date

Demo on DB Fiddle:

Name    | OrderID | Goods     | Date      
:------ | ------: | :-------- | :---------
Michael |       1 | Shoes     | 2019-04-05
Michael |       3 | Caps      | 2019-03-02
Lucas   |       4 | Shoes     | 2019-02-28
Mary    |       7 | Shoes     | 2018-03-25
Mary    |       8 | Groceries | 2017-08-22

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL, How to delete rows from related tables using a query?

How to delete duplicate rows in SQL Server with condition?

How to delete rows by a condition using sqlalmchemy and fastapi?

How to delete duplicates based on condition on 2 columns using SQL

Delete rows with symbol '?' at the end using sql query

SQL query to display rows of 2 different tables

Delete last rows using a condition

How to delete certain rows with fix date but variable time using SQL query?

how to delete the only the rows in postgres but not to drop table using pandas read_sql_query method?

How do delete rows where column matches any of set using SQL query?

How to delete rows if it does not satisfy a condition using pandas

SQL delete if query returns more than 2 rows

How to exclude rows from a SQL query with an "IF"-like condition

How to join two rows in sql using query

Delete rows from 2 tables using a single query

A sql query to create multiple rows in different tables using inserted id

How to optimize this sql query on + 2 million rows

How to delete rows from a table using an SQLAlchemy query without ORM?

How to delete rows from a table using an SQLAlchemy query without ORM?

How can I delete rows in a database using a query?

SQL Delete records from grouped rows with condition

Delete rows via PDO query using PHP arrays with NOT IN with row constructor expression and an IN condition

How to delete space inside the string under condition, using SQL?

How to delete all rows from a group, if a different column meets condition in pandas

Pandas merges 2 Collumns into one

How do I merge and delete duplicated rows in SQL using UPDATE?

How can I remove duplicates by searching on a rows for a range of collumns until the last row with text in VBA?

How to delete row in Pandas Dataframe using 2 colums as condition?

How to delete rows from dataframe based on condition