-2

Mainly, I want to order the ids in descending direction but also, how can I sort parent product/items (the ones with empty ean) first and their child rows below them?

Here's an example data from the database:

| id   | ean           | name                                                        |
| 1005 | 4064722314889 | Camera Protector Series 15T - Model: 15T - Qty: 2           |
| 1004 | 4064722314896 | Camera Protector Series 15T - Model: 15T Pro - Qty: 2       |
| 1003 |               | Camera Protector Series 15T                                 |
| 1002 | 4064722314162 | Smartwatch Case Series GT6 Pro - Color: Black - Size: 46 mm |
| 1001 |               | Smartwatch Case Series GT6 Pro                              |

and so on...

Now, when I query it, I want to group it by id (desc) but also make sure to prioritize the row with an empty ean. Here's the desired result

| id   | ean           | name                                                        |
| 1003 |               | Camera Protector Series 15T                                 |
| 1005 | 4064722314889 | Camera Protector Series 15T - Model: 15T - Qty: 2           |
| 1004 | 4064722314896 | Camera Protector Series 15T - Model: 15T Pro - Qty: 2       |
| 1001 |               | Smartwatch Case Series GT6 Pro                              |
| 1002 | 4064722314162 | Smartwatch Case Series GT6 Pro - Color: Black - Size: 46 mm |

and so on...

Thanks in advance!

6
  • I don't understand your desired result. You have two rows with empty ean, but only one of them is prioritized. Commented Oct 28 at 15:34
  • 1
    Why isn't the desired order 1003, 1001, 1005, 1004, 1002? Commented Oct 28 at 15:35
  • Is there some other column that indicates the parent-child relationship? Commented Oct 28 at 15:38
  • 1
    Here's a solution: dbfiddle.uk/bXhilvZV I can't post it as an answer since other users have desperately moved to close this question, as they do nearly all questions on Stack Overflow. This site as really lost its way. Commented Oct 28 at 15:57
  • @bill-karwin I agree, at least we should know who is voting and why ! Commented Oct 28 at 16:31

1 Answer 1

0

First you should get your parents and children together to have a common key to keep them together in the Order By close. In your sample data only column "name" (not a good choice for naming a column) could be used to match the children to the parent (it would be better to have some kind of common key).
Using your sample data - first fetch the parents in a cte and then join them to the children using complete parent's "name column" contained in the childrens' "name columns" (Inner Join ON() clause).
Having them together it is not to hard to do the ordering in a way you want - below is just one of the options to do it (using Case expression):

With
    parents AS
      ( Select * From tbl Where ean Is Null )
Select     t.*
From       tbl t
Inner Join parents p ON(Length(Replace(t.name, p.name, '')) = Length(t.name) - Length(p.name))
Order By   p.id Desc, Case When t.ean Is Null Then t.id + p.id Else t.id End Desc
id ean name
1003 null Camera Protector Series 15T
1005 4064722314889 Camera Protector Series 15T - Model: 15T - Qty: 2
1004 4064722314896 Camera Protector Series 15T - Model: 15T Pro - Qty: 2
1001 null Smartwatch Case Series GT6 Pro
1002 4064722314162 Smartwatch Case Series GT6 Pro - Color: Black - Size: 46 mm

fiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.