Multiple rows in table with values from another table - sql

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

Related

New table from two table with max(timestamp) - Bigquery SQL

I have two tables where the combination of retailer and id are the one common between the two. I need to create a new table for all retailer + id combination from the first table and respective data for those from the second table that has the latest timestamp
The first table will have only one record for each retailer, id combination but the second table will have multiple records for each retailer, id combination based on the time it was scraped, I need to create a new table with the latest timestamp data for each combination
input table 1:
input table 2:
output table:
This is basically aggregation and join:
select *
from table1 t1 left join
(select t2.retailer, max(timestamp) as max_timestamp
from table2 t2
group by t2.retailer
) t2
on using (retailer);
If you wanted the entire most recent row, you can use a variant of this:
select *
from table1 t1 left join
(select ( array_agg(t2 order by timestamp desc limit 1) )[safe_ordinal(1)].*
from table2 t2
group by t2.retailer
) t2
on using (retailer);

How to map each distinct value of a column in one table with each distinct value of a column in another table in Hive

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

Repeat values from table 1 for each value in table 2

I have two tables:
and
I'm trying to create the following output:
How can I do this considering there's nothing to join on? Both of these initial tables are coming from SQL queries and are not a sequence. Using MSSQL.
You can use cross join by 2 ways
SELECT Number,Letter FROM Table1,Table2
Or
SELECT Number,Letter FROM Table1 CROSS JOIN Table2
Demo
http://sqlfiddle.com/#!9/157e3/3

In SQL Server, How to join two table having different column name and different data row?

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

SQL query for determining unique values

I am trying to display corresponding dates for serial number, sometimes the query doesn't display values as the Partobj column doesn't have unique values.
How could I get serial number which have unique Partobj?
select
ib.Date1,w.Date2,w.Date3
from
table1 w
left outer join
table2 ib on w.Partobj=ib.Partobj
where
ib.SerialNumber = '12we'
Without seeing any sample data you could do something similar to this, where you join on table2 looking for only the DISTINCT records:
select ib.Date1,w.Date2,w.Date3
from table1 w
left outer join
(
SELECT DISTINCT Date1, Partobj, SerialNumber
FROM table2
) ib
on w.Partobj=ib.Partobj
where ib.SerialNumber = '12we'
If you post some sample data from both tables, it would be helpful.