SQL QUERY - how to get child records with different types but from the same parent group

Andrei Brayer

I needed a query where to return the finished type records from the service_flow_actions table, but the records must belong to the service_flow table group where there are records that were given play

  1. Table work_order
id company_id closed status
1 1 true true
2 1 true true
  1. Table service_flow
id work_order_id company_id
1 1 1
2 2 1
3 2 1
  1. Table service_flow_actions
id work_order_id service_flow_id type company_id
1 1 1 finished 1
2 1 2 play 1
3 2 2 finished 1
4 2 3 play 1
5 2 3 pause 1
6 2 3 finished 1

I had thought of something like that, but it didn't work.

SELECT DISTINCT(service_flow_actions.*)
FROM service_flow_actions
INNER JOIN work_order ON service_flow_actions.work_order_id = work_order.id
LEFT JOIN service_flow_actions t ON work_order.id = t.work_order_id AND t.type = 'play' AND work_order.company_id = 37
WHERE work_order.company_id = 37 AND service_flow_actions.type = 'finished' AND work_order.closed = true AND work_order.status = true 
amd

The exists clause helps in such situation, of course you can achieve the same result using the join clause, but I would go for simple solution first, unless I face performance issues.

SELECT *
FROM service_flow_actions
WHERE type = 'finished'
AND EXISTS (
    SELECT 1
    FROM service_flow_actions sub
    WHERE sub.service_flow_id = service_flow_actions.service_flow_id
    AND sub.type = 'play' 
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to extract different json elements from the same table in SQL query?

SQL query for finding records where members within each group are the same

SQL Query to select parent that has child records with different values in the same column

SQL select and group records with same words in different order

How to ignorre same successive records from group with SQL

SQL select parent and child from different tables

How to make SQL query to get parent child data in group

How to get the average from different records in SQL

How to get parent from child

SQL: Get Parent Tree Hierarchy from child

How to Order table records with parent followed by child using SQL Query

SQL Server - select and join parent-child records, pivot and group

SQL: How to group records, when they have different types

Recursive Parent/Child in same table query in SQL where parent is PK

How to get records from different tables and ignoring duplicated dates in SQL?

SQL Query to group records

How to get records from SQL

SQL Query to get different records from two tables

How to get Parent and Child Table data as a single query in Sql

SQL query to get all the data from different tables with same id

how to get not matching records from same tables in sql?

How to copy records from parent to child in same table using MS SQL CTE

How to get the first record from a group of records MS sql

How to get same types of content from different website?

SQL get attributes from parent row to child

Rails group_by query with parent & child records

How to get all records as parent records with child records chained together?

Query to get parent child

postgresql How to SELECT and UPDATE parent and child records from the same table?