How to join two tables with same ID? - sql-server

For example, I have two tables.
Table1:
Schools
Type
City
school1
A
CityA
school2
B
CityB
Table2:
schools
population
time
school1
1000
01/01/2021
school2
2000
01/02/2021
school3
3000
01/03/2021
I want to figure out how to join them like below (without school3):
schools
type
city
population
time
school1
A
CityA
1000
01/01/2021
school2
B
CityB
2000
01/02/2021
I am a beginner and really confused about how to deal with this problem.
http://sqlfiddle.com/#!18/3eaf9/2

You should use JOIN:
SELECT t1.schools, t1.type, t1.city, t2.population, t2.time
FROM
Table1 t1
INNER JOIN
Table2 t2 ON t1.schools = t2.schools
More information how JOIN works you can see in picture in this post
Difference between INNER JOIN and OUTER JOIN see here

Related

Get all rows even if not existe - SQL

I have 2 tables :
T1:(sales per store)
`Store Sales DateT
A01 100 01-01-22
B01 200 01-01-22
C01 300 01-01-22`
T2 :(store)
`CodeStore Name
A01 Name1
B01 Name2
C01 Name3
D01 Name4`
I want to get all rows of both tables, even if there is no sales in a store.
For ex : in table T1, there's no sales for the store D01 in this date (01-01-22) : but i want to get this record.
expected result :
`Store Sales
A01 100
B01 200
C01 300
D01 `
I'v tried this code, but the store D01 doesn't appear :
`SELECT CodeStore, sum(Sales)
FROM T1
LEFT OUTER JOIN T2 ON CodeStore = Store
where DateT = '01-01-22'
group by CodeStore`
You have to switch the two tables in your query so that all records from T2 will be included. Also, the DateT filter should be in the join criteria (in the ON clause) now since it filters T1.
SELECT CodeStore, sum(Sales)
FROM T2
LEFT OUTER JOIN T1 ON CodeStore = Store AND DateT = '01-01-22'
group by CodeStore
Using a left join (LEFT OUTER JOIN) ensures that even if there isn't a matching row in the joined table (T1), all the rows from the table in the select (T2) will be included in the result.

SQL Join Including NULLs

Using SQL Server Management Studio 2016 (v 13.0).
I have two tables with two distinct keys I can use to join, with the catch being that there are mixed NULLs in both PK columns:
Table 1 Table 2
Acct1 App1 Acct2 App2 Product
----------- ----------------------
1 A NULL A Bed
2 B 2 B Sofa
3 C 3 NULL Bed
4 D 4 D Bed
Desired result in the joined table, only including those where Product = Bed:
Acct App Product
1 A Bed
3 C Bed
4 D Bed
Thank you!
While I agree #d219's answer should be the correct solution, a different approach could use an or in the join such like:
select Acct1,App1,Product
from table1 inner join table2
on App1=App2 or Acct1=Acct2
where Product='Bed'
See this post for discussion on using the or join.
One way would be to do two separate SELECT statements using each key and then UNION them, something like this:
SELECT t1.Acct1, t1.App1, t2.Product
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.Acct1 = t2.Acct2
WHERE t2.Product = 'Bed'
UNION
SELECT t1.Acct1, t1.App1, t2.Product
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.App1 = t2.App2
WHERE t2.Product = 'Bed'

Query junction table-many to many relationship

I'm trying to run a query for a many-many relationship, I created a junction table to do that. Now I'm having issues getting no results when I'm expecting to return 3 rows for each of indexes 122,123,124, can anyone point out my errors, thank you
SELECT * FROM [Moldingdata].[dbo].[mach_part_junction] ORDER BY [machinename] ASC
SELECT MachineList.machinename ,JDEPARTIMGLU.Jde_part_num
from mach_part_junction
INNER JOIN MachineList on mach_part_junction.machinename = machinelist.Machine_ID
INNER JOIN JDEPARTIMGLU on mach_part_junction.machinename = machinelist.Machine_ID
WHERE mach_part_junction.machinename= 'MM01'
Results:
machinename ndx_jde_part_img
----------- ----------------
MM01 122
MM01 123
MM01 124
MM04 122
MM15 124
MM17 122
MM32 122
MM32 123
(8 row(s) affected)
machinename Jde_part_num
----------- --------------------
(0 row(s) affected)
The join conditions on your second and third table are the same.
It looks like the third table's join condition is the one that needs to be changed.
When I troubleshoot queries like this I generally do the following:
SELECT *
FROM mach_part_junction
INNER JOIN MachineList on mach_part_junction.machinename = machinelist.Machine_ID
Then I add the next JOIN:
SELECT *
FROM mach_part_junction
INNER JOIN MachineList on mach_part_junction.machinename = machinelist.Machine_ID
INNER JOIN JDEPARTIMGLU on mach_part_junction.machinename = machinelist.Machine_ID
then the WHERE (if the tables aren't too big)
That way you can see what's going on.

Combine two subqueries to main query in SQL

I am new to SQL and need assistance to combine (JOIN) two subqueries with a main query in SQL. Here is what I have. *Each query works independent of another. The end result would be that I would retrieve the # of accommodations for each resort, retrieve the lowest cost of all accommodations for each resort, and join those results to the list of resort types and resorts.
DB Schema
Table 1 - Resort -
resort_id (PK)
resort_type_id (FK)
name
Table 2 - Resort_type -
resort_type_id (PK)
resort_type
Table 3 - Accommodations -
accommodations_id (PK)
resort_id (FK)
description
cost_per_night
Query
SELECT Resort.name, Resort_type, Acc.Accommodations, Low.min_price
FROM
(SELECT resort.name AS resort_name, Resort_type.resort_type
FROM Resort
INNER JOIN Resort_type
ON Resort.resort_type_id = Resort_type.resort_type_id
(SELECT resort_id, Count(resort_id) AS Accommodations
FROM Accommodations
GROUP BY resort_id) AS Acc
(SELECT resort_id, Min(cost_per_night) AS min_price
FROM Accommodations
GROUP BY resort_id) AS Low
Any guidance would be greatly appreciated. I am having a difficult time visualizing how this should come together.
The query below lists each resort and its type along with the number of accommodations and the lowest cost per night.
select
r.name,
t.resort_type as type,
count(a.accommodations_id) as accommodations,
min(cost_per_night) as lowestcost
from resort r
inner join resort_type t
on t.resort_type_id = r.resort_type_id
left join accommodations a
on a.resort_id = r.resort_id
group by r.name, t.resort_type
Example: http://sqlfiddle.com/#!9/fc089/6
Is this what you're looking for:
SELECT
r.name,
rt.resort_type,
t.no_of_accommodations,
t.min_price
FROM Resort r
INNER JOIN Resort_Type rt
ON rt.resort_type_id = r.resort_type_id
LEFT JOIN(
SELECT
COUNT(*) AS no_of_accommodations,
MIN(cost_per_night) AS min_price
FROM Accommodations
GROUP BY resort_id
)t
ON t.resort_id = r.resort_id

SQL Server date_format in join

I have two tables with two formats date
table 1 :
id time ref
5 1397635972 A
10 1397635975 B
50 1397635976 C
table 2 :
id time ref
10 2013/10/05 D
51 2014/01/02 E
how join two table on table1.id=table2.id and table1.time=table2.time
This is my attempt :
$sql=' select table1.id, table1.time, table1.ref, table2.id, table2.time, table2.ref
from table1 INNER JOIN table2
ON (table1.id = table2.id AND DATE_FORMAT(table1.time,'%y/%m/%d') = table2.time)';
Try this:
select table1.id, table1.time, table1.ref, table2.id, table2.time, table2.ref
from table1 INNER JOIN table2
ON (table1.id = table2.id AND FROM_UNIXTIME(table1.time) = table2.time)
Are you sure that the ID needs to be a part of the join as well? This kind of join with this kind of structure would make almost no sense from a database design standpoint...
No data are not shown
For example FROM_UNIXTIME table2.time :
SELECT FROM_UNIXTIME(1196440219) : '2007-11-30 10:30:19'
I need to format then become : 2013/10/05

Resources