-1

I don't know how to ask this correctly - it was probably asked many times. I have two tables. Companies and budgets.

Companies [id,name,address]
Budgets [id,company_id,year,budget]

I want to create a select where I would get the result for each company and budgets for each year in the same row.

budgets table:

id | company_id | year | budget  
1  | 1          | 2018 | 1500   
2  | 1          | 2019 | 2500  
3  | 2          | 2018 | 700  
4  | 2          | 2019 | 6000

So I would like to get the budgets in year columns

SELECT
a.id,
a.`name`,
IF(b.season_id = 1,b.budget,'') AS "budget 2018",
IF(b.season_id = 2,b.budget,'') AS "budget 2019"

FROM companies AS a
INNER JOIN budgets AS b ON a.id = b.company_id

This of course returns double rows. :)

company name | budget 2018 | budget 2019  
company one  | 1500        | 
company one  |             | 2500  
company two  | 700         | 
company two  |             | 6000

How can I get the budgets in the same row?

company name | budget 2018 | budget 2019  
company one  | 1500        | 2500  
company two  | 700         | 6000

Or how is this procedure called/described so I can ask uncle google? :)

Edit: I got suggestions for solving this task with outer joins. Truthfully I haven't had the time to solve the problem with outer joins - not yet confident if I can trust the result. The other thing is that I find the title of the suggested solution somewhat misleading. I ended solving this problem with the use of "fake" aggregates. Because this is a task I need once a year for a custom export I won't be pursuing a "better" solution. Thank you for your input.

2
  • 3
    Does this answer your question? SQL - Select rows from two different tables Commented Nov 15, 2019 at 19:52
  • Thank you for the link. I got it working with "fake" max agregate. In my case there are no empty rows. The problem was that the rows duplicated for every instance of budget year. I think there is a working solution with outer join but I haven't had the time for learning a new trick. Anyway thank you for your help. Commented Nov 19, 2019 at 13:02

3 Answers 3

1

With conditional aggregation:

SELECT
  c.id,
  c.name,
  MAX(CASE WHEN b.season_id = 1 THEN b.budget END) AS `budget 2018`,
  MAX(CASE WHEN b.season_id = 2 THEN b.budget END) AS `budget 2019`
FROM companies AS c INNER JOIN budgets AS b 
ON c.id = b.company_id
GROUP BY c.id, c.name 

I use MAX() because in your sample data there is only 1 row for each company/year.
If this is not the case and you want the total for each company/year then use SUM().

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

1 Comment

Hi I think your soltion is better than just (fake) SUM because I actually have multiple table with budgets (different budget types).
1

You could use a (fake) aggrgation function

    SELECT
    a.id,
    a.`name`,
    sum(IF(b.season_id = 1,b.budget,'')) AS `budget 2018`,
    sum(IF(b.season_id = 2,b.budget,'')) AS `budget 2019`
    FROM companies AS a
    INNER JOIN budgets AS b ON a.id = b.company_id
    GROUP BY a.id, a.name

4 Comments

You could use NULL instead of '' in the IF-clause to avoid unnecessary datatype conversions from numeric to char and back to numeric.
Hi I tried both and get the same correct results with both. This is used once a year for an export so optimisation is not key here. But thank you for the additional explanaton.
A ran across another problem. I actually have multiple budget types in additional tables so if I add another budget table the values get doubled where there is another budget in the second budget table. So this solution is ok for one "budgets" table.
anyway each aggreagation function return the same result ....sum()... max() ...min().... ... the concept is the same
0

probably this is what you wanted to know.

SQL - Select rows from two different tables

use outer join

INNER JOIN budgets AS b ON a.id = b.company_id

to

outer join budgets AS b ON a.id = b.comapny_id

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.