Join two tables and retrieve relevant data and calculate the result - sql

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.

Related

How can I divide two columns and show results per row?

I would not be able to do what I want :(, I explain:
I have two tables:
Table1
Table2
I am looking for a query where I can get FALSE/(FALSE+TRUE).
So far I managed to do it over the total using this:
select
(select count(*) from (select Drink from Table1
where Drink=False)) as F,
(select count(*) from (select Drink from Table1
where Drink=True)) as T,
(F/(F+T)) as total
With that I can get the total of the column I want, I just have to change, if I want, Drink-->Food.
But now I would like the final result by Country, that is, something similar to this:
Expected result
In order not to break the forum rules, I'll leave what I've tried so far:
select * from (select distinct t2.Country_Name, count(t1.Drink)
from Table 1 t1
left join Table 2 t2 on t2.Country_ID=t1.Country_ID
where Drink=False
group by t2.Country_Name) as F
union
select * from (select distinct t2.Country_Name, count(t1.Drink)
from Table 1 t1
left join Table 2 t2 on t2.Country_ID=t1.Country_ID
where Drink=True
group by t2.Country_Name) as T
But it doesn't even bring me what I expect :(
I don't know if it helps much, but I'm using Snowflake
Any help is welcome. Thank you!
The first query could be simpliefied using COUNT_IF:
SELECT COUNT_IF(Drink=False)/COUNT(*)
FROM Table1;
Grouped by Country:
SELECT t2.Country_Name, COUNT_IF(t1.Drink=False)/COUNT(*)
FROM Table1 AS t1
JOIN Table2 AS t2
ON t2.Country_ID=t1.Country_ID
GROUP BY t2.Country_Name;

merge multiple tables in Hive 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;

Join table and pick rows where for given id exists only one value

I don't know, if I made good title, but please let me visualize this.
So I have two tables and for given case I need to select row where payment currency was ONLY in EUR.
Correct document Id's will be: 2, 3, 4, 5
These are overall bigger tables with 900k+ records.
Can you please suggest me how query should look?
use correlated subquery with not exists
select distinct a.document_id from tablename a inner join tablename b b on a.document_id=b.payment_docid
where not exists
(select 1 from tablename b1 where b1.payment_docid=b.payment_docid and currency<>'EUR')
Try this query:
select payment_docId from MyTable
group by payment_docId
having max(currency) = 'EUR'
and min(currency) = 'EUR'
or you could use having count(*) = 1 with min or max as well.
use corelated subquery
select t1.* from table2 as t1
where exists( select 1 from table2 t2 where t1.payment_docid=t2.payment_docid
having count(distinct currency)=1)
and currency='EUR'
It is possible to use INNER JOIN with the following conditions to get all rows:
SELECT
pd.payment_doc_id
, pd.currency
FROM DocTable dt
INNER JOIN PaymentDocs pd
ON dt.document_id = pd.payment_doc_id AND pd.currency IN ('EUR')
If you want distinct rows, then you can apply operator GROUP BY:
SELECT
pd.payment_doc_id
, pd.currency
FROM DocTable dt
INNER JOIN PaymentDocs pd
ON dt.document_id = pd.payment_doc_id AND pd.currency IN ('EUR')
GROUP BY pd.payment_doc_id
, pd.currency
Aggregation is the only efficient want :
select doc_id
from table t
group by doc_id
having min(currency) = max(currency) and min(currency) = 'EUR';

How to fetch data from two different tables having equal value of sum for same sales id?

I have the following two tables:
saleid is the column in common. I can individually obtain the sum of the totals for each table:
select saleid, sum(quantity)
from table1
group by saleid;
select saleid, sum(extracted)
from table2
group by saleid;
I need to compare the sum of extracted, in table 2, with the sum of quantity in table 1 for each sale
What tool of SQL Server or SQL could I use to join the outputs of the sum query and get something like this:
Try below query with inner join and subquery:
select a.saleid, quantity ,extracted from
(select saleid, sum(quantity) as quantity from table1 group by saleid)a
inner join
(select saleid, sum(extracted) as extracted from table2 group by saleid)b
on a.saleid=b.saleid
group by a.saleid
Assuming saleid is same in both the tables, you can join both the tables over the column and get your data via the below query
select t1.saleid,
sum(t1.quantity) ,
sum(t2.extracted)
from table1 t1 join table2 t2
on t1.saleid = t2.saleid
group by t1.saleid;
Try this :
select t1.saleid, t1.quantitySum, t2.extractedSum from
(Select saleid, sum(quantity) as quantitySum from table1 group by saleid)t1
inner join (Select saleid, sum(extracted) as extractedSum from table2 group by saleid)t2 on t1.saleid= t2.saleid and t1.quantitySum=t2.extractedSum ;

Joining tables + SUM & GROUP BY function

I'm struggling in joining tables together with SUM and GROUP BY function. The query below works fine:
select ID, sum(amount)
from table1
group by ID
having sum(amount) between 1000 and 10000
As table1 only includes customer ID, I also need to join table CUSTOMERS, which contain customer name (column NAME). Following query will not work for me anymore:
select ID, name, sum(amount)
from table1
left join customers on table1.ID = customers.ID2
group by ID
having sum(amount) between 1000 and 10000
Ditching SUM and GROUP BY functionality does "fix" the issue as also column NAME will be available in the result, however I still need to sum and group the AMOUNT based on ID. How should I join the other table in this case to also present field NAME from table CUSTOMERS?
Column NAME or expression in SELECT list not valid'
is currently given as error message.
It needs to be in the group by:
select t1.ID, c.name, sum(t1.amount)
from table1 t1 left join
customers c
on t1.ID = c.ID2
group by t1.ID, c.name;
Note the use of table aliases.
Add "name" in group by clause
select table1.ID, customers.name, sum(table1.amount) amount
from table1,customers on table1.ID = customers.ID2
group by table1.ID,customers.name
try it
select t.ID, c.name, sum(t.amount)
from table1 t
left join customers c on table1.ID = customers.ID2
group by t.ID, c.name
having sum(t.amount) between 1000 and 10000
or without having depends on your requirement