merge multiple tables in Hive SQL - sql

I have two original tables: product table and component tables
Firstly, I need to do an inner merge between these two tables, then create a new table called T1:
select a.Product, a.Plant, b.component, b.position, b.valid_date from Product a
inner join component b on a.Product=b.Product
where a.Plant='A'
The result T1 looks like in the following way:
Furthermore, I need to create a new table named T2 based on T1
select T1.Product, T1.Plant,T1.position,max(T1.valid_date) as valid_date from T1
Where T1.Plant='A'
Group by T1.Product, T1.Plant,T1.position
The result T2 is:
Finally, I want to merge T1 and T2 based on Product, Plant, position, and valid_date for a final table:
select T2.Product, T2.Plant,T1.Component, T2.position, T2.valid_date from T1
INNER JOIN T2 on T1.Product=T2.Product and T1.Plant=T2.Plant and T1.position=T2.position and T1.valid_date=T2.valid_date
where T1.Plant='A'
The final table:
I know this whole process can be done in one hive SQL script. I am confused with multiple tables in one query. I appreciate someone can help me for that. Thank you

isn't it that you just want to do this?
select
a.Plant,b.Product,b.position,b.component,
max(valid_date) as valid_date
from Product a
inner join component b on a.Product = b.Product
where a.Plant = 'A'
group by a.Plant,b.Product,b.position,b.component
db<>fiddle here
the query above produces what you show in output , but based on your comment , I think this is what you are looking for :
select t.Plant,t.Product,t.position,t.component, max(t.valid_date) valid_date
(
select
b.Product, a.Plant,b.component,b.position,max(valid_date) over (partition by a.Plant,b.Product,b.position) as valid_date
from Product a
join component b on a.Product = b.Product
where a.Plant = 'A'
) t
group by t.Plant,t.Product,t.position,t.component;

Related

Sum Column in Joined Table and add as column SQL

So say I have two tables in Oracle SQL (not actually data but for ease should highlight my question)
Table1 that contains only Order_id and Order_quantity and Table2 that contains only Order_id and Order_price
Then I join them as follows
Select T1.Order_id,
T1.Order_quantity,
T2.Order_price,
T1.Order_quantity*T2.Order_price As "Order_amount",
Sum(Order_amount) As "Total_Sales"
from Table1 T1
inner join Table2 T2
on T1.Order_id = T2.Order_id
So essentially I want to have two extra columns, one as the product of columns from the two tables, and another as the sum of that column in my joined table(so every entry will be the same). However as you need to
SUM(variable_name) From Table_Name
Can I assign a variable name to my new table and then refer to that. I tried the following but I'm getting a SQL command not properly ended error
Select T1.Order_id,
T1.Order_quantity,
T2.Order_price,
T1.Order_quantity*T2.Order_price As "Order_amount",
Sum(Order_amount) from New_Table As "Total_Sales"
from (Table1 T1
inner join Table2 T2
on T1.Order_id = T2.Order_id) As New_Table
Thanks for any assistance, apologies as I have a pretty naive understanding of SQL at present
I think you just want a window function:
select T1.Order_id, T1.Order_quantity, T2.Order_price,
T1.Order_quantity*T2.Order_price As order_amount,
sum(T1.Order_quantity*T2.Order_price) over () As Total_Sales
from Table1 T1 inner join
Table2 T2
on T1.Order_id = T2.Order_id
You cannot re-use the alias order_amount in the select. You need to repeat the expression -- or use a subquery or CTE to define it.
If your DBMS doesn't have a window function supports then you can use subquery instead
select order_id, Order_quantity,
(select t1.Order_quantity * t2.Order_price
from table2 t2
where t2.Order_id = t1.Order_id) as Order_amount,
(select sum(t1.Order_quantity * t2.Order_price)
from table2 t2
where t2.Order_id = t1.Order_id) as Total_Sales
from table1 t1;

SQL Multiple INNER JOINS In One Select-Statement

I am using this code for inventory management system, in which i want to retrieve stock in hand from four tables. i have tried with two table and got accurate result as i need it.please help me out.
Table Schema
Productmastertb
prod_id,
Product_name
salesdetailstb
sales_id,
Prod_id,
Prod_qty
estimatedetailstb
est_id,
Prod_id,
Prod_qty
Purchasedetailstb
est_id,
Prod_id,
Prod_qty
Query example (working):
SELECT
productmastertb.prod_id,
productmastertb.prod_name,
sum(estimatedetailstd.prod_qty) as Est_qty
FROM
productmaster
INNER JOIN
estimatedetailstb ON productmastertb.prodid = estimatedetails.prodid
GROUP BY
productmastertb.prod_id, productmastertb.prod_name
Similarly I have to retrieve sum of salesdetailstb.qty and purchasedetailstb.qty
Thanks in advance
You want to summarize across different "dimensions" -- that is tables. One good approach is to aggregate before doing the JOINs. Or to use subqueries. Here is the latter approach:
SELECT pm.prod_id, pm.prod_name,
(SELECT SUM(ed.prod_qty)
FROM estimatedetailstb as ed
WHERE ed.prodid = ed.prodidas
) as Est_qty,
(SELECT SUM(sd.prod_qty)
FROM salesdetailstb as sd
WHERE sd.prodid = pm.prodidas
) as Sales_qty,
(SELECT SUM(pd.prod_qty)
FROM purchasedetailstb as pd
WHERE pd.prodid = pm.prodid
) as Sales_qty
FROM productmaster pm;
This will give you all products, even those missing from one or more of the other tables.
You can add multiple joins.
SELECT t1.id, t4.name, count(t4.name)
FROM Table1 AS t1
INNER JOIN Table2 AS t2 -- the AS statement renames the table within
-- this query to t2. Columns from this table can be used
-- as t2.columnname. This needs to be done when you have
-- columns with the same name in different tables.
ON t1.id = t2.id
INNER JOIN Table3 as t3
ON t1.id = t3.id
INNER JOIN Table4 as t4
ON t3.name = t4.name
GROUP BY t1.id, t4.name

INNER JOIN with a large table

I am currently using Microsoft Access 2013, and I am trying to join Table1 and Table2 together, but the problem is that Table2 is massive. Table1 is a list of part, vendor combinations with PK as part, vendor. Table2 is a table I created with the top2 most recent quotes for each part,vendor combination. All these quotes were pulled from a table with PK quote_id. I think my creation of Table2 might be the problem, because I cannot create Table2 with every part,vendor combination (i have to filter out by vendor). This is the query I used for Table2.
a.part, a.vendor, a.quote_date
FROM quoteTable AS a
WHERE a.quote_date > DATEADD("yyyy", -3, DATE()) AND
a.quote_date IN
(SELECT TOP 2 quote_date
FROM quoteTable
WHERE quote_date > DATEADD("yyyy", -3, DATE()) AND
part=a.part AND vendor=a.vendor
ORDER BY quote_date DESC)
If anyone knows a better way to select the top 2 most recent quotes from the table for each part,vendor combination, I would really appreciate it. As for the join, this works but would take too long.
SELECT *
FROM Table1 AS a INNER JOIN Table2 AS b ON a.id = b.id
I am wondering if there was a way I could use the id from Table1 to filter Table2? Something like this:
SELECT *
FROM Table1 AS a INNER JOIN
(SELECT * FROM Table2 WHERE id=a.id) AS b ON a.id = b.id
You definitely could use:
SELECT *
FROM Table1 AS a
INNER JOIN
(SELECT * FROM Table2 WHERE id=a.id) AS b
ON a.id = b.id
Or you could use :
With CTE
as
(
SELECT *
FROM Table2
WHERE id in (select id from table1)
)
SELECT *
FROM Table1 AS a
INNER JOIN
CTE
ON CTE.Id = a.Id
For performance issue, you could try to create index on id column from both tables, or try to limit the selected column from your result.
The real solution is that you need an index on TABLE2.id. I am assuming that it is not the primary key of that table, because it probably is the primary key of TABLE1, and why would you have two tables with the exact same, matching primary keys? It would not be a normalized layout. But then again, there are times when it makes sense to de-normalize.

Join two tables and retrieve relevant data and calculate the result

We have two tables table1 and table2
Table 1
Table 2
We need the resultant table as :
All this should be done in a single SQL query
Thanks in advance.
I think you can make it without the second query, I tested it and returned your expected values.
select table_2.id_pro,
product_name,
SUM(1) as Quantity,
priceprod,
SUM(1) * priceprod as 'quantity * priceprod'
from Table_2
inner join Table_1 t1 on table_2.id_pro = t1.id_pro
group by table_2.id_pro, product_name, priceprod
And my SqlFiddle test http://sqlfiddle.com/#!3/08c2ef/1
I believe this should be what you need, or fairly close anyway! You need to group up your results from your first table to get your quantity value and then join those results to your second table to be able to create your desired output.
SELECT t1.id_pro,
t2.product_name,
s.Quantity,
t2.priceperprod,
s.Quantity * t2.priceperprod
FROM table_2 t2
INNER JOIN (
SELECT COUNT(*) AS Quantity,
t.id_pro
FROM table_1 t
GROUP BY t.id_pro
) t1 ON t2.id_pro = t1.id_pro
i believe that this is correct, i just JOIN the two tables AND used group by then count how many records each group, i hope this will help you, thanks.
SELECT
A.id_pro,
A.product_name,
(count(*)) AS Quantity,
A.priceperprod,
(COUNT(*) * A.priceperprod) AS Total
FROM table_2 A
LEFT JOIN table_1 B
ON B.id_pro = A.id_pro
GROUP BY A.id_pro
Guess this should be helpful to you.
SELECT A.PRODUCT_CODE, A.PRODUCT_NAME, B.QUANTITY,
A.PRICE PRICE_PER_PRODUCT,
(B.QUANTITY * A.PRICE) TOTAL_PRICE FROM
TABLE2 A,
(SELECT X.PRODUCT, COUNT(X.PRODUCT) QUANTITY FROM TABLE1 AS X
GROUP BY X.PRODUCT) as B
WHERE A.PRODUCT_CODE = B.PRODUCT
ORDER BY A.PRODUCT_CODE
Am doing following :
Taking the entire TABLE2 aliased as A
Getting the products and its corresponding count using group by and aliasing it as B
Selecting corresponding fields from table_Aliases A and B provided A's ProductCode and B's Products are same.

SUM function in SQL

There are three tables,we have to select data from these table using one primary key and foreign key. But in the one table there is lot of data in the third table. We have to sum the data on the base of the primary key.
BAl = Balance, met = Method, amo = amount, cst_id, cut_id, cut_i = customer_id
Now we have to sum the on the basis of method and sum for 10 cust id in the same query. Can anyone help me on this?
;WITH cte
AS
(
SELECT
*,
ROW_NUMBER() OVER (ORDER BY t1.cst_id) RowNum
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.cst_id = t2.cut_id
INNER JOIN Table3 t3
ON t2.cut_id = t3.customer_id
AND t2.BAL = t3.Balance
AND t2.amo = t3.amount
)
SELECT SUM(*)
FROM cte
WHERE RowNum Between 1 AND 10
-- You can add a GROUP BY here
If you give some sample data it will be easier to write queries to help you.
But if your MET field is numerical and you want to sum it then you need.
select
t1.cst_n, t2.bal,
sum(t3.met) as met,
sum(t3.amo) as amo
from table1 as t1
inner join table2 as t2 on t2.cut_id = t1.cst_id
inner join table3 as t3 on t3.cut_i = t1.cst_id
group by t1.cst_n, t2.bal
well if you want to sum data for all 10 customers into one number, may be you just need
select
sum(t3.met) as met,
sum(t3.amo) as amo
from table3 as t3
where t3.cut_i in (select t.customerid from #<your variable table with cust. ids> as t)