how to join from 2 fields to 1 field on another table - sql-server

on sqlServer i have 2 tables:
table1: Students => studentName=david, class1Id=2,class2Id=4
table2: classes=> classId=2, className="class1"
classId=4, className="class2"
class1Id and class2Id relate to classes.classId
i want to do sql query to get :
studentName=david, className1="class1",className2="class2",
I know to do join between 2 table but not like that
thanks!

You just need to JOIN to Classes twice:
Select S.StudentName,
C1.ClassName As ClassName1,
C2.ClassName As ClassName2
From Students S
Join Classes C1 On C1.ClassId = S.Class1Id
Join Classes C2 On C2.ClassId = S.Class2Id

Related

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'

SQL - Multi Table Joining with 1-1 and 1 - Many relationship

I am trying to learn SQL and want to understand a scenario where i can Join 2 separate queries.
I have my APP_MTRX table would join with Cust Table to retrieve all the records whose TYPE_ORD_NUM = 0 or 1
I need to join ADD_DTLS Table with SHR_ADR table based on SHR_ADR_ID and need to retrieve all the columns whose adr_type_id is either 0 or 1 again
joining the result of 1 and 2
below is my SQL
select * from app_mtrx ABC
left join cust on cust.cust_id = ABC.cust_id and abc.cust_ty_ord = 0
left join cust BBC on cust.cust_id = BBC.cust_id and abc.cust_ty_ord = 1
left join add_dtls DEF ON DEF.cust_id=BBC.cust_id
left join shr_adr SHR on shr.shr_adr_id = def.shr_Adr_id
Can you please suggest if this is the correct approach and also if it is joining my 1 & 2 correctly....
Try this:
SELECT * FROM APP_MTRX am INNER JOIN Cust c
ON am.cust_id = c.cust_id
AND am.cust_ty_ord in (0,1)
INNER JOIN add_dtls ad
ON ad.cust_id=c.cust_id
INNER JOIN shr_adr sh
ON ad.shr_adr_id = sh.shr_Adr_id
AND ad.adr_type_id in (0,1)

Left join same table twice but one column is repeat

The main table want to join two time with the reffrence table twice with the same column.
The main table column is insert with integer and need to join with the refference table to get the character back to show back for user.
it show none of records by the sql below, why?
main table BD_BRAND
Sports_BR Leather_BR
2 1
Reffrence table BD_REF
ID NME REF_TYPE
1 NIKE Sports_Brand
2 ADIDAS Sports_Brand
3 PUMA Sports_Brand
1 CLACKS Leather_Brand
2 LOTTUSSE Leather_Brand
3 CHEANEY Leather_Brand
SELECT B.NME AS Sports_BR, C.NME AS Leather_BR
FROM BD_BRAND A
LEFT JOIN BD_REF B on B.ID = A.Sports_BR
LEFT JOIN BD_REF C on C.ID = A.Leather_BR
The Result i want as below:
Sports_BR Leather_BR
ADIDAS CLACKS
I think you're looking for:
SELECT B.NME AS Sports_BR, C.NME AS Leather_BR
FROM BD_BRAND A
LEFT JOIN BD_REF B on B.ID = A.Sports_BR and B.REF_TYPE = 'Sports_Brand'
LEFT JOIN BD_REF C on C.ID = A.Leather_BR and B.REF_TYPE = 'Leather_Brand'
Your ID's are present multiple times due to the different reference types, so you need to specify which ID to grab by the reference type.

Create and execute stored procedure in SQL Server

I have four tables:
dbo.Projects (id, ProjectName, Areas, PaymentSystem, Districts.id, purpose.id, types.id, etc)
dbo.Districts(id, DistrictsName)
dbo.Purpose (id, PurposeName) - has residential & commercial
dbo.Types (id, typName)
I want to select DistrictsName where PurposeName = 'residential'
I tried this procedure :
CREATE PROCEDURE [dbo].[SearchResidentialProjects]
AS
SELECT
dbo.Projects.ID,
dbo.Districts.DistrictName,
dbo.Purpose.PurposeName
FROM
dbo.Projects
INNER JOIN
dbo.Purpose ON dbo.Projects.PurposeID = dbo.Purpose.ID
INNER JOIN
dbo.Districts ON dbo.Projects.DistrictID = dbo.Districts.ID
WHERE
dbo.Purpose.PurposeName = N'Residential'
this is the result from this procedure:
ID DistrictsName PurposeName
1 District1 residential
2 District1 residential
3 District2 residential
4 District2 residential
i want display the DistrictsName without duplicate or with different values , i a have also one more project per district in projects records . this what i want to display :
ID DistrictsName PurposeName
1 District1 residential
2 District2 residential
how i get this result ,
any help is appreciated.
Why do people use stored procedures when views are much more appropriate? I have never understood this. It seems peculiar to SQL Server users.
In any case, you can do what you want with aggregation:
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) as id,
d.DistrictName, p.PurposeName
FROM dbo.Projects pr INNER JOIN
dbo.Purpose pu
ON pr.PurposeID = pu.ID INNER JOIN
dbo.Districts d
ON pr.DistrictID = d.ID
WHERE pu.PurposeName = N'Residential'
GROUP BY d.DistrictName, p.PurposeName;
The use of table aliases makes the query much easier to write and to read.
In addition, I don't understand the id column being output. Why would you want to construct a new id? In any case, that is what your data suggests.
Use DISTINCT statement for removing the duplicates:
CREATE PROCEDURE [dbo].[SearchResidentialProjects]
AS
SELECT DISTINCT
dbo.Projects.ID,
dbo.Districts.DistrictName,
dbo.Purpose.PurposeName
FROM
dbo.Projects
INNER JOIN
dbo.Purpose ON dbo.Projects.PurposeID = dbo.Purpose.ID
INNER JOIN
dbo.Districts ON dbo.Projects.DistrictID = dbo.Districts.ID
WHERE
dbo.Purpose.PurposeName = N'Residential'

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