Repeat values from table 1 for each value in table 2 - sql

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

Related

I need help joining these two datasets to produce the results on this exercise

I'm trying to join table 1 and table 2 to create table 3 with using SQL. How would I be able to do this?
you need a join, count and group by
select a.region, b.partnerType, count(*)
from table1 a
inner join table2 b on a.DistrectID = b.DistrectID
group by a.region, b.partnerType

Multiple rows in table with values from another table

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

How to take distinct values in hive join

I need to take the distinct values from Table 2 while joining with Table 1 in Hive. Because the table 2 has duplicate records.
Considering below join condition is it possible to take only distinct key_col from table 2? i dont want to use select distinct * from ...
select * from Table_1 a left join Table_2 b on a.key_col = b.key_col
Note: This is in Hive
Use Left semi join. This will give you all the record in table1 which exist in table2(duplicate record) without duplicates.
select a.* from Table_1 a left semi join Table_2 b on a.key_col = b.key_col

How to summarize two tables in teradata

I have the following two tables
I need to create a table that would summarize points for each date
How can I do this . I have updraded to Teradata 14 . And I am not quite familiar with all the new functions
If table1 is actually only a few rows you don't need any fancy query.
Assuming table1.caseid is a byteint this will result in a product join:
Select t2.datex, t2.caseid, sum(t1.points)
from table1 as t1 join table2 a t2
on position(trim(t1.caseid) in t2.caseid) > 0
group by 1,2
Of course if this was a properly normalized table you could simple use a t1.caseid = t2.caseid join instead.

SQL I want to duplicate record on insert

Without using a while or forloop, is there a way to insert a record two or more times on a single insert?
Thanks
INSERT INTO TABLE2 ((VALUE,VALUE)
SELECT VALUE,VALUE FROM TABLE1 )) * 2
You would need to CROSS JOIN onto a table with 2 rows. The following would work in SQL Server.
INSERT INTO TABLE2 ((VALUE,VALUE)
SELECT VALUE,VALUE
FROM TABLE1, (SELECT 1 UNION ALL SELECT 2) T(C)
If you have an auxilliary numbers table you could also do
SELECT VALUE,VALUE
FROM TABLE1 JOIN Numbers ON N <=2
--first create a dummy table with 2 records
INSERT INTO TABLE2 ((VALUE,VALUE)
SELECT VALUE,VALUE FROM TABLE1, dummytable ))
This is not an elegant way, but could work easily.
If you have a table with an high enough number of records you can do the cross join with a TOP clause
INSERT INTO TABLE2
SELECT VALUE,VALUE FROM TABLE1
cross join (select top 2 TABLE_DUMMY) as DUMMY
This works for MQ SqlServer, to let it work in other DBMS you should change the TOP with the keyword needed by your DBMS