SQL JOIN with 2 tables because data not complet - mysql

i need help with one Statement. I have 2 tables. In one table are my data, but not completed in month 1-12. In other table I have month 1-12. I dont know how to get all my data with all month. and if there are no data for this month he should set 0.
Table1:
Table2:
I tried to use this statment but it dont work...
SELECT table2.produkt, table2.monat
(CASE WHEN table2.wert IS NULL THEN table1.wert ELSE 0 END)
FROM table2 JOIN
table1
ON table1.produkt = table2.produkt

I think you just need left join and coalesce():
select t2.produkt, t2.monat, coalesce(t1.wert, 0) as wert
from table2 t2 left join
table1 t1
on t2.produkt = t1.produkt and t2.monat = t1.monat

You should use left join
SELECT table2.produkt, table2.monat
(CASE WHEN table2.wert IS NULL THEN auftrag.wert ELSE 0 END)
FROM table2 Left JOIN
table1
ON table1.produkt = table2.produkt

Use a left join as opposed to a join. That will return all the items in the left most table (table2) and only those that are linked from the other table. That is assuming that your base table is table2
SELECT table2.produkt, table2.monat
(CASE WHEN table2.wert IS NULL THEN auftrag.wert ELSE 0 END)
FROM table2 LEFT JOIN
table1
ON table1.produkt = table2.produkt

For your Query you can use a Left Join instead of join
SELECT table2.produkt, table2.monat
(CASE WHEN table2.wert IS NULL THEN auftrag.wert ELSE 0 END)
FROM table2
LEFT JOIN table1
ON table1.produkt = table2.produkt
With LEFT JOIN you'll return all the rows of the table you set as FROM, so you can have all the rows from the TABLE2 returning also the TABLE1 rows with a match on your LEFT JOIN Criteria (table1.produkt = table2.produkt), without your case that values would be null
Hope it can be solved your problem!

Related

How do i select rows of table if value of a column is 0 else do left join if value is not 0 in sql?

I want to get values of few columns of 'table1' if value of 'test' column of table1 is 0 else do left join with 'table2' and select few columns of 'table2' in sql?
Both table have different number of columns with different names.
SELECT t1.join_column,
t1.anycase_column,
CASE WHEN t2.join_column IS NULL
THEN t1.case_column
ELSE t2.case_column
END case_column
FROM table1 t1
LEFT JOIN table2 t2 ON t1.join_column = t2.join_column
AND t1.check_column != 0

MYSQL - How do I create a single query to accomplish this

I have a fairly complex situation where I'd like to see if there is a single query I can use to get the data.
In the following pseudo code, assume the "*" in each select clause has the appropriate columns selected and that table3 and table4 have similar enough columns that I can handle the diffs with padding/fake columns.
select * from table1 where ActiveFlag=1
for each row returned (as A)
//check if member has a record in membership table
select * from table2 where MembershipId=A.MembershipId
if (row exists)
select * from table3 where MemberId=A.MemberId
else
select * from table4 where MembershipId=A.MembershipId
end of for loop
This assumne each row in TableA can have 0-1 membership, otherwise we need to filter duplicates first.
SELECT CASE WHEN M.MembershipId IS NULL
THEN T4.field1
ELSE T3.field1
END as field1,
....
CASE WHEN M.MembershipId IS NULL
THEN T4.fieldN
ELSE T3.fieldN
END as fieldN
FROM TableA A
LEFT JOIN Membership M
ON A.MembershipId = M.MembershipId
CROSS JOIN table3 T3
CROSS JOIN table4 T4
WHERE A.MemberId = T3.MemberId
OR A.MembershipId = T4.MembershipId
If you have duplicate Membership replace the first two JOIN with something like this.
FROM (
SELECT DISTINCT TableA.*, M.MembershipId
FROM TableA A
LEFT JOIN Membership M
ON A.MembershipId = M.MembershipId
) A
CROSS JOIN ..

Select join statement is not working

I'm trying to make an output array from next statement:
I have 2 tables. In each table there is "material_code" which plays the main role. I want to select material_code from table1 where it'is equal to material_code from table2 and join them where status (from table2) is equal to 0.
This is what I've got so far.
SELECT material_code FROM table1
LEFT JOIN table2 ON material_code WHERE status IS 0
Solution:
SELECT material_code FROM table1
LEFT JOIN table2 ON table1.material_code = table2.material_code WHERE status = 0
Thank everyone.
You should try this one:
SELECT
material_code
FROM table1
LEFT JOIN table2
ON table1.material_code = table2.material_code
WHERE status = 0
Sounds like you want an INNER JOIN
SELECT material_code FROM table1
INNER JOIN table2 USING (material_code) WHERE status = 0
If you use LEFT JOIN, you will get records that only exist in table1 and not in table2. You also want to use USING not ON if the column name is the same in both tables (at least in MySQL).
WHERE status IS 0
should be
WHERE status = 0
Only when for null it have to be IS NULL.
And you also missed the join condition (ON table1.material_code = table2.material_code)
If you have a field 'status' in bot table, you can use an explicit table name before the field name to add a condition only on 'status' of table1 like :
WHERE table2.status = 0
Try this SQL code. INNER JOIN is better in your case.
SELECT tb1.material_code
FROM table1 as tb1
INNER JOIN table2 as tb2 ON tb1.material_code=tb2.material_code
WHERE tb2.status = 0

Left Join returning more records than in Table 1 and adding in additional data

I am new to MySQL having previously done everything in MS Access. I am trying to join together 2 tables so that I can show all of the records from Table1 and add in certain columns from Table2.
I can join the tables together using
SELECT Table1.Name, Table1.Address, Table1.TelephoneNumber
FROM Table1
LEFT JOIN Table2
ON Table1.TelephoneNumber=Table2.PhoneNumber
Table1 has 3900 records and Table2 almost 7million
I then want to add in (for example) PostTown and PostCode from Table2. So that my query will return
Table1.Name, Table1.Address, Table1.TelephoneNumber, Table2.PostTown, Table2.PostCode
How do I make the query only return everything in Table1 but show matches from Table2 where it has some and blanks where it hasn't. There are some blank values in the Table2.PhoneNumber which I think are duplicating in my results as it returns almost a million rows...
You have blanks in your data (not nulls):
SELECT Table1.Name, Table1.Address, Table1.TelephoneNumber
FROM Table1
LEFT JOIN Table2
ON Table1.TelephoneNumber = Table2.PhoneNumber
AND Table1.TelephoneNumber != ''
Checking for NOT NULL won't help, because null is not equal to null (whereas blank is equal to blank)
SELECT Table1.Name, Table1.Address, Table1.TelephoneNumber
FROM Table1
LEFT JOIN Table2
ON Table1.TelephoneNumber=Table2.PhoneNumber
WHERE Table1.TelephoneNumber IS NOT NULL
Try the following.
SELECT Table1.Name, Table1.Address, Table1.TelephoneNumber FROM Table1
LEFT JOIN Table2 ON Table1.TelephoneNumber=Table2.PhoneNumber
where Table1.TelephoneNumber is not null
SELECT Table1.Name, Table1.Address, Table1.TelephoneNumber, Table2.PostTown, Table2.PostCode
FROM Table1
LEFT JOIN Table2
ON Table1.TelephoneNumber=Table2.PhoneNumber
Identify all the data that you need in the SELECT statement, not just the data in Table 1.
The join will match up the correct data between the tables.

join tables and return multiple rows with same main id as single rows

I have two tables need to join and then the multiple row result with the same crb_pi_id from table2, will combine and return as single rows. it is possible? I used that result on printable report.
Thanks in advance.
table1 :
crb_pi_id,name,tel_no
1,john,1111111
2,paul,2222222
table2 :
crb_pd_id,crb_pi_id,account_name,amount
1,1,salary,500
2,1,utilities,800
3,2,transportation,300
result should
name,salary,utilities,trasportation
john,500,800,0
paul,0,0,300
select a.name, a.tel_no, sum(b.amount) as totamount
from table1 a left outer join table2 b on a.crb_pi_id = b.crb_pi_id
group by a.name, a.tel_no
You'll have to include whatever fields from table1 that you list in the select in the group statement
not sure you can really do that w/ straight sql aside from using a cursor and a temp variable being assigned the values of each cell value successively. or if you can use a higher level language you can use php's combination of implode() and string concat on the array from your query call.
edit 1: oh never mind... the question changed right after i posted my answer haha
edit 2: perhaps you wanted something like this? (untested pseudo)
select
t1.name,
case when (t2.crb_pi = t1.crb_pi) and (t2.account_name = 'salary') then t2.amount else 0 end as 'salary'
case when (t2.crb_pi = t1.crb_pi) and (t2.account_name = 'utilities') then t2.amount else 0 end as 'utilities'
case when (t2.crb_pi = t1.crb_pi) and (t2.account_name = 'transportation') then t2.amount else 0 end as 'transportation'
from table1 t1
left join table2 t2
on t2.crb_pi = t1.crb_pi
You can use with clause here and then group the results accordingly
select t1.name 'name', t2.amount 'salary',
CASE WHEN t2.crb_pi_id=t1.crb_pi_id and t2.crb_pd_id=2 then t2.amount ELSE 0 END 'utilities',
CASE WHEN t2.crb_pi_id=t1.crb_pi_id and t2.crb_pd_id=3 then t2.amount ELSE 0 END 'trans'
into #t3
from #t1 t1 inner join #t2 t2 on t1.crb_pi_id=t2.crb_pi_id
select name, max(salary), max(utilities), max(trans)
from t3
group by name