SQL - compares column values from two different table - sql

I am new to SQL servers and would like to how should I approach the following case:
I have two tables:
1st table has this month's orders and has the following data
Product
Order
First
2000
Second
3000
... and 10000 rows
and 2nd table has the expected orders for the upcoming month and has the following data:
Product
Order
First
3000
Second
1000
... and 12000 rows.
Now, I want to see the products in the 2nd table where expected orders are more than the values in the Order column in the 1st table.
For eg. the output should in our case should be
| Product |
| First |
Can you please help me how should I approach this as the column names are similar?

you can join them by product name using aliases for example and compare their orders
select eo.product, eo.order from current_orders co
join exp_orders eo on co.product = eo.product
where eo.order < co.order;

You have to use the inner join command, the sintax is like this:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Inner join will give you the rows that are in both tables where the condition on is true, then you add AND for the other condition that you want, something like this:
SELECT Product,Orders
FROM table2
INNER JOIN table1
ON table2.Product = table1.Product AND table2.Product > table1.Product ;

Check the required criteria with exists:
select *
from table2 t2
where exists (
select * from table1 t1
where t1.product = t2.product
and t2.order > t1.order
);

Related

SQL - join multiplying data

I need to produce a synthetic table from 2 tables but one of them is tricky.
First table (sample):
Second table (sample also) :
I want to use it to find the 'store' of each 'pers'
What i want at the end :
(and i want to keep the 'zero' if sum_of_duration is null)
My first code :
SELECT
QRT,
STORE,
sum(DURATION) as SUM_of_DURATION
FROM T1
LEFT JOIN T2
on T1.PERS=T2.PERS
group by QRT, STORE
But my 'Sum_of_durations' are out of the roof because of multiple joins induced by table 2 i guess.
Any help ?
Thanks
Remove duplicates before joining:
SELECT T1.QRT, T2.STORE,
SUM(T1.DURATION) as SUM_of_DURATION
FROM T1 LEFT JOIN
(SELECT DISTINCT QRT, PERS
FROM t2
) t2
ON T1.PERS = T2.PERS
GROUP BY T1.QRT, T2.STORE;

Select a value from a row that is matched to another value in the same table

Here is my issue:
I have the following table (truncated)
ID CustomerNumber ResellerID
1 12 NULL
2 56 1
As part of a Larger query - I'm doing this:
select customernumber,
case when ResellerID = id then customernumber end as 'Parent Account'
from table1
What I'm wanting to get is for rows with a resellerID, match that to the ID table and output the customernumber so the result would look like this:
CustomerNumber Parent Account
12 NULL
56 12
And I can't get the query to play nicely - I've tried running an outer left join but I just get NULL values - so I'm missing some syntax and logic somewhere.
You should use SELF JOIN
Join the same table by making alias with original table's ResellerID column and second table's ID column.
select t1.customernumber, t2.customernumber as 'ParentAccount'
from table1 t1 LEFT JOIN table1 t2 ON t1.ResellerID = t2.ID
do self(Left) join
select t1.CustomerNumber,t2.CustomerNumber as Parent_Account
from table1 t1
left join table1 t2
on t1.ResellerID=t2.id
CustomerNumber Parent_Account
12 NULL
56 12
DEMO IN DB FIDDLE

join single value from one table to multiple rows table - Oracle

I have the following tables in Oracle:
Table 1 Table 2
AllCustomers ProductCode Customers
5200000 ABC 15265
DEF 156890
In Oracle, I want to join them both, like this:
Table 3
ProductCode Customers AllCustomers
ABC 15265 5200000
DEF 156890 5200000
How can I join these tables? As you can see, they do not have a key field to join. I just need to populate a third column in the new table with the same value in it, which would be the one from AllCustomers. Thanks in advance!
Maybe you can try Cross join
SELECT t2.*,t1.*
FROM Table1 t1 CROSS JOIN Table2 t2
You can achieve your goal without a join, like so:
SELECT ProductCode, Customers, (SELECT AllCustomers FROM Table1 WHERE ROWNUM = 1)
FROM Table2

SQL computation between different tables

I have 3 tables like follows:
Table1
BusinessId | CustomerName
Table2
BusinessId | Product Name
Table3
Product Name | Price
and I try to write a query which should count the total cost of every single customer:
SELECT Table1.CustomerName, COUNT(Table2.Product Name) * Table3.Price
FROM Table1, Table2, Table3
WHERE Table1.BusinessId = Table2.BusinessId AND
Table2.Product Name = Table3.Product Name
GROUP BY SERVICE_NAME
But that doesn't work:(
Any help appreciated!
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
The following should be much closer to what you need. It as least fixes a bunch of problems in the query:
SELECT t1.CustomerName, SUM(t3.Price) -- use the right aggregation function
FROM Table1 t1 JOIN
Table2 t2
ON t1.BusinessId = t2.BusinessId JOIN -- column name spelled correctly
Table3 t3
ON t1.BusinessId = t2.BusinessId AND
t2.ProductName = t3.ProductName -- if the column has a space, you need to escape it
GROUP BY t1.CustomerName -- aggregate by the right column
COUNT(Table2.Product Name)
no allowed space here
yor query may look as
SELECT Table1.CustomerName, COUNT(Table2.ProductName) * Table3.Price
FROM Table1, Table2, Table3
WHERE Table1.BusinessId = Table2.BusinessId AND
Table2.ProductName = Table3.ProductName
GROUP BY Table1.CustomerName,Table3.Price

Query With Distinct Value

I have two tables Tabel1 and Table2. My tables look something like this.
Table1 has three fields Cust_Number, sales_org and Orders.
Table 2 has fields by name Cust_Number, sales_org, BU and Dist_Channel.
Dist_Channel is missing in table1 hence I have to get the Distinct of Cust_Number and sales_org from TABLE 2 and then do a join with table1 to get the corresponding BU.
I was able to do it in MS access by creating one additional query to pull the distinct Numbers and then using that query in my final query.
Could anybody give some suggestions on this?
SELECT t1.cust_number, t2.bu, t2.dist_channel
FROM table1 t1, table2 t2
WHERE t1.cust_number = t2.cust_number
AND t1.sale_org = t2.sales_org
AND -- your actual criteria go here
Try this simple version :
SELECT t1.cust_number, t1.sales_org, t2.BU
FROM Tabel1 t1
INNER JOIN Table2 t2 ON t1.cust_number= t2.cust_number AND t1.sales_org = t2.sales_org
You can do it like this:-
SELECT tbl1.cust_number, tbl1.sales_org tbl2.bu, tbl2.dist_channel
FROM Table1 as tbl1, Table2 as tbl2
WHERE tbl1.cust_number = tbl2.cust_number
AND tbl1.sale_org = tbl2.sales_org
......
Cust_Number, sales_org, are common in both the table you can do this by left join
also first check distinct count of Cust_Number and sales_org in table 2 is same or not
SELECT Tabel1.Cust_Number, Tabel1.sales_org, Table2.BU FROM Tabel1 left JOIN Table2 ON Tabel1.Cust_Number = Table2.Cust_Number AND Tabel1.sales_org = Table2.sales_org
Santhosa, as I understand the problem, it's that in Table2, you can have multiple rows that have the same customer number and sales org (but different dist_channels) so that a simple join to the table will multiply the number of rows coming out of Table1, which is not what you want. Instead, you want to use Table2 simply as a look up for the BU for a customer number and sales org, and we are assuming that there is a functional dependency from cust_number,sale_org --> BU, i.e. that for any given cust_number, sale_org pair in Table2, the BU is always the same.
If that assumption is true, then you can do the following:
SELECT tb1.cust_number, tb1.sales_org, tb2.bu
FROM Table1 AS tb1
JOIN (
SELECT DISTINCT cust_number, sales_org, bu
FROM Table2) AS tb2
ON tb1.cust_number = tb2.cust_number
AND tb1.sales_org = tb2.sales_org
But keep in mind that if you have multiple BUs for a given cust_number, sales_org pair, then this will still result in multiple rows being returned for a given cust_number, sales_org pair in Table1.