Search code examples
sqlpostgresqlaggregate-functions

Get aggregate data from SQL tables with a one to many lookup relationship without altering the table


I need summary information from a table of data where there are multiple IDs for each item that map onto unique real names available in a lookup table.

I have a working solution that relies on altering the data table with a new column: http://sqlfiddle.com/#!17/1f224/7

Is there a way to get unit sums grouped by real_name and region without altering the data table?

The structure of the data table (region_data) is:

item_code short_code region units
B2513-70 Brash East 18
C2692-59 Scope East 100
C2692-59 Scope North 94
A6152-94 Chunk South 70
C2692-59 Scope West 40
A4891-91 Topic East 65
... ... ... ...
... ... ... ...

item_code is the link to the lookup.

The lookup table (item_lookup) is:

item_code real_name
B2513-70 Oven
C2692-59 Oven
F6940-84 Music
A4891-91 Music
E6031-11 Music
B2007-23 Hotel
D6228-48 Hotel
F3679-48 Ladder
E3587-36 Ladder
A6152-94 Ladder

In this example, there are 10 unique item_codes that map onto 4 unique items (based on real_names).

My working example requires 3 steps:

Step 1: alter region_data to add a column for real_name

-- # add column to region_data
ALTER TABLE region_data
ADD COLUMN rn text;

Step 2: update region_data rn column with real_name from item_lookup

-- # add real_name as rn to region_data from item_lookup
update 
    region_data rd
set
    rn = lu.real_name
from
    item_lookup lu
where
    rd.item_code = lu.item_code;

Step 3: Create the desired summary output

-- # create the summary table by real_name, region
select 
    rn,
    region, 
    sum(units)
from 
    region_data
group by
    rollup(rn, region)

Again is there a way to make the link between item_codes and real_name in a query that does not rely on altering the region_data table?


Solution

  • Just as I posted, I came up with this which works:

    select 
        rd.region region,
        lu.real_name name,
        sum(units)
    from 
        region_data rd
    join
        item_lookup lu
    on 
        lu.item_code = rd.item_code
    group by
        rollup(name, region)
    order by
        region;
    

    I had a block on not having item_code in the select.