Mysql Union Query Record result emptys column - mysql

I have a union query to get a Max date record from two tables
SELECT
id,
MAX(mx) AS max_date,
`assigned_user_id`
FROM (SELECT
id,
MAX(date_due) AS mx,
`assigned_user_id`
FROM tasks
WHERE `deleted` = 0
AND `parent_id` = 'aa6f0330-7a17-572f-94b5-4a5db1a1201f'
AND `parent_type` = 'Accounts'
AND date_due >= CURDATE() UNION SELECT
id,
MAX(date_start) AS mx,
`assigned_user_id`
FROM calls
WHERE `deleted` = 0
AND `account_id` = 'aa6f0330-7a17-572f-94b5-4a5db1a1201f'
AND date_start >= CURDATE()) s
this would look into both calls and Tasks and get me the Futuristic Call or a Task, but when i run this and i have a Futuristic Call and task is older, i get NULL in id and assigned_user_id columns. How do i fetch the whole info for the MAX date record.
When i Run individual Queries, i get null for first one while a record in the second one.

Perhaps I'm misreading the problem, or I'm missing something - in which case, write me a comment to let me know, and I can delete this post altogether.
But, the way I understand what you've written, you want to:
Get the single record in tasks with the given parent_id and parent_type and where date_due >= CURDATE() which has the latest date_due value.
Get that same type of record from the calls table (using date_start instead of date_due).
Of those two records, find the one record with the later date value. Select the id, the date (as max_date), and the assigned_user_id from that record.
Your query (and sub-queries) could just order by descending date and fetch only the first record.
I'm thinking something like this...
SELECT id,
mx AS max_date,
`assigned_user_id`
FROM (
SELECT id,
date_due as mx,
`assigned_user_id`
FROM tasks
WHERE `deleted` = 0
AND `parent_id` = 'aa6f0330-7a17-572f-94b5-4a5db1a1201f'
AND `parent_type` = 'Accounts'
AND date_due >= CURDATE()
ORDER BY date_due DESC
LIMIT 1
UNION
SELECT id,
date_start as mx,
`assigned_user_id`
FROM calls
WHERE `deleted` = 0
AND `account_id` = 'aa6f0330-7a17-572f-94b5-4a5db1a1201f'
AND date_start >= CURDATE()
ORDER BY date_start DESC
LIMIT 1
)
ORDER BY max_date DESC
LIMIT 1

Use UNION ALL. To allow duplicate values, use the ALL keyword with UNION.

Select Max(max_date) from
(SELECT
id,
MAX(mx) AS max_date,
assigned_user_id
FROM
(SELECT
id,
MAX(date_due) AS mx,
assigned_user_id
FROM tasks
WHERE deleted = 0
AND parent_id = 'aa6f0330-7a17-572f-94b5-4a5db1a1201f'
AND parent_type = 'Accounts'
AND date_due >= CURDATE() group by id UNION SELECT
id,
MAX(date_start) AS mx,
assigned_user_id
FROM calls
WHERE deleted = 0
AND account_id = 'aa6f0330-7a17-572f-94b5-4a5db1a1201f'
AND date_start >= CURDATE()) s group by id)
as temp

Related

Getting all orders, where the last order ist older than a year in mySql

I have a mySQL table, which holds:
CustomerId and OrderDate
There can me multiple rows for one CustomerId
Now, I try to get the CustomerId's where only the last OrderDate is older than a year.
I try the following:
SELECT *
FROM order
WHERE OrderDate <=DATE_SUB(now(), Interval 1 Year)
GROUP BY CustomerId
ORDER BY OrderDate DESCC;
The problem here is, that I get all the rows, which are older then 1 year.
But as I said above, I try to get only the latest order, which is older than 1 year.
THX for any advise
Order the rows and limit to the last. Also, you had DESCC instead of DESC.
SELECT *
FROM order
WHERE OrderDate <=DATE_SUB(now(), Interval 1 Year)
GROUP BY CustomerId
ORDER BY OrderDate DESC
LIMIT 1;
You might also try this query:
SELECT
`CustomerId`,
`CustomerName`, // Add other fields you want returned.
MAX(`OrderDate`)
FROM `order`
WHERE `OrderDate` <= DATE_SUB(now(), Interval 1 Year)
GROUP BY `CustomerId`
ORDER BY MAX(`OrderDate`) DESC;
Also, this will return all of the related columns in the last order for each customer:
SELECT *
FROM `order` a
JOIN (
SELECT
`CustomerId`,
MAX(`OrderDate`) as `maxdate`
FROM `order`
WHERE `OrderDate` <= DATE_SUB(now(), Interval 1 Year)
GROUP BY `CustomerId`) b
ON a.`CustomerId` = b.`CustomerId` AND
a.`OrderDate` = b.`maxdate`
ORDER BY `maxdate` DESC;
Try this subquery:
select customer_id
from customer table
where order_id in(Select order_id from (select order_id from order_table (year(now())-year(order_date)) = 1 order by order_date desc limit 1))
if it doesn't work please post your table structure.
THX for all your tips.
At the end, I found my working solution:
SELECT
*
FROM order a1
INNER JOIN (SELECT
order.Id
FROM (SELECT
*
FROM (SELECT
*
FROM order
WHERE OrderDate <= DATE_SUB(NOW(), INTERVAL 1 year)
ORDER BY OrderDate DESC) AS Sub
GROUP BY Sub.CustomerId) AS a2) AS a3
ON a1.id = a3.id;

Using column data from another table for the where clause of two joined tables

I basically have three tables, trans, master, and period. I wanted to display data from trans and master, but use the data from period for the where clause.
This is my current query, though it returned an error 1064 from on the third line:
SELECT b.projname AS companies,SUM(otreg_h) AS hours, SUM(otreg_m) AS minutes FROM trans AS a
JOIN MASTER AS b ON a.empno = b.empno
WHERE otreg_h != 0 AND otreg_m != 0 AND pdate BETWEEN STRDate IN(SELECT STRDate FROM period) AND ENDDate IN (SELECT ENDDate FROM period)
GROUP BY b.projname
ORDER BY SUM(otreg_h) DESC LIMIT 15
What did I do wrong here?
EDIT: This is the current structure of table 'period'
PayCat - varchar(1) NOT NULL
PayMonth - int(10) NULL
PeriodNo - int(10) NULL
(Foreign key)STRDate - date NOT NULL
(Foreign key)ENDDate - date NOT NULL
PAYDate - date NULL
Processed - tinyint(3) NOT NULL
Posted - tinyint(3) NOT NULL
Pyear - varchar(50) NULL
id - bigint(10) NOT NULL
date1 - date NULL
date2 - date NULL
I'm a little hesitant to supply an answer here since I am not sure there is a correct answer but given the comment that the pdate should be between STRDate and ENDDate from period and nothing else then the following comparison would do
pdate BETWEEN (SELECT MIN(STRDate) FROM period) AND (SELECT MAX(ENDDate) FROM period)
So the full query would then be
SELECT b.projname as companies, sum(otreg_h) as hours, sum(otreg_m) as minutes
FROM trans a
JOIN master b on a.empno=b.empno
WHERE otreg_h != 0 AND otreg_m != 0
AND pdate BETWEEN (SELECT MIN(STRDate) FROM period) AND (SELECT MAX(ENDDate) FROM period)
GROUP BY b.projname
ORDER BY hours DESC LIMIT 15
You have used strdate 2 times
SELECT
b.projname AS companies,
SUM(otreg_h) AS hours,
SUM(otreg_m) AS minutes
FROM
trans AS a
JOIN MASTER AS b ON a.empno = b.empno
WHERE
otreg_h != 0
AND otreg_m != 0
AND pdate BETWEEN (
SELECT
STRDate
FROM
period
)
AND ENDDate IN (
SELECT
ENDDate
FROM
period
)
GROUP BY
b.projname
ORDER BY
SUM(otreg_h) DESC
LIMIT
15
SELECT b.projname AS companies,SUM(otreg_h) AS hours, SUM(otreg_m) AS minutes FROM trans AS a
JOIN MASTER AS b ON a.empno = b.empno
WHERE otreg_h != 0 AND otreg_m != 0 AND pdate BETWEEN (STRDate IN(SELECT STRDate FROM period)) AND (ENDDate IN (SELECT ENDDate FROM period))
GROUP BY b.projname
ORDER BY SUM(otreg_h) DESC LIMIT 15

Need help in executing SQL query

Here are two queries with results, I need to run one query to get same result.
1-Total slots
2-created Date
3-start date
4-end date
5-Unused Slots
First Query:-
SELECT COUNT( id ) as total_slots ,
created_date, MIN( DATE ) as start_date ,
MAX( DATE ) as end_date
FROM slots
GROUP BY created_date;
Query 1(Result)
Here is image with query result
Can I get unused slots in same query as I am getting from below query?
But here
SELECT COUNT( id ) AS unused
FROM slots
WHERE user_id =0
AND created_date = '2016-10-01 20:20:20'
Result Query with created date 2016-10-01 20:20:20
unused
79
SELECT COUNT( id ) AS unused
FROM slots
WHERE user_id =0
AND created_date = '2016-10-01 20:24:45'
Result Query with created date 2016-10-01 20:24:45
unused
51
Try
SELECT
COUNT( id ) as total_slots,
created_date,
MIN( DATE ) as start_date,
MAX( DATE ) as end_date,
COUNT(CASE WHEN user_id = 0 THEN 1 END) as unused_slots
FROM slots
GROUP BY created_date;

Mysql UNION Tables MAX date

I have two tables calls and tasks. I want to get the farthest date which ever is from both tables. The result will be one date that is the Maximum of all.
So basically want to join the quires below
SELECT id, MAX(date_due) FROM tasks WHERE
parent_id = '35'
AND date_due > CURDATE()
SELECT id, MAX(date_start) FROM calls WHERE
parent_id = '35'
AND date_start > CURDATE()
These will result the max date from each table but how do i get single record which is the Highest Date.
If you want the greatest value from MAX(date_due) and MAX(date_start)
add an alias to your MAX function
use an UNION between your two queries
use that UNIONed query as a subquery.
select id, MAX(mx) FROM
(SELECT id, MAX(date_due) as mx FROM tasks WHERE parent_id = '35' AND date_due > CURDATE()
UNION
SELECT id, MAX(date_start) as mx FROM calls WHERE parent_id = '35' AND date_start > CURDATE()) s

mysql: select between

I am getting invalid use of group function, not really sure where the problem is
expected result is list of timestamps from within xxx seconds starting from the max available
Please advise.
SELECT timestamp, response_time
FROM results
WHERE id = XYZ AND timestamp between
(SELECT MAX(timestamp) FROM results inn WHERE id = 22) AND
(SELECT timestamp FROM results WHERE id =22 AND timestamp = MAX(timestamp) - XXX)
ORDER BY timestamp DESC
thank you
SELECT timestamp, response_time
FROM results
WHERE id = #xyz
AND timestamp BETWEEN
(
SELECT MAX(timestamp)
FROM results
WHERE id = 22
) AND
(
SELECT MAX(timestamp)
FROM results
WHERE id = 22
) - #xxx
ORDER BY
timestamp DESC
Make sure you have an index on (id, timestamp) for this to work fast.