Many Columns in One Table JOIN one reference table - sql

image. Very new so, please be kind. Using DBForge Query Builder
I have one table with a number of columns which contain abbreviations.
Another table contains the descriptions for the abbreviations.
Need to write a query to replace all the abbreviations with their descriptions. Tried a number of joins without any success. Any ideas?
T1
ID Date Type Cat Sub Cat
1 01/09/18 E F L
2 05/09/18 Cc F D
3 06/09/18 Cc C Dr
4 08/09/18 Cc C Sh
5 08/09/18 E C Sh
T2
Code Des
E Eft Payment
Cc Credit Card
F Food
C Clothes
B Breakfast
L Lunch
D Dinner
Sh Shirt
Dr Dress
Desired Output
ID Date Type D Cat D Sub Cat D
1 01/09/18 Eft Payment Food Lunch
2 05/09/18 Credit Card Food Dinner
3 06/09/18 Credit Card Clothes Dress
4 08/09/18 Credit Card Clothes Shirt
5 08/09/18 Eft Payment Clothes Shirt

Try below using multiple join by T2 with different alias
select
a.ID, a.date,
b.Des as TypeD,
c.Des as CatD,
d.Des as SubCatD
from T1 a inner join T2 b on a.type=b.code
inner join T2 c on a.Cat=c.code
inner join T2 d in a.SubCat=d.code

I will assume the columns of T1 are (ID, Date, Type, Cat, SubCat)
SELECT T1.ID, T1.Date, T2Type.Des, T2Cat.Des, T2SubCat.Des
FROM T1 INNER JOIN T2 T2Type ON T1.Type = T2Type.Code
INNER JOIN T2 T2Cat ON T1.Cat = T2Cat.Code
INNER JOIN T2 T2SubCat ON T1.SubCat = T2SubCat.Code

You need to use multiple instances of the T2 table:
select t1.id, t1.date, typ.des as type_d,ca.des as cat_d,subca.des as sub_cat_d from T1 t1
inner join T2 typ on t1.type=typ.code
inner join T2 ca on t1.cat = ca.code
inner join T2 subca on t1.subcat = subca.code;
Try this...

Related

Selecting values from 2 columns that both are IDs of another tables rows

Let's say i have:
Table1:
ID Name
1 Ann
2 Mike
3 Stan
4 Kyle
Table2:
Pair ID Person1ID Person2ID
1 1 2
2 3 4
I want to select pairs, but with names instead of IDs, so this would be the output:
1 Ann Mike
2 Stan Kyle
I imagine a simple:
inner join Table1 on Table1.ID=Table2.Person1ID
won't work, because I want both of them, not only one.
I'm pretty new to SQL so i'm sorry if there is a simple answer.
You have to join twice and use alias to diference the tables
SELECT t2.PairID, A.Name, B.Name
FROM Table2 t2
JOIN Table1 A
ON t2.Person1ID = A.ID
JOIN Table1 B
ON t2.Person2ID = B.ID
You can join sub query like below:
Select p.PairID, p.Name, q.Name
from
(Select t2.PairID, t1.Name
from Table1 t1 inner join Table2 t2
on t1.ID = t2.Person1ID) p
inner join
(Select t2.PairID, t1.Name
from Table1 t1 inner join Table2 t2
on t1.ID = t2.Person2ID) q
on p.PairID = q.PairID
See the result here in the demo.

How to join one table with another two tables in sql?

Suppose I have 3 tables and want to join three tables as Result below without repetitive values and NOT NULL.
Table-1: EMP
Id Name Client_Id
1 abc c1
2 xyz c2
3 pqr c3
Table-2: EMP_Account:
Id salary
1 10,000
2 20,000
3 30,000
Table-3: Client_Details;
Client_Id Client_Name
c1 client_1
c2 client_2
c3 client_3
Final Result:
Id Name Salary Client_Name
1 abc 10,000 client_1
2 xyz 20,000 client_2
3 pqr 30,000 client_3
I tried to do Join with Left join but not success.
So, can anyone help me or suggest that how we can do this?
You can join more than once, but each must be in its own section
select t1.id, t1.name, t2.salary, t3.client_name
from EMP t1
inner join EMP_Account t2
on t1.id = t2.id
inner join Client_Details t3
on t1.client_id = t3.client_id
Aliasing helps
If you want to return results even where there is no client info, substitute inner for left
SELECT e.id, e.name, ea.salary, cd.client_name
FROM emp e
JOIN emp_account ea ON e.id = ea.id
JOIN client_details cd ON cd.client_id = e.client_id
SELECT E.ID, E.NAME, EA.SALARY, CD.CLIENT_NAME FROM EMP E
JOIN EMP_ACCOUNT EA ON EA.ID = E.ID
JOIN CLIENT_DETAILS CD ON CD.CLIENT_ID = E.CLIENT_ID

SQL nested Inner Joins? Or Where Clause On Inner Joins?

I am having an issue with a query I am trying to write. Unfortunately I can only query against the database and cannot change the schema. Here are some simplified example tables:
Table 1
itemID sale date
----------------------------
1 1/2015
1 3/2016
2 5/2016
2 1/2015
Table 2
itemID colorID price
--------------------------------------
1 1 23
1 2 10
1 3 13
2 1 11
2 2 14
2 3 18
Table 3
ColorID color
---------------------------------------
1 Red
2 Blue
3 Green
Table 4
SaleBegin SaleEnd ColorID
----------------------------------------
1/1/2014 12/31/2014 1
1/1/2015 12/31/2015 2
1/1/2016 12/31/2016 3
Now I need a query that essentially gets the price and color for both item ids in the first table. I'm not sure how to do it with this schema even just for one. I tried something like:
SELECT item_id, price, color FROM Table1 T1
INNER JOIN Table2 T2
ON T1.ItemID=T2.ItemID
INNER JOIN Table3 T3
ON T2.ColorID=T3.ColorID
INNER JOIN Table4 T4
ON T3.ColorID=T4.ColorID
WHERE T1.itemID between SaleBegin AND SaleEnd
But it's not working for me, instead giving me every possible color and price for each item. What am I doing wrong? How do I get those values? Expected results should look like:
itemID color price
------------------------------------
1 Blue 10
1 Green 13
2 Green 18
2 Blue 14
http://sqlfiddle.com/#!9/e6fac/4
SELECT t1.itemID
, t3.color
, t2.price
FROM table_1 t1
LEFT JOIN table_2 t2
ON t1.itemID = t2.itemID
LEFT JOIN table_3 t3
ON t2.colorID = t3.colorID
LEFT JOIN table_4 t4
ON t3.colorID = t4.colorID
WHERE t1.sale_date BETWEEN saleBegin and saleEnd;
I'm not sure this is what you're looking for, but this query will give you the itemID, color and price for each product during each sale
SELECT
t4.SaleBegin
,t4.SaleEnd
,t1.itemID
,t3.color
,t2.price
FROM Table4 t4
INNER JOIN Table1 t1
ON t4.SaleBegin = CAST(REPLACE(t1.[sale date], '/', '/1/') AS DATE)
INNER JOIN Table2 t2
ON t1.itemID = t2.itemID
AND t4.ColorID = t2.ColorID
INNER JOIN Table3 t3
ON t4.ColorID = t3.colorID
Here is the SQL Fiddle

How to join with two tables simultaneously?

Table Product
Id name t1 t2
1 A 1 4
2 B 5 2
3 C 3 1
4 D 4 5
Table Tan
id tan
1 tanA
2 tanB
3 tanC
4 tanD
5 tanE
I have two above table and i want the result as below in expecting result how it is possible.
Expecting result
A tanA tanD
B tanE tanB
C tanC tanA
D tanD tanE
You can join on multiple tables:
SELECT p.Name AS ProductName,
t1.tan AS Tan1,
t2.tan AS Tan2
FROM dbo.Product p
INNER JOIN Tan t1
ON p.t1 = t1.id
INNER JOIN Tan t2
ON p.t2 = t2.id
ORDER BY ProductName ASC
Demo
You can select from the tan table twice by using a table alias:
FROM Product INNER JOIN tan tan1 ON tan1.id = product.t1
INNER JOIN tan tan2 ON tan2.id = product.t2
select the appropriate columns:
SELECT Product.name AS name, tan1.tan AS Tan_1, tan2.tan AS Tan_2
to give:
SELECT product.name AS name, tan1.tan AS Tan_1, tan2.tan AS Tan_2
FROM Product INNER JOIN tan tan1 ON tan1.id = product.t1
INNER JOIN tan tan2 ON tan2.id = product.t2

SQL Server: Subquery on a join

I have two tables with schema and data as below. Table A has an id and an associated name. Table B associates the id from Table A with a price and otherAttr. For each entry in Table A, there may be multiple entries with different prices in Table B, but otherAttr is the same for each entry.
Given an id for Table A, I would like to select the name, otherAttr, and the minimum price.
The below query returns multiple results, I need to write a query that will return a single result with the minimum price.
SELECT a.name, b.price, b.otherAttr
FROM A a
LEFT Join B b on b.idFromA = 1
WHERE a.id = 1
Table A Table B
id | name idFromA | price | otherAttr
-------- ---------------------------
1 | A 1 | 200 | abc
2 | B 1 | 300 | abc
1 | 400 | abc
2 | 20 | def
2 | 30 | def
2 | 40 | ef
I massively oversimplified my example. In addition to selecting the min price and otherAttr from Table B, I also have to select a bunch of other attributes from joins on other tables. Which is why I was thinking the Group By and Min should be a subquery of the join on Table B, as a way to avoid Grouping By all the attributes I am selecting (because the attributes being selected for vary programmatically).
The Actual query looks more like:
SELECT a.name, b.price, b.otherAttr, c.x, c.y, d.e, d.f, g.h....
FROM A a
LEFT Join B b on b.idFromA = 1
LEFT Join C c on something...
LEFT Join D d on something...
LEFT Join G g on something...
WHERE a.id = 1
To get this, you could use GROUP BY in an INNER query:
SELECT gd.name, gd.price,gd.otherAttr, c.x, c.y, d.e, d.f, g.h....
FROM
(SELECT a.id,a.name, MIN(b.price) as price, b.otherAttr
FROM A a
LEFT Join B b on b.idFromA = 1
WHERE a.id = 1
GROUP BY a.id,a.name,b.otherAttr) gd
LEFT Join B b on b.idFromA = 1
LEFT Join C c on something...
LEFT Join D d on something...
LEFT Join G g on something...
Try:-
SELECT a.name, MIN(b.price) minprice, b.otherAttr
FROM A a
LEFT Join B b ON a.Id = b.idFromA
GROUP BY a.name, b.otherAttr
HAVING a.id = 1
You could just do this instead:
SELECT a.name, MIN(b.price), MIN(b.otherAttr)
FROM TableA a
LEFT JOIN TableB b on b.idFromA = a.id
GROUP BY a.name
HAVING a.id = 1;
You need to inner join on price as well in addition to id on the subquery to intersect the right record(s) with the lowest price(s). Then TOP(1) will return only one of those records. You can avoid using TOP(1) if you can expand the conditions and group by fields in the subquery so you schema can assure only a single record is returned for that combination of attributes. Lastly, avoid left joins when intersecting sets.
SELECT TOP(1) p.id, p.price, b.OtherAttr
FROM B as b
INNER JOIN
(SELECT A.id, min(B.price) as price
FROM B
INNER JOIN A on A.id=B.idFromA and A.ID=1
GROUP BY A.id) as p on b.idFromA=p.id and b.price=p.price