Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

group by for summary data

Tags:

sql

sql-server

Trying to do something here to create a kind of summary data. I'm not sure it will be the most elegant of sql code!

I have the following table

Product             Channel         Sold
-------------------     ----------------------  
PC                  Web             48
Laptop              Web             2
Speakers            Web             74
DVDs                Web             33
PC                  Shop            1
Laptop              Shop            1
Speakers            Shop            1
DVDs                Shop            5
PC                  Door-to-door    7
Laptop              Door-to-door    16
Speakers            Door-to-door    9
DVDs                Door-to-door    21
PC                  Referals        7
Laptop              Referals        16
Speakers            Referals        9
DVDs                Referals        21

I would like to query the data so I get something to represent "direct" sales which is a total of Web and Shop Sales and thus ignoring door-to-door and referals.

Product             Channel         Sold
-------------------     ----------------------  
PC              Direct          49
Laptop          Direct          3
Speakers        Direct          75
DVDs            Direct          38

Does anyone know how I might do this? I was thinking about Group by (select .... group by) but everything I'm trying is a desperate failure! lol.

Thanks in advance.

DS

EDIT!

What if I wanted to put door-to-door and referals together as 'secondary'? Is that easily acheived? So I'm looking for...

Product             Channel         Sold
-------------------     ----------------------  
PC              Direct          49
Laptop          Direct          3
Speakers        Direct          75
DVDs            Direct          38
PC              Secondary       14
Laptop          Secondary       32
Speakers        Secondary       18
DVDs            Secondary       42

Thanks again!

DS

like image 390
dstewart101 Avatar asked Jun 03 '13 14:06

dstewart101


1 Answers

You just need to filter out the records by channel and aggregate them using SUM() on column Sold for each group specifically the product.

SELECT  Product,    
        'Direct' Channel,
        SUM(Sold) TOtalSold
FROM    TableName
WHERE   Channel IN ('Web','Shop')
GROUP   BY Product
  • SQLFiddle Demo

UPDATE

SELECT  Product,    
        CASE    WHEN Channel IN ('Web','Shop') 
                THEN 'Direct'
                ELSE 'Secondary'
        END Channel,
        SUM(Sold) TOtalSold
FROM    TableName
GROUP   BY Product,
        CASE    WHEN Channel IN ('Web','Shop') 
                THEN 'Direct'
                ELSE 'Secondary'
        END 
ORDER   BY Channel
  • SQLFiddle Demo

OUTPUT

╔══════════╦═══════════╦═══════════╗
║ PRODUCT  ║  CHANNEL  ║ TOTALSOLD ║
╠══════════╬═══════════╬═══════════╣
║ Laptop   ║ Direct    ║         3 ║
║ Speakers ║ Direct    ║        75 ║
║ DVDs     ║ Direct    ║        38 ║
║ PC       ║ Direct    ║        49 ║
║ Laptop   ║ Secondary ║        32 ║
║ Speakers ║ Secondary ║        18 ║
║ DVDs     ║ Secondary ║        42 ║
║ PC       ║ Secondary ║        14 ║
╚══════════╩═══════════╩═══════════╝
like image 74
John Woo Avatar answered Sep 28 '22 22:09

John Woo