简体   繁体   中英

JOIN tables to get name of ID from other table

I have the following tables:

CREATE TABLE PaL (
    Event_Date DATE,
    Country CHAR(2),
    Category CHAR(255),
    Revenue INTEGER(255),
    Costs INTEGER(255)
);

INSERT INTO PaL 
(Event_Date, Country, Category, Revenue, Costs)
VALUES

("2017-01-31", "1", "1", "691.09816652375", "-173.071989376023"),
("2017-02-28", "1", "1", "8419.9977988914", "-7622.61265984317"),
("2017-03-31", "1", "1", "2018.80471444031", "-1498.76213884283"),
("2017-04-30", "1", "1", "8863.15663035884", "-7965.69268589649"),
("2017-05-31", "1", "1", "6838.4514829573", "-1088.70351845663"),
("2017-06-30", "1", "1", "2025.73421386331", "-483.454199185678"),
("2017-07-31", "1", "1", "5389.0163788639", "-2643.93624645182"),
("2017-08-31", "1", "1", "6238.85870250446", "-1985.9879371866"),
("2017-09-30", "1", "1", "2294.62451106469", "-1864.98271539745"),
("2017-10-31", "1", "1", "4141.2074159951", "-197.773961036073"),
("2017-11-30", "1", "1", "1456.17584217397", "-1018.54129047119"),
("2017-12-31", "1", "1", "3623.54984724091", "-745.715567286581"),

("2017-01-31", "1", "2", "5955.20947079185", "-4745.39564508682"),
("2017-02-28", "1", "2", "9555.29563511224", "-5729.82601329738"),
("2017-03-31", "1", "2", "5490.36170257556", "-925.286457266534"),
("2017-04-30", "1", "2", "7652.35548686073", "-7335.32532050594"),
("2017-05-31", "1", "2", "9102.38987703511", "-5724.92574170071"),
("2017-06-30", "1", "2", "1703.95901703023", "-1678.19547060803"),
("2017-07-31", "1", "2", "3679.60045104324", "-2095.94207835501"),
("2017-08-31", "1", "2", "6672.43210841331", "-3475.55411014914"),
("2017-09-30", "1", "2", "7718.7744220635", "-1252.75877307055"),
("2017-10-31", "1", "2", "6976.01564153854", "-509.991595559256"),
("2017-11-30", "1", "2", "4725.46976319905", "-2835.09460170927"),
("2017-12-31", "1", "2", "8390.84483147949", "-7476.83516162742"),


("2017-01-31", "2", "1", "939788.159047677", "-742666.846083707"),
("2017-02-28", "2", "1", "826418.514009279", "-702997.151099908"),
("2017-03-31", "2", "1", "775940.69563018", "-211238.971709086"),
("2017-04-30", "2", "1", "516829.583069596", "-407521.856789393"),
("2017-05-31", "2", "1", "635701.377748304", "-627829.016481388"),
("2017-06-30", "2", "1", "757852.95599751", "-740948.867522139"),
("2017-07-31", "2", "1", "154224.257732688", "-139805.456987081"),
("2017-08-31", "2", "1", "102035.465731255", "-100103.875992667"),
("2017-09-30", "2", "1", "880671.692714021", "-665324.083753931"),
("2017-10-31", "2", "1", "187868.653562564", "-105676.793254039"),
("2017-11-30", "2", "1", "994600.486892401", "-177382.899789215"),
("2017-12-31", "2", "1", "813824.90461202", "-132527.311010471"),

("2017-01-31", "2", "2", "661069.933966637", "-454778.427240679"),
("2017-02-28", "2", "2", "675942.334464692", "-254489.623313569"),
("2017-03-31", "2", "2", "473604.307973888", "-404226.047653847"),
("2017-04-30", "2", "2", "872018.822577053", "-348781.396359871"),
("2017-05-31", "2", "2", "718012.023481434", "-625306.312927362"),
("2017-06-30", "2", "2", "688487.679029354", "-584512.575888519"),
("2017-07-31", "2", "2", "690085.013711018", "-581753.771085971"),
("2017-08-31", "2", "2", "204473.88894161", "-172301.871771595"),
("2017-09-30", "2", "2", "516932.092423463", "-328002.737710081"),
("2017-10-31", "2", "2", "609355.245817292", "-323624.391366703"),
("2017-11-30", "2", "2", "313599.625504231", "-210253.020497022"),
("2017-12-31", "2", "2", "723573.681040319", "-107333.764977439");


CREATE TABLE Categories (
    ID CHAR(2),
    Name CHAR(255)
);

INSERT INTO Categories
(ID, Name)
VALUES

("1", "Apparel"),
("2", "Shoes");


CREATE TABLE Countries (
    ID CHAR(2),
    Name CHAR(255)
);

INSERT INTO Countries
(ID, Name)
VALUES

("1", "DE"),
("2", "US");

I use the following query to get data from the table PaL :

Select Country, Category, sum(Revenue) as Revenue, sum(Costs) as Costs
FROM PaL
WHERE Event_Date BETWEEN "2017-01-01" AND "2017-12-31"
GROUP BY Country, Category

You can also find the tables in the sql fiddle here .
All this works finde so far.


Now, I want to join the table Categories and Countries into my SQL query so instead of the ID it shows the Name in the results. Therefore, I changed it to:

Select a.Country, a.Category, sum(a.Revenue) as Revenue, sum(a.Costs) as Costs
FROM PaL a
JOIN Categories b ON a.PaL = b.Name
JOIN Countries c ON a.PaL = c.Name
WHERE Event_Date BETWEEN "2017-01-01" AND "2017-12-31"
GROUP BY Country, Category

However, this gives me back error: Unknown column 'a.PaL' in 'on clause' .

Do you know where there is a mistake in my code?

There is no Pal column in your table(s). You need to use the column name for JOIN , not the table name. See below:

Select a.Country, a.Category, sum(a.Revenue) as Revenue, sum(a.Costs) as Costs
FROM PaL a
JOIN Categories b ON a.Category = b.ID
JOIN Countries c ON a.Country = c.ID
WHERE Event_Date BETWEEN "2017-01-01" AND "2017-12-31"
GROUP BY a.Country, a.Category

You need to join with COUNTRY and CATEGORY columns of PaL table. As you said these column stores ID for COUNTRY and CATEGORY , you need to modify your join condition as below. Also in your SELECT clause, you need to take values from CATEGORY and COUNTRY tables.

Select b.Name, c.Name, sum(a.Revenue) as Revenue, sum(a.Costs) as Costs
FROM PaL a
JOIN Categories b ON a.category = b.id
JOIN Countries c ON a.country = c.id
WHERE Event_Date BETWEEN "2017-01-01" AND "2017-12-31"
GROUP BY Country, Category

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM