Join 2 tables by ID who from first table - sql

I have 2 tables, One with Auto Increment ID and the other with a column ID but no auto increment. I want IDs from the first table will be in the column ID of the second table.
I know Inner Join/ left/ right but it seems this is not what I really want.

select *
from table_a a
inner join table_b b on a.auto_increment_id = b.column_id

Related

How do I sum values ​in one place with the same ID number in different tables

for example I have 3 tables,
First Table
|PrjID|ProjectType|
1 Sample
Second Table
|PrjID|ProjectEng|
1 Joe Doen
Third Table
|PrjID|ProjectDate|
1 20112020
I want to sum values ​​in one table with the same ID number in different tables like this :
|PrjID|ProjectType|ProjectEng|ProjectDate|
1 Sample Joe Doen 20112020
I have about 150 data tables. How can I sum values ​​in one table with the same ID number in different tables with query?
Assuming you are having another fourth table containing PrjID and containing values in the val column and you want to sum val column.
SELECT f.PrjID, s.ProjectEng, t.ProjectDate, SUM(f.val) --Assuming you want to sum val column
FROM FirstTable as f
INNER JOIN SecondTable as s
ON s.PrjID = f.PrjID
Inner Join ThirdTable as t
ON t.PrjID = f.PrjID
Inner Join FourthTable as fo --Assuming this table has value column
ON fo.PrjID = f.PrjID
GROUP BY f.PrjID, s.ProjectEng, t.ProjectDate
Do you just want join?
select t1.*, t1.projecttype, t2.projecteng, t3.projectdate
from t1 join
t2
on t1.prjid = t2.prjid join
t3
on t1.projid = t3.projid;

Duplicate rows in left join

I have 2 tables. There are about 100000 of null in one column, other values are integer, total values are about 200000. Another table has only the integer value. When I use the left join on this column, it gave me a lot of duplicates rows. Is it ok to use left join here?
Table 1:
Column 1
2
3
5
null
null
Table 2:
Column 1
1
2
3
so on
Your example is really odd. Why would anyone have null values in an ID field? But anyway.
If you need fields from table 2 in the resultset as you say above then you must use an INNER JOIN not a LEFT JOIN
Something like:
SELECT DISTINCT a.id, a.name, b.someOtherField
FROM Table1 a
INNER JOIN Table2 b ON a.id = b.id
Please note: Since only the ID field of table 1 has null values there will be no records selected from table 1 with id IS NULL because they have no equivalent in table 2. Adding the DISTINCT keyword helps in case this query would still produce duplicates.

SQL - Need to conditionally join based on value in TableB column

I need to know the rows in TABLE A that have join records in TABLE B based a column value in TABLE B, but I also need to return rows in which a row in TABLE A has no match in TABLE B.
It seems like I need a LEFT JOIN and a LEFT OUTER JOIN, so I'm not sure what to do there. I understand how to do each, but don't understand how to do them together.
The schema looks like:
TABLE_A
pk
TABLE_B
pk
a_fk
some_value
I need the joined rows where Table_A has no join record in Table_B OR Table_A has a join record row in Table_B (it can have many) in which some_value does not equal "thisValue"
Thanks.
A Left join is a left outer join. Outer joins preserve one of the tables which is what you are after so good guess.
SELECT *
FROM Table A
LEFT JOIN Table B
ON TableA.Column = TableB.Column
AND B.SomeValue <> 'ThisValue'
All of the rows with a match will have the B information populated all of those without will have nulls in the B data

SQL MS Access Summing Results Based on Two Table Criteria

I've been asked to find if totals from one table exceed a certain value. However the identifier I need to group these is stored in another table. So I've figured how to isolate what I need and then copy into excel. However I do understand the principle of summing in SQL, as I've made my own queries that look like this:
SELECT * FROM
(SELECT ID, SUM(Table1.Amount) AS Subtotal FROM Table1 WHERE LineNumber = 1 GROUP BY ID) AS a, Table2 AS b
WHERE a.ID = b.ID
AND a.Subtotal > b.Threshold
In this case, the totals I need to sum all have the same LineNumber from the original table (Table1) so it's easy to compare to a value in a different table. What I want to do is be able to is join a subquery back to the original table, then GROUPBY the identifier and sum from the other table, adding a criteria that the original ID from table one must be a duplicate:
SELECT * FROM
((Table1 as t
INNER JOIN
(SELECT ID FROM Table1
GROUP BY ID HAVING COUNT(ID) >1) as b
ON t.ID = b.ID
Joining criteria back to the original table, I need to do this because the table I need to sum doesn't contain ID,
INNER JOIN Table2 as c
ON t.ID2 = c.ID2
WHERE c.LineNumber = 4
This is where I'm stuck. I want to sum all the LineNumber = 4 for each ID. Again, I had to join using ID2 because ID1 isn't in Table2
Perhaps I'm making this too complicated? Any suggestions would be welcome
Thanks!

Comparing value in a table vs count of rows in other table

I have two tables A (primary key - unit_id) and B (primary key - unit_id)
I have a value (eg :4 ) in table A and has a unit_id.
I have 4 rows in table B with the same unit ID
I have to write a SQL query to check whether the value in table A matches with the count (rows) in table B with the same unit_id
You can just use inner join and you will see how many values from table A are in table B :
Select a.unit_id from Table1 a inner join Table2 b on a.unit_id = b.unit_id
Im assumin that is what you need , because as #StanislavL pointed out, you cant have more then one unique unit_id in each table.