left join result of two union - mysql

Trying to join those two information and create a new table. My goal is to add create year and month table
For example
t1 is
Year
1990
1991
1992
t2 is
Jan
Feb
Mar
I want all the possible combinations in one table.
1990-Jan
1990-Feb
1990-Mar
1991-Jan
1992-Feb...
thats why i didnt put any conditions on my code.
SELECT b.year
FROM
(
SELECT year from barcelona.births
UNION
SELECT year from barcelona.immigrants_by_age
)
AS b
left join a.month,
FROM
(
SELECT month
from barcelona.unemployment
UNION
SELECT month from barcelona.accidents
)
AS a
;

You can use the following solution:
INSERT INTO new_table_name (create_year, create_month)
SELECT t1.create_year, t2.create_month FROM t1, t2
In case you want to concat the values together you can use the following:
INSERT INTO new_table_name (create_year_month)
SELECT CONCAT_WS('-', t1.create_year, t2.create_month) FROM t1, t2
Using a JOIN (or like on the solution ,) without a condition (without ON) creates every possible combination between the rows of the two tables. By using a INSERT INTO ... SELECT ... you can SELECT and INSERT the selected values in one query into the new table.
demo on dbfiddle.uk
Your query should look like the following:
SELECT b.`year`, a.`month`
FROM (
SELECT `year` FROM barcelona.births
UNION
SELECT `year` FROM barcelona.immigrants_by_age
) AS b, (
SELECT `month` FROM barcelona.unemployment
UNION
SELECT `month` from barcelona.accidents
) AS a

Related

How do i select the most recent datetime from 2 tables

I've searched around but can't find the answer I'm looking for.
I have two tables, each has a date field called last_update_date. I want to search which of the two tables has the most recent date and only return that date.
I tried this query hoping it will order the two tables last_update_date field and return that result but the query does not work. Any help would be appreciated.
"Select last_update_date from Table1, Table2 order by last_update_date DESC Limit 1"
SELECT MAX(last_update_date)
FROM (
SELECT MAX(last_update_date) as last_update_date
FROM Table1
UNION ALL
SELECT MAX(last_update_date)
FROM Table2
) tMax
select * from
( select last_update_date from table1
UNION ALL
select last_update_date from table2
) order by last_update_date;
SELECT MAX(A.last_update_date)
(SELECT last_update_date FROM Table1
UNION ALL
SELECT last_update_date FROM Table2) A;

SQL Find date range gaps in Table

Good day.
I seem to be struggling with what seems like a simple problem.
I have a table that has a value connected to a date (Monthly) for a finite number of ID's
ie. Table1
ID | Date ---| Value
01 | 2015-01 | val1
01 | 2015-02 | val2
02 | 2015-01 | val1
02 | 2015-03 | val2
So ID: 02 does not have a value for date 2015-02.
I would like to return all ID's and Dates that do not have a value.
Date range is: select distinct date from Table1
I can't seem to think outside the realms of selecting and joining on the same table.
I need to include the ID in my select to I can somehow select the ID and Date range that exists for that ID and compare to the entire date range, to get all the dates for each ID that isn't in the "entire" date range.
Please advise.
Thank you
Not very clear about your last two sentences. But you can play with the following query with different #max_days and #min_date:
-- DROP TABLE table1;
CREATE TABLE table1(ID int not null, `date` date not null, value varchar(64) not null);
INSERT table1(ID,`date`,value)
VALUES (1,'2015-01-01','v1'),(1,'2015-01-02','v2'),(2,'2015-01-01','v1'),(2,'2015-01-03','v2'),(4,'2015-01-01','v1'),(4,'2015-01-04','v2');
SELECT * FROM table1;
SET #day=0;
SET #max_days=5;
SET #min_date='2015-01-01';
SELECT i.ID,d.`date`
FROM (SELECT DISTINCT ID FROM table1) i
CROSS JOIN (
SELECT TIMESTAMPADD(DAY,#day,#min_date) AS `date`,#day:=#day+1 AS day_num
FROM table1 WHERE #day<#max_days) d
LEFT JOIN table1 t
ON t.ID=i.ID
AND t.`date`=d.`date`
WHERE t.`date` IS NULL
ORDER BY i.ID,d.`date`;
I now understand your requirement of dates being taken from the table; you want to find any gaps in the date ranges for each id.
This does what you need, but can probably be improved. Explanation below and you can view a working example.
DROP TABLE IF EXISTS Table1;
DROP TABLE IF EXISTS Year_Month_Calendar;
CREATE TABLE Table1 (
id INTEGER
,date CHAR(7)
,value CHAR(4)
);
INSERT INTO Table1
VALUES
(1,'2015-01','val1')
,(1,'2015-02','val2')
,(2,'2015-01','val1')
,(2,'2015-03','val1');
CREATE TABLE Year_Month_Calendar (
date CHAR(10)
);
INSERT INTO Year_Month_Calendar
VALUES
('2015-01')
,('2015-02')
,('2015-03');
SELECT ID_Year_Month.id, ID_Year_Month.date, Table1.id, Table1.date
FROM (
SELECT Distinct_ID.id, Year_Month_Calendar.date
FROM Year_Month_Calendar
CROSS JOIN
( SELECT DISTINCT id FROM Table1 ) AS Distinct_ID
WHERE Year_Month_Calendar.date >= (SELECT MIN(date) FROM Table1 WHERE id=Distinct_ID.ID)
AND Year_Month_Calendar.date <= (SELECT MAX(date) FROM Table1 WHERE id=Distinct_ID.ID)
) AS ID_Year_Month
LEFT JOIN Table1
ON ID_Year_Month.id = Table1.id AND ID_Year_Month.date = Table1.date
-- WHERE Table1.id IS NULL
ORDER BY ID_Year_Month.id, ID_Year_Month.date
Explanation
You need a calendar table which contains all dates (year/months) to cover the data you are querying.
CREATE TABLE Year_Month_Calendar (
date CHAR(10)
);
INSERT INTO Year_Month_Calendar
VALUES
('2015-01')
,('2015-02')
,('2015-03');
The inner select creates a table with all dates between the min and max date for each id.
SELECT Distinct_ID.id, Year_Month_Calendar.date
FROM Year_Month_Calendar
CROSS JOIN
( SELECT DISTINCT id FROM Table1 ) AS Distinct_ID
WHERE Year_Month_Calendar.date >= (SELECT MIN(date) FROM Table1 WHERE id=Distinct_ID.ID)
AND Year_Month_Calendar.date <= (SELECT MAX(date) FROM Table1 WHERE id=Distinct_ID.ID)
This is then LEFT JOINED to the original table to find the missing rows.
If you only want to return the missing row (my query displays the whole table to show how it works), add a WHERE clause to restrict the output to those rows where an id and date is not returned from Table1
Original answer before comments
You can do this without a tally table, since you say
Date range is: select distinct date from Table1
I've slightly changed the field names to avoid reserved words in SQL.
SELECT id_table.ID, date_table.`year_month`, table1.val
FROM (SELECT DISTINCT ID FROM table1) AS id_table
CROSS JOIN
(SELECT DISTINCT `year_month` FROM table1) AS date_table
LEFT JOIN table1
ON table1.ID=id_table.ID AND table1.`year_month` = date_table.`year_month`
ORDER BY id_table.ID
I've not filtered the results, in order to show how the query is working. To return the rows where only where a date is missing, add WHERE table1.year_month IS NULL to the outer query.
SQL Fiddle
You will need a tally table(s) or month/year tables. So you can then generate all of the potential combinations you want to test with. As far as exactly how to use it your example could use some expanding on such as last 12 months, last3 months, etc. but here is an example that might help you understand what you are looking for:
http://rextester.com/ZDQS5259
CREATE TABLE IF NOT EXISTS Tbl (
ID INTEGER
,Date VARCHAR(10)
,Value VARCHAR(10)
);
INSERT INTO Tbl VALUES
(1,'2015-01','val1')
,(1,'2015-02','val2')
,(2,'2015-01','val1')
,(2,'2015-03','val1');
SELECT yr.YearNumber, mn.MonthNumber, i.Id
FROM
(
SELECT 2016 as YearNumber
UNION SELECT 2015
) yr
CROSS JOIN (
SELECT 1 MonthNumber
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
UNION SELECT 10
UNION SELECT 11
UNION SELECT 12
) mn
CROSS JOIN (
SELECT DISTINCT ID
FROM
Tbl
) i
LEFT JOIN Tbl t
ON yr.YearNumber = CAST(LEFT(t.Date,4) as UNSIGNED)
AND mn.MonthNumber = CAST(RIGHT(t.Date,2) AS UNSIGNED)
AND i.ID = t.ID
WHERE
t.ID IS NULL
The basic idea to determine what you don't know is to generate all possible combinations of something could be. E.g. Year X Month X DISTINCT Id and then join back to figure out what is missing.
Probably not the prettiest but this should work.
select distinct c.ID, c.Date, d.Value
from (select a.ID, b.Date
from (select distinct ID from Table1) as a, (select distinct Date from Table1) as b) as c
left outer join Table1 d on (c.ID = d.ID and c.Date = d.Date)
where d.Value is NULL

Combining tables while changing id - SQL

I'm trying to create a search function in different tables using UNION and what happened is that the id's are duplicating making the search go wrong. How can I merge different tables into one while no id's are in common?
Here is the example
table1
id name desc
1 henry post
2 albert doth
3 jun cloth
table2
id name desc
1 kin revenge
2 pot eve
The result SHOULD be like this
id name desc
1 henry post
2 albert doth
3 jun cloth
4 kin revenge
5 pot eve
Please help me. Thanks.
In most databases, you would add a new id using the ANSI standard row_number() function:
select row_number() over (order by which, id) as newid, name, description
from (select 1 as which, t1.* from table1 t1 union all
select 2 as which, t2.* from table2 t2
) t;
Note that desc is a really bad name for a column, because it is a SQL keyword and usually a reserved word.
EDIT:
MySQL doesn't support this ANSI standard functionality. Instead, use variables:
select (#rn := #rn + 1) as newid, name, description
from (select 1 as which, t1.* from table1 t1 union all
select 2 as which, t2.* from table2 t2
) t cross join
(select #rn := 0) vars
order by which, id;
I've include the order by so the rows remain in the same order that you seem to want them in -- rows from the first table followed by rows from the second table. If you don't care about the order, just drop the order by.
For SQLite, the calculation is much more painful:
with cte as (
select 1 as which, t1.* from table1 t1 union all
select 2 as which, t2.* from table2 t2
)
select (select count(*)
from cte cte2
where cte2.which < cte.which or (ct2.which = cte.which and cte2.id <= cte.id
) as id,
name, description
from cte;
In MySql, you can simulate the row_number() function of Sql Server and Oracle using a mutating variable hack:
set #rownum := 0;
SELECT #rownum:=#rownum+1 AS` row_number`, `name`, `desc`
FROM
(
SELECT `name`, `desc` FROM table1
UNION
SELECT `name`, `desc` FROM table2
) AS x;
SqlFiddle
It looks like you have to Generate Id's so you can make you Union query as Sub select and generate Id's in Outer Query
MySQL does not have any system function like SQL Server’s row_number () to generate the row number for each row. However, it can be generated using the variable in the SELECT statement
SET #row_number:=0;
SELECT #row_number:=#row_number+1 As Id,
NAME,
desc
FROM (SELECT NAME,desc
FROM table1
UNION ALL
SELECT NAME,desc
FROM table2
UNION ALL
........
........) A
Order by NAME -- Change the column in Order by in which order you want to create New ID's

repeat result multiple times in mysql

I have a table having id and no field, what I really want is the result raw will be repeated no filed times, if the no field is 2 then that raw must be repeated twice in result.
this is my sample table structure:
id no
1 3
2 2
3 1
now I need to get a result like:
1 3
1 3
1 3
2 2
2 2
3 1
I tried to write mysql query to get the result like above, but failed.
You need a table of numbers to accomplish this. For just three values, this is easy:
select t.id, t.no
from t join
(select 1 as n union all select 2 union all select 3
) n
on t.no <= n.no;
This query must do what you want to achieve:
select t.id, t.no from test t cross join test y where t.id>=y.id
not completely solve your problem, but this one can help
set #i=0;
select
test_table.*
from
test_table
join
(select
#i:=#i+1 as i
from
any_table_with_number_of_rows_greater_than_max_no_of_test_table
where
#i < (select max(no) from test_table)) tmp on no >= i
order by
id desc
EDIT :
This is on SQL Server. I checked online and see that CTEs work on MySQL too. Just couldn't get them to work on SQLFiddle
Try this, remove unwanted columns
create table #temp (id int, no int)
insert into #temp values (1, 2),(2, 3),(3, 5)
select * from #temp
;with cte as
(
select id, no, no-1 nom from #temp
union all
select c.id, c.no, c.nom-1 from cte c inner join #temp t on t.id = c.id and c.nom < t.no and c.nom > 0
)
select * from cte order by 1
drop table #temp

avoiding creation of temporary or permanent tables

I want to manipulate with a table, but the only solution I found so far is to create some tables first and join them together to the desired results.
I'm trying to avoid creating tables and dropping them at the end of my MySQL query which btw, I'm running on phpmyadmin page.
Here is the data: I have one table containing user_id, columnA_unixtime, columnB_unixtime -- meaning that for each user there are two unix_time stored in the database for two different events.
user_id eventA_join eventB_join
1 1321652009 1321652009
2 0 1321652257
3 0 1321668650
4 1321669261 0
what I want to have is a table showing how many users joined the two events for each day. Something like this (just a sample)
day eventA eventB
11/18/11 3 2
11/19/11 11 8
11/20/11 6 3
11/21/11 17 11
Here is the code I'm using so far:
CREATE TABLE table1(
day VARCHAR(256),
eventA_count INT);
INSERT INTO table1 (day, eventA_count)
(SELECT DATE(FROM_UNIXTIME('eventA_join') ) AS 'day', COUNT('user_id') AS 'eventA_count'
FROM org_table
WHERE 'eventA_join' > 0
GROUP BY day);
CREATE TABLE table2(
day VARCHAR(256),
eventB_count INT);
INSERT INTO table2 (day, eventB_count)
(SELECT DATE(FROM_UNIXTIME('eventB_join') ) AS 'day', COUNT('user_id') AS 'eventB_count'
FROM org_table
WHERE 'eventB_join' > 0
GROUP BY day);
SELECT t.day, t1.eventA_count, t2.eventB_count FROM
(SELECT DISTINCT day FROM table1
UNION
SELECT DISTINCT day FROM table2) t
LEFT JOIN table1 t1
ON t.day = t1.day
LEFT JOIN table2 t2
ON t.day = t2.day
DROP table2;
DROP table1;
As far as I tried I couldn't use table variables in phpmyadmin and neither I could use template tables because there was no way to refer to template tables multiple times (error #10327 Can't reopen temporary table) when I try to join them together.
Is there anyway I avoid creating tables but gain what I'm looking for? Any thoughts?
Edit: both tables are getting data from 'org_table' which is now corrected in the code.
This is pretty easy to do with a single query. You just need UNION ALL:
select date, sum(IsEventA) as EventA, sum(isEventB) as EventB
from ((select user_id, DATE(FROM_UNIXTIME('eventA_join') as date,
1 as IsEventA, 0 as IsEventB
from table1
where eventA_join > 0
) union all
(select user_id, DATE(FROM_UNIXTIME('eventB_join') as date,
0 as IsEventA, 1 as IsEventB
from table1
where eventB_join > 0
)
) t
group by date
order by 1