Forum Discussion
Calculating Main Current Task column by concatenating 2 task names based on multiple conditions
Hello, I am trying to solve the problem if someone can help that would be appreciated. I am beginning my journey with SQL.
Desired Output - Main Current Task Column
Task ID column is in ASCENDING order, and each language as a group has multiple Parent tasks.
1st Condition
I am trying to calculate Main Current Task Column in such a way where ever Tsk Status is Delivered and Project_Deliverable_Not_Deliverable is 'Deliverable' between one Parent and other parent (i.e., Column - Parent_Find) within a same language group it concatenates the Task of Parent + Task from the row which as Delivered status from Tsk Status column (i.e., Task 3) and gives Main Current Task as Task 1 + Task 3
Likewise Task 4 + Task 9 for second Parent within same English language.
2nd if CONDITION understanding
Can be any langauge, but For example lets say for Czech, as a language group Tsk Status is delivered for Task B but Project_Deliverable_Not_Deliverable is 'Non-Deliverable' hence no concatenation with Parent task as earlier just Task B.
and Task D as a output within same Czech group as new parent is present, (order by ASC Tsk Id column) because Tsk Status is delivered, Project_Deliverable_Not_Deliverable is deliverable and Parent at a same time, hence only picking Task D.
I was thinking to develop a logic saying where Parent Find = Parent and Tsk Id < Tsk Id of 2nd Parent within same language group and write if else statements to pick the relative task from Task Type column and then concatenate with Task of the parent.
Issue is there can be multiple parent within a language group order by Tsk Id ASC
Any help with the logic is helpful. Thanks.
Job ID | Langauge | Task Type | Parent_Find | Project Id | Tsk Id | Tsk Status | Main Current Task | Project_Deliverable_Not_Deliverable |
12345 English | English | Task 1 | Parent | 12345 | 8206892 | Finished | Task 1 + Task 3 | Deliverable |
12345 English | English | Task2 | 12345 | 8206893 | Finished | Task 1 + Task 3 | Deliverable | |
12345 English | English | Task3 | 12345 | 8206894 | Delivered | Task 1 + Task 3 | Deliverable | |
12345 English | English | Task 4 | Parent | 12345 | 8206895 | Finished | Task 4 + Task 9 | Deliverable |
12345 English | English | Task 5 | 12345 | 8206896 | Finished | Task 4 + Task 9 | Deliverable | |
12345 English | English | Task 6 | 12345 | 8206897 | Finished | Task 4 + Task 9 | Deliverable | |
12345 English | English | Task 7 | 12345 | 8206898 | Finished | Task 4 + Task 9 | Deliverable | |
12345 English | English | Task 8 | 12345 | 8206899 | Finished | Task 4 + Task 9 | Deliverable | |
12345 English | English | Task 9 | 12345 | 8206900 | Delivered | Task 4 + Task 9 | Deliverable | |
12345 English | English | Task 10 | 12345 | 8206901 | Canceled | Task 4 + Task 9 | Deliverable | |
23412 Czech | Czech | Task A | Parent | 23412 | 8206902 | Canceled | Task B | Deliverable |
23412 Czech | Czech | Task B | 23412 | 8206903 | Delivered | Task B | Non-Deliverable | |
23412 Czech | Czech | Task C | 23412 | 8206904 | Canceled | Task B | Deliverable | |
23412 Czech | Czech | Task D | Parent | 23412 | 8206905 | Delivered | Task D | Deliverable |
23412 Czech | Czech | Task E | 23412 | 8206906 | Delivered | Task D | Deliverable | |
23412 Czech | Czech | Task F | Parent | 23412 | 8206907 | Finished | Task F + Task I | Deliverable |
23412 Czech | Czech | Task G | 23412 | 8206908 | Finished | Task F + Task I | Deliverable | |
23412 Czech | Czech | Task H | 23412 | 8206909 | Finished | Task F + Task I | Deliverable | |
23412 Czech | Czech | Task I | 23412 | 8206910 | Delivered | Task F + Task I | Deliverable |
- Francisco_nittCopper Contributor
Based on the provided description and sample data, it seems that you are looking for a logic to calculate the "Main Current Task" column based on certain conditions. Here's a possible approach to achieve that:
Create a temporary table or view with the necessary columns: Job ID, Language, Task Type, Parent_Find, Tsk Id, Tsk Status, and Project_Deliverable_Not_Deliverable.
Use the following SQL query to calculate the "Main Current Task" column:
SELECT
t1.`Job ID`,
t1.`Language`,
t1.`Task Type`,
t1.`Parent_Find`,
t1.`Tsk Id`,
t1.`Tsk Status`,
CONCAT(t1.`Task Type`,
CASE
WHEN t1.`Tsk Status` = 'Delivered' AND t1.`Project_Deliverable_Not_Deliverable` = 'Deliverable' THEN
(SELECT CONCAT(' + ', t2.`Task Type`)
FROM your_table_name t2
WHERE t2.`Language` = t1.`Language`
AND t2.`Parent_Find` = t1.`Parent_Find`
AND t2.`Tsk Status` = 'Delivered'
AND t2.`Project_Deliverable_Not_Deliverable` = 'Deliverable'
AND t2.`Tsk Id` > t1.`Tsk Id`
ORDER BY t2.`Tsk Id` ASC
LIMIT 1)
ELSE ''
END
) AS `Main Current Task`,
t1.`Project_Deliverable_Not_Deliverable`
FROM your_table_name t1
Replace your_table_name with the actual name of the table or view you created in step 1.
This query uses a subquery to find the next delivered task with deliverable project status within the same language group and parent. It concatenates the current task with the found task using the CONCAT function.
Note: Make sure to adjust the column names and table/view names in the query based on your actual schema.
Hope this will help you. - olafhelperBronze ContributorPlease post table design as DDL, some sample data as DML statement and the expected result.