I have tables that contain same field, for example:
p_central_ticket p_south_ticket p_west_ticket
=====================================================================
- t_id - t_id - t_id
- t_open_by - t_open_by - t_open_by
- t_closed_by - t_closed_by - t_closed_by
- t_open_time - t_open_time - t_open_time
- t_closed_time - t_closed_time - t_closed_time
One thing that i expect is output just like this, but definitely for 3 table above in single query:
Name today weekly monthly yearly
=================================================================
test1#random.com 2 10 70 1000
test2#random.com 5 14 60 1234
But, my query right now just for calculate 1 table.
SELECT t_closed_by As Username, ixt_user_type.user_owner As Role,
COUNT( case when t_closed_time > curdate() - interval 1 day THEN 1 END ) as today,
COUNT( case when t_closed_time > curdate() - interval 7 day THEN 1 END ) as weekly,
COUNT( case when t_closed_time > curdate() - interval 1 month THEN 1 END ) as monthly,
COUNT( case when t_closed_time > curdate() - interval 1 year THEN 1 END ) as yearly
FROM p_central_ticket
LEFT JOIN m_event_type ON p_central_ticket.t_req_type = m_event_type.ev_type
LEFT JOIN ixt_user_type ON m_event_type.ev_user_type_target = ixt_user_type.user_type
WHERE t_status = 9
GROUP BY t_closed_by;
My question is, how should i do, to make my query calculate from 3 tables but in single query ?
You can use UNION to merge three tables and apply all your join on that merged table like below -
SELECT t_closed_by As Username, ixt_user_type.user_owner As Role,
COUNT( case when t_closed_time > curdate() - interval 1 day THEN 1 END ) as today,
COUNT( case when t_closed_time > curdate() - interval 7 day THEN 1 END ) as weekly,
COUNT( case when t_closed_time > curdate() - interval 1 month THEN 1 END ) as monthly,
COUNT( case when t_closed_time > curdate() - interval 1 year THEN 1 END ) as yearly
FROM
( select * from p_central_ticket
union
select * from p_south_ticket
union
select * from p_west_ticket
)A
LEFT JOIN m_event_type ON A.t_req_type = m_event_type.ev_type
LEFT JOIN ixt_user_type ON m_event_type.ev_user_type_target = ixt_user_type.user_type
WHERE t_status = 9
GROUP BY t_closed_by
Related
i have query
select a.`2021`,
b.`2022`,
a.product,
concat(ceil((b.`2022`-a.`2021`)/ a.`2021` * 100), '%') as growth
from ( SELECT SUM(total) as `2021`,
product,
sum
FROM table
WHERE YEAR(month) = 2021
AND case when day(CURRENT_DATE()) > 10
then QUARTER(month) = QUARTER(CURRENT_DATE() - INTERVAL 1 MONTH)
else QUARTER(month) = QUARTER(CURRENT_DATE() - INTERVAL 3 MONTH)
end
GROUP BY Product ,
YEAR(month) )a
JOIN ( SELECT SUM(total) as `2022`,
Product
FROM table
WHERE YEAR(month) = 2022
AND case when day(CURRENT_DATE()) > 10
then QUARTER(month) = QUARTER(CURRENT_DATE() - INTERVAL 1 MONTH)
else QUARTER(month) = QUARTER(CURRENT_DATE() - INTERVAL 3 MONTH)
end
GROUP BY Product ,
YEAR(month) ) b on a.Product=b.Product;
if the current date is not the end of the quarter then the data that appears is the data in the previous quarter period
I have table just like this :
p_central_ticket
================
- t_id ======> id ticket
- t_open_by ======> name that raise the ticket
- t_closed_by ======> name that closed the ticket
- t_open_time ======> open ticket time
- t_closed_time ======> closed ticket time
How should i do, if i want to show the Count all tickets that closed by name, and closed time today, weekly, monthly and yearly ? Just like this :
Name today weekly monthly yearly
=================================================================
test1#random.com 2 10 70 1000
test2#random.com 5 14 60 1234
Sample data :
t_id t_open_by t_closed_by t_open_time t_closed_time
===========================================================================
1 amir#random.com test1#random.com 2018-03-28 2018-03-29
2 tiki#random.com test1#random.com 2018-04-28 2018-05-29
Need Help guys...
Thanks
Something like that:
SELECT `t_closed_by`,
COUNT( case when `t_closed_time` > curdate() - interval '1' day THEN 1 END ) as today,
COUNT( case when `t_closed_time` > curdate() - interval '7' day THEN 1 END ) as weekly,
COUNT( case when `t_closed_time` > curdate() - interval '1' month THEN 1 END ) as monthly,
COUNT( case when `t_closed_time` > curdate() - interval '1' year THEN 1 END ) as yearly
FROM Table1
GROUP BY `t_closed_by`
Demo: http://sqlfiddle.com/#!9/3d792/11
Something like the below can be used in MySQL: (Note please check the Syntax if there is any error.)
SELECT a1.t_closed_by as Name,
(SELECT COUNT(*) FROM p_central_ticket WHERE t_closed_time = CURDATE() and t_closed_by = a1.t_closed_by) as today,
(SELECT COUNT(*) FROM p_central_ticket WHERE t_closed_time > DATE_SUB(CURDATE(), INTERVAL 7 DAY) and t_closed_by = a1.t_closed_by) as weekly,
(SELECT COUNT(*) FROM p_central_ticket WHERE t_closed_time > DATE_SUB(CURDATE(), INTERVAL 30 DAY) and t_closed_by = a1.t_closed_by) as monthly,
(SELECT COUNT(*) FROM p_central_ticket WHERE t_closed_time > DATE_SUB(CURDATE(), INTERVAL 365 DAY) and t_closed_by = a1.t_closed_by) as yearly
FROM (SELECT DISTINCT t_closed_by FROM p_central_ticket) a1 ;
You can use this to get records from the tables with even minutes time interval
SELECT column1,column2,.... FROM tableName
WHERE
(DateAndTime BETWEEN '2019-02-11 14:00' AND '2019-02-12 22:00')
AND
(DATEPART(MINUTE, DateAndTime) % 60 = 0)
select
pd.state AS StateName,
pd.county AS `County Name`,
CASE pc.close_dt WHEN pc.close_dt >= DATE(NOW() - INTERVAL 3 MONTH)
THEN COUNT(pd.property_id)
ELSE NULL
END AS `3 MONTH`,
CASE pc.close_dt WHEN pc.close_dt >= DATE(NOW() - INTERVAL 6 MONTH)
THEN COUNT(pd.property_id)
ELSE NULL
END AS `6 MONTH`
from resnet.property_details pd
join resnet.property_closings pc
on pd.property_id = pc.Property_id
GROUP BY pd.state,pd.county
I'm trying to get the interval of 3 months from today, 6 months from today for properties that have been closed.
I want it to look like the this:
You need conditional aggregation. In MySQL, you can do this as:
select pd.state AS StateName, pd.county AS `County Name`,
SUM(pc.close_dt >= CURDATE() - INTERVAL 3 MONTH) AS `3 MONTH`,
SUM(pc.close_dt >= CURDATE() - INTERVAL 6 MONTH) AS `6 MONTH`
from resnet.property_details pd join
resnet.property_closings pc
on pd.property_id = pc.Property_id
group by pd.state, pd.county;
Notes:
Your case syntax just doesn't make sense. You either have conditions (as case when <condition> or you have constants as case <column> when <value>). But not both.
date(now()) = CURDATE().
You don't need case expressions because MySQL treats booleans as integers in a numeric context. You can just "sum them up" to count the number of true values.
EDIT:
If you want 3-6 months, then you would do:
SUM(pc.close_dt >= CURDATE() - INTERVAL 6 MONTH AND
pc.close_dt < CURDATE() - INTERVAL 3 MONTH) AS `6 MONTH`
I have a query that selects 3 random items from database table but I need to apply some more logic to the query based on the value of a field in the query.
This is what I have so far hope it makes sense. Have not fully tested it yet figured I would run it through you guys first see if there is anything that jumps out.
SELECT ord.id, keyword, url, daily_max
FROM orders AS ord
LEFT JOIN product_tasks AS tsk ON tsk.id = ord.task_id
LEFT JOIN product_groups AS grp ON grp.id = tsk.product_group
WHERE (
status = 'approved' AND
ord.total_actions_today < tsk.daily_max AND
grp.id = 1 AND
country_code = '$country' AND
(
CASE WHEN daily_max >= 5 THEN last_displayed < (NOW() - INTERVAL 30 MINUTE)
CASE WHEN daily_max >10 THEN last_displayed < (NOW() - INTERAVAL 5 MINUTE)
ELSE last_displayed < (NOW() - INTERAVAL 60 MINUTE)
)
)
GROUP BY ord.id
ORDER BY RAND()
LIMIT 3
Just tested the query and as I suspected I have my syntax wrong so any help would be appreciated:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE WHEN daily_max >10 THEN last_displayed < (NOW() - INTERAVAL 5 MINUTE) ' at line 14
Edit - Fixed
(After several attempts + edits):
SELECT ord.id, keyword, url, daily_max
FROM orders AS ord
LEFT JOIN product_tasks AS tsk ON tsk.id = ord.task_id
LEFT JOIN product_groups AS grp ON grp.id = tsk.product_group
WHERE (
status = 'approved' AND
ord.total_actions_today < tsk.daily_max AND
grp.id = 1 AND
country_code = 'us'
AND last_displayed <
CASE WHEN (daily_max >= 5) THEN (NOW() - INTERVAL 30 MINUTE)
WHEN (daily_max >10) THEN (NOW() - INTERVAL 5 MINUTE)
ELSE (NOW() - INTERVAL 60 MINUTE)
END
)
GROUP BY ord.id
ORDER BY RAND()
LIMIT 3
http://sqlfiddle.com/#!2/ac4499/1
Solved DOH missing ) in the where statement
Move the last_displayed out of the CASE, such that it is compared to a single value as projected out of the CASE statement:
WHERE...
AND last_displayed <
CASE WHEN (daily_max >= 5) THEN (NOW() - INTERVAL 30 MINUTE)
WHEN (daily_max >10) THEN (NOW() - INTERVAL 5 MINUTE)
ELSE (NOW() - INTERVAL 60 MINUTE)
END;
Also note a couple of typos - INTERVAL not INTERAVAL, and just one CASE is required.
SqlFiddle here
Your structure of your WHERE was not correct. Try the following:
SELECT ord.id, keyword, url, daily_max
FROM orders AS ord
LEFT JOIN product_tasks AS tsk ON tsk.id = ord.task_id
LEFT JOIN product_groups AS grp ON grp.id = tsk.product_group
WHERE (
status = 'approved' AND
ord.total_actions_today < tsk.daily_max AND
grp.id = 1 AND
country_code = '$country' AND
(
CASE WHEN daily_max >10 THEN (NOW() - INTERAVAL 5 MINUTE)
WHEN daily_max >= 5 THEN (NOW() - INTERVAL 30 MINUTE)
ELSE (NOW() - INTERAVAL 60 MINUTE)
END > last_displayed
)
)
GROUP BY ord.id
ORDER BY RAND()
LIMIT 3
I want to compute scores for some data I have in a MySQL database. The score will be computed as follows:
score = COUNT(purchases MADE BETWEEN NOW() AND (NOW() - 1 WEEK))
+ 0.7 * COUNT(purchases MADE BETWEEN (NOW() - 1 WEEK) AND (NOW() - 2 WEEKS))
+ 0.4 * COUNT(purchases OLDER THAN (NOW() - 2 WEEKS))
I have purchses in a table with a purchase_time column.
Is it possible to do this in MySQL and get output similar to the following?
ORDER_ID SCORE
3 8
4 3
5 15
Thanks
--- EDIT ---
The table structure is:
tblOrder - table
id - primary key
created - time stamp
SELECT orderId,
SUM
(
CASE
WHEN purchase_date > NOW() - INTERVAL 1 WEEK AND purchase_date <= NOW() THEN
1
WHEN purchase_date > NOW() - INTERVAL 2 WEEK AND purchase_date <= NOW() - INTERVAL 1 WEEK THEN
0.7
ELSE
0.3
END
)
FROM mytable
GROUP BY
orderId
Your between and older need to be converted into CASE.
CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
END CASE
At the same time you can rewrite the expression so that specific cases are the factors such as
SELECT SUM(
CASE DATEDIFF(now(),purchase_datetime) DIV 7
WHEN 0 THEN 1
WHEN 1 THEN 0.7
ELSE 0.4
END
)
FROM table
WHERE purchase_datetime < now()
GROUP BY ORDER_ID
SELECT ORDER ID, COUNT(purchases MADE BETWEEN NOW() AND (NOW() - 1 WEEK))
+ 0.7 * COUNT(purchases MADE BETWEEN (NOW() - 1 WEEK) AND (NOW() - 2 WEEKS))
+ 0.4 * COUNT(purchases OLDER THAN (NOW() - 2 WEEKS)) AS SCORE
FROM TABLE
should do the trick, I don't currently have mysql installed so I can't test it.
also, use datediff to find if a date is between the range of dates
SELECT order_id, sum(score) FROM
(
(SELECT Order_id, COUNT(id) AS Score FROM purchases
WHERE purchase_time BETWEEN CURDATE() AND DATE_SUB(CURDATE(),INTERVAL 1 WEEK))
GROUP BY order_id
UNION ALL
(SELECT Order_id, (COUNT(id) * 0.7) AS score FROM purchases
WHERE purchase_time BETWEEN DATE_SUB(CURDATE(),INTERVAL 1 WEEK)
AND DATE_SUB(CURDATE(),INTERVAL 2 WEEK))
GROUP BY order_id
UNION ALL
(SELECT Order_id, (COUNT(id) * 0.4) AS score FROM purchases
WHERE purchase_time < DATE_SUB(CURDATE(),INTERVAL 2 WEEK))
GROUP BY order_id
) s
GROUP BY order_id;