Get column from 2 tables and sort by date - mysql

I have 2 tables both containing an event and date column. Is there a way to combine the results of both column's event field into one and sort them by their date field. That way only a single (and combined) event is returned instead of 2.

SELECT event,date FROM table1
UNION
SELECT event,date FROM table2 ORDER BY date
When using UNION you use ORDER by at bottom query it will order marged query
You can't use it except bottom query anyway it should throw an error

SELECT a.event, MAX(a.date) date
FROM
(
SELECT event, date FROM TableA
UNION
SELECT event, date FROM TableB
) a
GROUP BY a.event
ORDER BY a.date DESC

Related

Mysql. how to join two tables from this example?

everybody.
I have two requests.
1 query - show the list of dates with time 22:00 from one table
SELECT DATE_FORMAT(tt.create_time,"%Y-%m-%d 22:00:00") AS DAY,tt.id
FROM tick tt
GROUP BY DATE_FORMAT(tt.create_time,"%Y-%m-%d")
2 query - shows the number of records that have create_time less than the date specified in the query
SELECT COUNT(*) AS count FROM
(SELECT * FROM
(SELECT * FROM tick_history th
WHERE th.create_time < '2019-04-15 22:00:00'
ORDER BY th.id DESC) AS t1
GROUP BY t1.tick_id) AS t2
WHERE t2.state NOT IN (1,4,9) AND t2.queue = 1
Is it possible to somehow combine these two queries to get one column with dates from the first query, and the second column is the number from the second query for each date from the first column?
Ie as if substituted date and calculated the number of the second request..
Is it possible? Help with request please

Collect dates of 2 columns within the same table

I have two date columns in my table and I need a list of all dates which are in the table.
I tried it with the following statement already
SELECT GREATEST(
IFNULL(DATE(record_date), 0),
IFNULL(DATE(edit_date), 0)
) as 'date_val'
FROM TBL_EXAMPLE
GROUP BY date_val
ORDER BY date_val;
With this SQL statement some of the dates getting lost and I don't know why. I just want a complete list of all dates which are in this table (DISTINCT, descending)
This is a job for UNION.
SELECT record_date AS dt FROM TBL_EXAMPLE
UNION
SELECT edit_date AS dt FROM TBL_EXAMPLE
ORDER BY dt DESC;
Union eliminates nulls and duplicates.

Using aliased result of function in where clause

I want to pull specific rows from a table where the date matches a certain date. First I'm converting the date string to date format, here's the query:
SELECT id, str_to_date(candidate.AddDate,"%d/%m/%Y") n FROM candidate WHERE n='2016-01-01';
But I get the error "Unknown column 'n' in WHERE clause"
How do I make the query use the result of str_to_date in the where clause?
You cant use the alias on the same level, because isnt created at that time
SELECT id,
Str_to_date(candidate.adddate, "%d/%m/%y") n
FROM candidate
WHERE Str_to_date(candidate.adddate, "%d/%m/%y") = '2016-01-01';
Or create a subquery
SELECT *
FROM (
SELECT id,
Str_to_date(candidate.adddate, "%d/%m/%y") n
FROM candidate
) T
WHERE n = '2016-01-01';
I dont know if this is what you are trying to achieve.
SELECT id, adddate from candidate C where C.adddate = "2016-01-01"
Why cant you pull all the table rows where the given date is 2016-01-01. Is this what you want? Or something else. If you have stored the date as date field you dont really need to do str_to_time.
If it is stored as string then
SELECT * FROM ( SELECT id, DATE_FORMAT(STR_TO_DATE(candidate.adddate, '%d/%m/%Y') x FROM candidate
) C WHERE x = '2016-01-01';

combining a temp table with union query

So I have a query that I am working on. It will have ship, order, and return data in it by date and unit type.
I have each sub query working so they come together like this:
Select dateproc,unit,totqty,type(ship)
union
Select dateproc,unit,totqty,type(order)
union
Select dateproc,unit,totqty,type(return)
Now not every date will have something in it, but I want to represent that date whether or not it has any date, for instance weekends.
So the same query has code to create a temp table called #dates with the field lookupdate which has been populated with each day of the current month.
I need to join the combined union queries with this table so that I have a record for each day of the month.
Perhaps easiest to read if you use a common table expression (CTE) for your union query and the join it to your dates.
;WITH cte AS (
Select dateproc,unit,totqty,type(ship) type_total
union
Select dateproc,unit,totqty,type(order)
union
Select dateproc,unit,totqty,type(return)
)
SELECT *
FROM #dates a
LEFT JOIN
cte b ON a.date = b.dateproc

Select count from multiple tables and group by one field

I have "users" table with fields
user_name, user_id
I have data tables like
data_table_2012_10
data_table_2012_11
data_table_2012_12
data_table_2013_01
data_table_2013_02
each table contains the following fields
user_id, type ('ALARM', 'EMERGENCY', 'ALIVE', 'DEAD'), date_time
There will be millions of records in each table.
I have to select the count of type from the data_tables within the time frame given by the user, as well as have to get the corresponding name of the user with the help of user_id.
Can some one help me out with the best solution.
Try this query where DATE1 and DATE2 is your date range. You should union all tables in the inner query. Also you can try to make a query dynamically to include in the inner query only those tables that are in a date range you use:
select t.user_id,t.type, MAX(users.user_name), SUM(t.cnt)
from
(
select user_id,type,count(*) cnt
from data_table_2012_10 where date_time between DATE1 and DATE2
group by user_id,type
union all
select user_id,type,count(*) cnt
from data_table_2012_11 where date_time between DATE1 and DATE2
group by user_id,type
union all
.........................................
union all
select user_id,type,count(*) cnt
from data_table_2013_02 where date_time between DATE1 and DATE2
group by user_id,type
) t
left join users on (t.user_id=users.user_id)
group by t.user_id,t.type
Remember not to use UNION, but UNION ALL as UNION will return only merge similar rows into one and that may cause problem