SQL Group by main category in self-referenced table

PWL

I need to get a list of total sales grouped by the main category and Seller. Note that there can be sales on the main category (this is the best example I can think of at the moment).

Source table

+--------------------------------------+
|ID   |Name        |Seller|Qty|ParentID|
+--------------------------------------+
|10   |Egg         |John  |5  |NULL    |
|10   |Egg         |Anna  |2  |NULL    |
|10-01|Egg - Small |John  |3  |10      |
|10-01|Egg - Small |Anna  |4  |10      |
|10-02|Egg - Medium|John  |2  |10      |
|10-02|Egg - Medium|Bob   |11 |10      |
|10-03|Egg - Large |Anna  |7  |10      |
+--------------------------------------+

Desired output

+------------------+
|ID|Name|Seller|Qty|
+------------------+
|10|Egg |John  |10 | <- SUM of all sales John has made for any type of egg
|10|Egg |Anna  |13 |
|10|Egg |Bob   |11 |
+------------------+

I'm getting close with this query, but if someone has not made a sale on the main category, they will get the wrong Name when I use MIN(Name).

Current query

SELECT 
    SUBSTRING(t1.ID, 1, 2) AS 'ID',
    MIN(t1.Name) AS 'Name', 
    t1.Seller, 
    SUM(t1.Qty) AS 'Qty'
FROM EggTest t1
GROUP BY 
    SUBSTRING(t1.ID, 1, 2),
    t1.Seller

Current output

+--------------------------+
|ID|Name        |Seller|Qty|
+--------------------------+
|10|Egg         |Anna  |13 |
|10|Egg - Medium|Bob   |11 | <- Bob has not made sales on the main category
|10|Egg         |John  |10 |
+--------------------------+

EDIT: Seeing that multiple answers have already suggested SUBSTRING(Name, 1, 3) it will not work for me. Name does not always start with "Egg".

Update:

Now trying this query:

WITH report AS(
  SELECT 
    ID = CASE WHEN s.ParentID IS NOT NULL THEN s.ParentID ELSE s.ID END,
    Name = CASE WHEN s.ParentID IS NOT NULL THEN p.Name ELSE s.Name END,
    s.Seller,
    s.Qty
  FROM EggTest s
  LEFT JOIN EggTest p ON p.ID = s.ParentID
)

SELECT ID, Name, Seller, SUM(Qty) AS 'Total'
FROM report
GROUP BY ID, Name, Seller;

But I am getting this strange result:

+--------------------+
|ID|Name|Seller|Total|
+--------------------+
|10|Egg |Anna  |24   | <- Wrong (Should be 13)
|10|Egg |Bob   |22   | <- Wrong (Should be 11)
|10|Egg |John  |15   | <- Correct(!!)
+--------------------+

In the report-table I'm getting some duplicates:

+------------------+
|ID|Name|Seller|Qty|
+------------------+
|10|Egg |John  |5  |
|10|Egg |Anna  |2  |
|10|Egg |John  |3  |
|10|Egg |John  |3  |
|10|Egg |Anna  |4  |
|10|Egg |Anna  |4  |
|10|Egg |John  |2  |
|10|Egg |John  |2  |
|10|Egg |Anna  |7  |
|10|Egg |Anna  |7  |
|10|Egg |Bob   |11 |
|10|Egg |Bob   |11 |
+------------------+
Monah

I will consider the source table name as [Sales]

You can use the following

with report as(
   select ID = case when s.ParentID is not null then s.ParentID else s.ID end,
          Name= case when s.ParentID is not null then p.Name else s.Name end,
          s.Seller,
          s.Qty
   from Sales s
   left join Sales p on p.ID = s.ParentID and p.Seller = s.Seller
)
select ID,Name,Seller,sum(Qty) as Qty
from report
group by ID,Name,Seller

Here a demo using Distinct

Here a demo by including the Seller in the left join , which will give you Name of the item for Seller Bob as NULL, the left join should work if you have correct data integrity which means separate table for the Items and Categories

replying on your last comment, here a demo how to make your data clear

Hope this will help you

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to return the parents of a child in a self referenced table?

From Dev

How to duplicate rows of self-referenced table

From Dev

How to return the parents of a child in a self referenced table?

From Dev

Combine rows from self-referenced table

From Dev

Self referenced MySql table with conditions on parent and child

From Dev

TSQL/SQL Server 2008 R2 - Recursive select consolidating self-referenced table Unit and apply SUM on UnitSale and UnitCharge

From Dev

TSQL/SQL Server 2008 R2 - Recursive select consolidating self-referenced table Unit and apply SUM on UnitSale and UnitCharge

From Dev

How to get all child of each records of a self-referenced table

From Dev

display table group by category using php

From Dev

Select Data from multiple table and Group by Category

From Dev

Finding a referenced table in multiple views using SQL

From Dev

SQL joins, "there is an entry for table but it cannot be referenced"

From Dev

SQL Server : there are no primary or candidate keys in the referenced table

From Dev

SQL query to get value from referenced table

From Dev

Finding SQL records referenced by another table

From Dev

SQL Sum by Category and group by month/year

From Dev

group by category to show difference by month in SQL

From Dev

How to get last category of a self-join table

From Dev

Self Referencing Table SQL query

From Dev

SQL group table values

From Dev

SQL CASE, when column category = x group by category else don't group

From Dev

How to update foreign key referenced table using trigger, in SQL Server?

From Dev

SQL trying to group by ID and keep variable based on category

From Dev

How to get category count using group by in sql server?

From Dev

return join table but group by only main table id

From Dev

Self-join a table in SQL Server 2008

From Dev

Self referencing table SQL Query to rows

From Dev

SQL join table with it self given partition

From Dev

SQL calculate percentage using table self join

Related Related

  1. 1

    How to return the parents of a child in a self referenced table?

  2. 2

    How to duplicate rows of self-referenced table

  3. 3

    How to return the parents of a child in a self referenced table?

  4. 4

    Combine rows from self-referenced table

  5. 5

    Self referenced MySql table with conditions on parent and child

  6. 6

    TSQL/SQL Server 2008 R2 - Recursive select consolidating self-referenced table Unit and apply SUM on UnitSale and UnitCharge

  7. 7

    TSQL/SQL Server 2008 R2 - Recursive select consolidating self-referenced table Unit and apply SUM on UnitSale and UnitCharge

  8. 8

    How to get all child of each records of a self-referenced table

  9. 9

    display table group by category using php

  10. 10

    Select Data from multiple table and Group by Category

  11. 11

    Finding a referenced table in multiple views using SQL

  12. 12

    SQL joins, "there is an entry for table but it cannot be referenced"

  13. 13

    SQL Server : there are no primary or candidate keys in the referenced table

  14. 14

    SQL query to get value from referenced table

  15. 15

    Finding SQL records referenced by another table

  16. 16

    SQL Sum by Category and group by month/year

  17. 17

    group by category to show difference by month in SQL

  18. 18

    How to get last category of a self-join table

  19. 19

    Self Referencing Table SQL query

  20. 20

    SQL group table values

  21. 21

    SQL CASE, when column category = x group by category else don't group

  22. 22

    How to update foreign key referenced table using trigger, in SQL Server?

  23. 23

    SQL trying to group by ID and keep variable based on category

  24. 24

    How to get category count using group by in sql server?

  25. 25

    return join table but group by only main table id

  26. 26

    Self-join a table in SQL Server 2008

  27. 27

    Self referencing table SQL Query to rows

  28. 28

    SQL join table with it self given partition

  29. 29

    SQL calculate percentage using table self join

HotTag

Archive