Collect dates of 2 columns within the same table - mysql

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.

Related

Merge two select statement without ordering

There is a table TableX (id, user_id, sticky_date, created_at)
Few of them entries having sticky dates.
I am trying to get the result as,
result will contain, all the entries having sticky_date order desc and then all remaining entries order by created_at.
I tried with following query,
(SELECT * FROM TableX ORDER BY sticky_expiry_date)
UNION
( select * from TableX order by created_at desc)
But after union created_at is not working as expected. Is there any way to do this stuff in single query.
We can do this using computed columns:
SELECT *
FROM
(
SELECT *, 0 AS position, UNIX_TIMESTAMP(sticky_expiry_date) AS odr
FROM TableX
UNION -- UNION ALL ?
SELECT *, 1, -UNIX_TIMESTAMP(created_at)
FROM TableX
) t
ORDER BY
position, odr;
The idea here is that the position alias keeps track of which half of the union should come first. Then, the odr alias tracks the ordering within each half of the union. The top half uses sticky_expiry_date for the odr alias, while the second half uses created_at. To deal with the issue of ascending/descending date ordering, we can wrap the date columns in UNIX_TIMESTAMP and order by that quantity (ascending), or by that quantity negated (descending).

how to get a distinct list of formatted dates sorted using Mysql

It makes some years since I studied sql so, I am having trouble getting a
get a distinct list of formatted dates sorted using Mysql. I don't need to show my table because I only use one column of datetime
data_vencimento datetime
If I have 2018-10-29 , 2018-10-29, 2018-09-29. It should be sorted as
10/2018
09/2018
notice that the repeated date is "removed" and a sorted list of formatted date was generated
here is my attempt. It is generating repeated results.
select distinct(data_vencimento), date_format( data_vencimento,'%m/%Y' ) as data from (
select data_vencimento from custo_extra_movimento where id_admin
union
select data_vencimento as data from custo_fixo_movimento where id_admin
union
select data_vencimento as data from custo_variavel_movimento where id_admin) as tbl order by data_vencimento desc ;
Distinct is not a function; so you do not need to use parentheses with Distinct.
Nevertheless, you need a Distinct combination of Month and Year, so you can use Group By instead, alongwith date functions like Month() and Year().
Also, in your Union queries, defining data alias for second and third Select query will not serve any purpose. MySQL would consider the first Select query column name only.
Do the following instead:
SELECT
YEAR(tbl.data_vencimento) AS year_data,
MONTH(tbl.data_vencimento) AS month_data,
DATE_FORMAT( MAX(tbl.data_vencimento),'%m/%Y' ) AS data
FROM (
select data_vencimento from custo_extra_movimento where id_admin
union
select data_vencimento from custo_fixo_movimento where id_admin
union
select data_vencimento from custo_variavel_movimento where id_admin
) AS tbl
GROUP BY year_data, month_data
ORDER BY year_data DESC, month_data DESC
I think this is sufficient:
select date_format(data_vencimento, '%m/%Y') as data from custo_extra_movimento where id_admin
union -- on purpose to remove duplicates
select date_format(data_vencimento, '%m/%Y') as data from custo_fixo_movimento where id_admin
union -- on purpose to remove duplicates
select date_format(data_vencimento, '%m/%Y') as data from custo_variavel_movimento where id_admin
order by data desc;
To be honest, I am a little unclear on the logic for the ordering, so that might be off.

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

Get column from 2 tables and sort by date

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

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