How to get query from two different tables as single table - sql

I have two tables
Tab1
id Name Level
1 a1 1
2 a2 1
3 b1 2
4 b2 2
5 c1 3
6 c2 3
7 b3 2
8 c3 3
9 c4 3
Tab2
id linkid
1 3
3 6
1 7
7 8
3 9
I am trying to fetch different levels of id which falls for level = 1
something like
Tab3
Level1 Level2 Level3
1 3 6
1 7 8
1 3 9
Can anyone help me?

Following statement is sufficient for the data you've presented ...
SQL Statement
SELECT lvl1.id AS Level1
, lvl2.id AS Level2
, lvl3.id AS Level3
FROM Tab2 lvl1
INNER JOIN Tab2 lvl2 ON lvl2.id = lvl1.linkid
INNER JOIN Tab2 lvl3 ON lvl3.id = lvl2.linkid
WHERE lvl1.id = 1
... but I assume you somehow need Tab1 incorporated into the results so this might be closer to what you really require
SQL Statement
SELECT lvl1.id AS Level1
, lvl2.id AS Level2
, lvl3.id AS Level3
FROM Tab1 t1
INNER JOIN Tab2 lvl1 ON lvl1.id = t1.id
INNER JOIN Tab2 lvl2 ON lvl2.id = lvl1.linkid
INNER JOIN Tab2 lvl3 ON lvl3.id = lvl2.linkid
WHERE t1.id = 1
note that I assume Tab2.id being a foreign key to Tab1.id

Related

SQL Server : select from multiple tables and calculate percentages

I have four tables and I would need to extract data from them to calculate the percentage
Table1
ID FK1 FK2
------------------
1 1 1
2 2 2
3 3 3
Table2
ID Name
------------------
1 L1
2 A1
3 B
Table3
ID FK3
------------------
1 1
2 2
3 3
Table4
ID Name
------------------
1 BA
2 N
3 CE
Now I need to get a Name from table4, which will be displayed as individual rows and then a Name from table2, which will be listed as individual columns and the value will then be a percentage of the record from table4:
Name L1 A1 B
---------------------------
BA 20(%) 40(%) 40(%)
N 30(%) 20(%) 30(%)
CE 15(%) 15(%) 70(%)
Because there are links, I'll give an example of what question I have now
select t3.Name
from table1 t1 (nolock)
join table2 t2 (nolock) on t1.FK1 = t2.ID
join table3 t3 (nolock) on t1.FK2 = t3.ID
join table4 t4 (nolock) on t2.FK3 = t4.ID
Does anyone have any idea how to do this? Thank you very much

Join two tables, using value from the first unless it is null, otherwise use value from the second

I have three tables which look like those:
TABLE 1
id j_id
1 1
2 2
3 3
TABLE 2
id j_id table1_id
1 57 1
2 84 1
3 1 1
4 9 2
5 2 2
and every j has a value in a third table
id value
1 1abc
2 2bcd
3 3abc
57 57abc
84 84abc
9 9abc
I am trying to write a query which will join table 1 and table 2 and use the J value from the third table instead of the j_id, but the problem is that I want to use the j value from the second table if it exists and otherwise use the value from the first table.
in order the make it clearer this is my query result without using the third table:
tbl1.j_id tbl2.j_id
1 1
1 84
1 57
2 2
2 9
3 null
I want the end query result to use the second table's j value unless it is null:
tbl1.j_id tbl2.j_id j_id
1 1 1abc
1 84 84abc
1 57 57abc
2 2 2abc
2 9 9abc
3 null 3abc
(Question and title edits are more than welcome, weren't that sure how to phrase them..)
You can simply JOIN to table3 on the COALESCE of table2.j_id and table1.j_id:
SELECT t1.j_id AS t1_j_id, t2.j_id AS t2_j_id, t3.value
FROM table1 t1
LEFT JOIN table2 t2 ON t2.table1_id = t1.id
JOIN table3 t3 ON t3.id = COALESCE(t2.j_id, t1.j_id)
Output:
t1_j_id t2_j_id value
1 1 1abc
1 57 57abc
1 84 84abc
2 2 2bcd
2 9 9abc
3 null 3abc
Demo on dbfiddle
One solution is to left join table3 twice:
select
t1.j_id,
t2.j_id,
coalesce(t31.value, t32.value) j_value
from
table1 t1
left join table2 t2 on t2.table1_id = t1.id
left join table3 t31 on t31.id = t2.j_id
left join table3 t32 on t32.id = t1.j_id

Self join queries in hive

I need a hive query to fetch the hierarchy in which my product was sold .
Considering the below records , the end customer was 1 and 6 , since their SoldTo column value is NULL.
CustomerID SoldTo
--------------------
1 NULL
2 1
3 2
4 3
5 4
6 NULL
7 1
8 6
My output should look like :
c1 c2 c3 c4 c5
-------------------
5 4 3 2 1 (c1 (5) - first customer who bought product and c5(1) -last customer)
8 6 (c1 (8) - first customer , c2 (6)- Last customer)
7 1
Hive has no real support for recursive CTEs or hierarchical data structures. You can do this using multiple joins -- but the depth of the hierarchy is fixed.
select t1.CustomerId as c1, t2.CustomerId as c2, t3.CustomerId as c3,
t4.CustomerId as c4, t5.CustomerId
from t t1 left join
t t2
on t2.SoldTo = t1.CustomerId left join
t t3
on t3.SoldTo = t2.CustomerId left join
t t4
on t4.SoldTo = t3.CustomerId left join
t t5
on t5.SoldTo = t4.CustomerId
where t1.CustomerId is null;

If Statements in where clause?

So I have to run a query against a database schema: http://sqlfiddle.com/#!9/d0b643 , but an example schema would look like:
Table 1
itemID sale date salesmanID storeID
---------------------------------------------------
1 1/2015 1 1
1 3/2016 1 1
2 5/2016 2 1
2 1/2015 4 1
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 salesmanID storeID
----------------------------------------------------------------
1/1/2014 12/31/2014 1 0 1
1/1/2015 12/31/2015 2 0 1
1/1/2016 12/31/2016 3 0 1
1/1/2014 12/31/2014 3 2 1
1/1/2015 12/31/2016 2 2 1
I need to have something in the where clause that pretty much says if there is a SalesmanID and the saleDate from Table1 falls between the StartDate and Enddate of Table4, use that color. Otherwise, if there is no salesmanID, use the StoreID (in this example they are all 1, but they could be different).
The current query I am adding this to is:
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;
How can I run this? The expected results should look like:
itemID color price
1 Blue 10
1 Green 13
2 Blue 14
2 Blue 14
I am trying to use MSSQL logic. and try it in SQL fiddle it run success.
Just try this code yourself to see if this is what you want.
select t1.itemID,t3.color,t2.price from table_1 t1
LEFT JOIN table_4 t41
on t1.sale_date BETWEEN t41.saleBegin and t41.saleEnd
and t41.salesmanID!=0 and t1.salesmanID=t41.salesmanID
LEFT JOIN table_4 t42
on t1.sale_date BETWEEN t42.saleBegin and t42.saleEnd
and t42.salesmanID=0 and t1.storeID=t42.storeID
INNER JOIN table_2 t2
ON t1.itemID = t2.itemID
INNER JOIN table_3 t3
ON t2.colorID = t3.colorID and t2.colorID=IF(ISNULL(t41.colorID),t42.colorID,t41.colorID)
order by t1.itemID,t3.color,t2.price;
I think this is what you want. You will get too many rows because of overlaps between store # and Sale dates. Once I changed your sample data table4 to have the last two rows be store #2 I get the results you posted.
1/1/2014 12/31/2014 3 2 2
1/1/2015 12/31/2016 2 2 2
Select t1.itemID,t3.color,t2.price
From table_1 t1
LEFT JOIN table_4 t4 ON t1.sale_date BETWEEN t4.saleBegin And t4.saleEnd And t1.SalesmanID = t4.SalesmanID
LEFT JOIN table_4 t4a ON t1.sale_date BETWEEN t4a.saleBegin And t4a.saleEnd And t1.StoreID = t4a.StoreID
LEFT JOIN table_2 t2 ON t1.itemID = t2.itemID And t2.ColorID = t4a.ColorID
LEFT JOIN table_3 t3 ON t2.colorID = t3.colorID
Where t4.SalesmanID Is Null
Group By t1.itemID,t3.color,t2.price
Union All
Select t1.itemID,t3.color,t2.price
From table_1 t1
LEFT JOIN table_4 t4 ON t1.sale_date BETWEEN t4.saleBegin And t4.saleEnd And t1.SalesmanID = t4.SalesmanID
LEFT JOIN table_2 t2 ON t1.itemID = t2.itemID And t2.ColorID = t4.ColorID
LEFT JOIN table_3 t3 ON t2.colorID = t3.colorID
Where t4.SalesmanID Is Not Null
itemID color price
1 Blue 10.000
1 Green 13.000
2 Blue 14.000
2 Blue 14.000

Selecting a Column based on Child and Parent Category

I Have a SQL server 2008 Database. I have a table with the following structure
CatID ParentCatID NAME Level
1 NULL A 1
2 1 B 2
3 2 C 3
4 NULL D 1
5 4 E 2
6 5 F 3
7 NULL G 1
8 7 H 2
I want to Select the Name column in Heirarchical format like below:
Level1 Level2 Level3
A B C
D E F
In my Table all level1 categories have level2 children. Similarly all level2 children have level3 children. So how can i get the data in my desired format.
It's a simple query like this:
select
C1.NAME as Level1,
C2.NAME as Level2,
C3.NAME as Level3
from Categories C1
inner join Categories C2
on C2.ParentCatId = C1.CatId
inner join Categories C3
on C3.ParentCatId = C2.CatId
where C1.Level = 1 and C2.Level = 2 and C3.Level = 3 and