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

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.

Related

SQL Request for joining a table and printing its column twice in the result table

There are two tables (A, B) in my database and I'm trying to join them and print the same column of table B twice in the result table. The problem is that I need to put in compliance with two different columns of table A, so to join these tables also twice. I tried to join them and put in the select expression required column two times, but in this case result table shows the same relation in every column.
Here is the result I want, where the id column of table B includes id_1 and id_2 from table A and the output names correspond to ids from table A.
Table A: (integer id_1, integer id_2)
Table B: (integer id, varchar name)
Result Table: (id_1, name, id_2, name)
You need inner join with tableB twice as follows:
select a.id_1, b1.name, a.id_2, b2.name
from tableA a join tableB b1 on a.id_1 = b1.id
join tableB b2 on a.id_2 = b2.id

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;

Select Statement that orders depending on existence of foreign key in other table

I have two tables a and b where b contains a foreign key fk_a_id to table a id column.
Now I would like to select on table a but order the result depending on whether table b has a foreign key entry for it or not. the rows where table b does not have an entry for it should come first.
I haven't tried much yet, besides a join, which is probably not even the right direction.
select a.*
from a as a
join b as b on b.fk_a_id = a.id
order by id desc
One method is a left join. But that could duplicate rows if b contains multiple instances of a given key.
Another method uses logic in the order by:
select a.*
from a
order by (case when not exists (select 1 from b where b.fk_a_id = a.id) then 1 else 2 end),
id desc;
For performance, you would want an index on b(fk_a_id).

Join 2 tables by ID who from first table

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

Two tables connected by foreign key

I need to connect two tables in SQL Server: Table A has a primary key called IDTableA (integer field) and Table B has a primary key IDTableB (integer field as well).
Table A contains a foreign key IDTableB whereby I want to connect both tables, this field is of type integer and its value is 0.
My problem is that since there is no record in Table B whose ID is 0, do not show me those records from table A with a 0 stored in the foreign key.
The relationship between both tables is: a record in Table A may belong to one or no record in the table B. Therefore, the default value of the foreign key in table A is 0.
I tried connecting the tables with INNER JOIN and LEFT OUTER JOIN but it does not work and left no records show. What I can do?.
Thanks.
My SQL statement:
Select TableA.*
From TableA
inner join TableB on TableA.IdTableB = TableB.IdTableB
The Solution:
Select TableA.*
From TableA
LEFT JOIN TableB on TableA.IdTableB = TableB.IdTableB
More Info:
LEFT JOIN vs. LEFT OUTER JOIN in SQL Server
Try to use left join only
Something like this
Select *
from table_1 tbl1
left join table_2 tbl2 on tbl2.id = tbl1.id
An inner join is the most common join operation used in applications and can be regarded as the default join-type. Inner join creates a new result table by combining column values of two tables (A and B) based upon the join-predicate. The query compares each row of A with each row of B to find all pairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, column values for each matched pair of rows of A and B are combined into a result row.
FROM Table A <renametablehere>
Inner Join Table B <renametablehere>
On <renamedtablename for A>.ID = <renamed table name for b>B.ID