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
Related
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
);
I am struggling with following issue:
Table1:
Table2:
Expected result:
Basically I want to multiple rows in dates table with rows from User table. Is it somehow possible? (using TSQL).
You need to apply cross join
select date,month,userid,userno from table1 cross join table2
select Date, Month, USER_ID, ID from
t1 cross join t2
I have two tables in Hive, Table1 and Table2. I want to get each distinct customerID in Table1 and map it to each distinct value in a column called category of Table2. However I am a bit lost on how to do this in hive. A better example of what I am trying to do is the following: Let's say Table1 contains 5 distinct customerID's and Table2 contains 3 distinct categories. I want my query result to look something like the following:
However Table1 and Table2 do not have any columns in common so I am a bit lost on how to perform a join on this two tables in hive. Is this task possible in hive? Any insights on this would be greatly appreciated!
You can do that with a cross join of distinct values from both tables.
select t1.customerid,t2.categories
from (select distinct customerid from tbl1) t1
cross join (select distinct categories from tbl2) t2
Suppose I have two tables T1 & T2. I want resulting output table as O1 with help of a SQL query
T1 {SName}
T2 {VName}
O1 {SName, VName}
It seems like you want a cross join of the two tables to include all combinations of rows in T1 and T2. Try this:
Select SName, VName From T1 Inner Join T2 On 1=1
The number of rows you will get is the product of the number of rows in T1 and T2 each.
if you're not joining on anything and want a table of all possible combinations:
select SName, VName
into O1
from T1 cross join T2
if you have a column to join on:
select SName, VName
into O1
from T1 inner join T2 on T1.col = T2.col
Select record from T1 and T2 based on filtering criteria and then insert record in table O1 , use below query to create table O1 and inserting those records.
INSERT INTO O1(SNAME, VNAME)
SELECT(T1.SNAME, T2.VNAME)
From T1 Inner Join T2 On 1=1 //i.e WHERE T1.id=T2.T1_id
use WHERE to filter records
there are several ways of doing it.one easy way is:
select T1.SName,T2.VName from T1,T2;
i don't know if i am right, you can use cross join.
if you have two tables Players and GameScores then
SELECT* INTO O1 FROM GameScores CROSS JOIN Players will return all records where each row from the first table is combined with each row from the second table. Which also mean CROSS JOIN returns the Cartesian product of the sets of rows from the joined tables.
to get the above result as
T1 {SName}
T2 {VName}
O1 {SName, VName}
(SELECT * FROM T1,T2) as O1;
will definitely work if both the table have single value if not then you can select the
Particular column like T1.SName,T2.VName
I have several tables that I am joining that I need to add another table to and I can't seem to get the right query. Here is what I have now -
Table 1
carid, catid, makeid, modelid, caryear
Table 2
makeid, makename
Table 3
modelid, modelname
Table 4
catid, catname
The query I am using to join these is:
SELECT * FROM table1 a
JOIN table2 b on a.makeid=b.makeid
JOIN table3 c on a.modelid=c.modelid
JOIN table4 d on a.catid=d.catid
WHERE a.carid = $carid;
Now I need to add a 5th table that I am getting from a 3rd party that I am having a hard time adding to my existing query. The new table has these fields -
Table 5
id, year, make, model, citympg, hwympg
I need the citympg and hwympg based on caryear from table 1, makename from table 2, and modelname from table 3. I know I can do a second query with those values, but I would prefer to do a single query and have all of the data in a single row. Can this be done in a single query? If so, how?
it's possible to have more than condition in a join.
does this work?
SELECT a.*, e.citympg, e.hwympg
FROM table1 a
JOIN table2 b on a.makeid=b.makeid
JOIN table3 c on a.modelid=c.modelid
JOIN table4 d on a.catid=d.catid
Join table5 e on b.makename = e.make
and c.modelname = e.model
and a.caryear = e.year
WHERE a.carid = $carid;
...though your question is not clear. Did you only want to join table5 to the others, or was there something else you wanted to do with table5?
Without indexes, It won't be efficient, but you can do
LEFT JOIN table5 ON (table2.make = table5.make AND table3.model = table5.model AND table1.caryear = table5.caryear)
This also assumes the make and models and years strings match exactly.