SELECT between today and yesterday - mysql

I'm trying to select between two dates like this:
SELECT p.Code, p.Name, sum(h.PA = 1) AS PA, sum(h.PB = 1) AS PB,
sum(h.PG = 1) AS PG, sum(h.GoedkeuringDoorNew = 'GF') AS GF,
sum(h.GoedkeuringDoorNew = 'SB') AS SB, sum(h.GoedkeuringDoorNew = 'VIA') AS VIA,
sum(h.Blanco) AS Blanco
FROM psthostess p
LEFT JOIN `psttodo-uit` h
ON h.`Hostess Code` = p.Code
AND DATE(h.`afgewerkt tablet datum`) BETWEEN CURDATE()
AND (CURDATE() - INTERVAL 1 DAY)
WHERE p.Indienst = 1 GROUP BY p.Code, p.Name
But I don't get any results.
I want the results of today and yesterday. But when I do this
SELECT *
FROM `psttodo-uit` p
WHERE DATE(p.`afgewerkt tablet datum`) = CURDATE() - INTERVAL 1 DAY
or change CURDATE() - INTERVAL 1 DAY into CURDATE() I get results... How does this come?

WHERE p.`afgewerkt tablet datum` >= CURDATE() - INTERVAL 1 DAY
AND p.`afgewerkt tablet datum` < CURDATE() + INTERVAL 1 DAY
Assuming current date and time is: April 11, 2014 05:30PM, the datetime you need is betweenApril 10, 2014 00:00:00 and April 11, 2014 23:59:59 (inclusive)
CURDATE() - INTERVAL 1 DAY => April 10, 2014 00:00:00
CURDATE() + INTERVAL 1 DAY => April 12, 2014 00:00:00

Select * from emp where joindate between SUBDATE(NOW(),1) and NOW();

Related

Query data in time interval that spans two days in mysql

I need to count data for a given <week | month | custom> interval grouped by a given time schedule that possibly spans 2 days. The chunks depends on customers working schedules.
Possible cases (for one month interval) :
all data from June 01, 2022 to July 01, 2022, each day between 08:00 pm and 04:00 am (overnight)
all data from June 01, 2022 to June 30, 2022, each day between 04:00 am and 08:00 pm
all data from June 01, 2022 to June 30, 2022, each day between 00:00 am and 23:59 pm
Here's what I came up with:
WITH RECURSIVE seq AS (
SELECT
0 AS value
UNION ALL
SELECT
value + 1
FROM
seq
WHERE
value < 29
),
period AS (
SELECT
'2022-06-01 20:00' + INTERVAL (value * 24 * 60) MINUTE AS start,
'2022-06-01 20:00' + INTERVAL (value * 24 * 60) MINUTE + INTERVAL (8* 60) MINUTE AS end
FROM seq
ORDER BY value DESC
)
SELECT *
FROM (
SELECT
DATE(sd.timestamp - INTERVAL(LEAST(20, 4)) HOUR) as date,
SUM(...) as count,
FROM sensor_data sd
WHERE sd.timestamp BETWEEN '2022-06-01 20:00' AND '2022-07-01 04:00'
AND HOUR(sd.timestamp) >= 20 AND HOUR(sd.timestamp) < 4
GROUP BY
date
) main_data
INNER JOIN period ON DATE(period.start) = date
Unfortunately in doesn't work for the first case (spans two days). Any ideas?
WITH RECURSIVE
cte AS (
SELECT datetime_from range_from,
datetime_from + INTERVAL range_length HOUR range_till
UNION ALL
SELECT range_from + INTERVAL 1 DAY,
range_till + INTERVAL 1 DAY
FROM cte
WHERE range_till < datetime_till
)
SELECT range_from, range_till, COUNT(*) rows_amount
FROM cte
LEFT JOIN test ON test.dt BETWEEN range_from AND range_till
GROUP BY 1, 2;
DEMO with some explanations.

Select the next day occurance from the table

This is my db structure of the table game days. Every game ends at 8:00 pm . Here all the game id is same.So now i want to query by the game id like this way that if today is monday and it is before 8:00 pm so it will fetch only the game_days with value of day='Monday'.Once 8:00pm is over then it will show the row with the value of day='Wednesday' until wednesday 8:00pm and after 8:00 pm it will show the row with the value of day='Friday' until Friday 8:00pm and then again after Friday 8:00pm it will show Monday...
So what will be the query for this ?
I think you just want the date offset by 4 hours. One method is:
where date(updated_at + interval 4 hour) = curdate()
I am guessing that updated_at is the column you want to reference, but it can be any date/time column.
I prefer to express this as:
where updated_at >= curdate() - interval 4 hour and
updated_at < curdate() + interval 1 day - interval 4 hour
This version can make use of an index on the date/time column.
Please try this (updated based on comments, replace table_name with appropriate table name is your db):
SELECT *
FROM `table_name`
WHERE day = (
SELECT t3.day FROM `table_name` as t3
LEFT JOIN (
SELECT HOUR(NOW()),
CASE
WHEN HOUR(NOW()) <= 19 THEN DAYNAME(NOW() + INTERVAL t.p DAY)
ELSE DAYNAME(NOW() + INTERVAL (t.p + 1) DAY)
END AS day,
CASE
WHEN HOUR(NOW()) <= 19 THEN t.p
ELSE t.p + 1
END as p
FROM (
SELECT 0 as p
UNION SELECT 1 as p
UNION SELECT 2 as p
UNION SELECT 3 as p
UNION SELECT 4 as p
UNION SELECT 5 as p
UNION SELECT 6 as p
) t
) t2 on t3.day = t2.day
ORDER BY t2.p ASC LIMIT 1
)

Mysql select rows based on previous quarterly month from current month

I want to query row to view registered record based on previous quarterly month, for example:
current month / year: Jan 2018
the query should show all records by registered month which is: Oct 2017, Jul 2017, Apr 2017 and so on.
I use below query but only can select rows for last 3 month.
SELECT name, date, amount, agreement, bank
FROM `account`
WHERE (YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 3 MONTH) AND MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 3 MONTH))
You can do arithmetic like this:
select a.*
from account a
where mod(month(date), 3) = mod(month(current_date), 3);
You seem to understand how to handle the year component, but something like:
and date >= curdate() - interval 1 year
Try This One, Manually
select *
from account
Where current_date >= dateadd(year, -1, Getdate())
And Month(current_date) In (
Month(Getdate()),
(Case When Month(Getdate())+3 > 12 Then Month(Getdate())+3-12 Else Month(Getdate())+3 End),
(Case When Month(Getdate())+6 > 12 Then Month(Getdate())+6-12 Else Month(Getdate())+6 End),
(Case When Month(Getdate())+9 > 12 Then Month(Getdate())+9-12 Else Month(Getdate())+9 End)
)

month starting another day

I've been using MONTH() function to get the months and grouping by month, like this example query.
SELECT
t1.ano,
t1.mes,
tempo_extra,
tempo_ativo,
tempo_extra / tempo_ativo AS volume_extra
FROM
(SELECT
YEAR(`data`) AS ano,
MONTH(`data`) AS mes,
SUM(tempo) AS tempo_extra
FROM
rh.aprovacoes
WHERE
(tipo = 'BH' OR tipo = 'HE')
AND estado = 1
AND YEAR(aprovacoes.`data`) = 2016
GROUP BY MONTH(`data`)) AS t1
LEFT JOIN
(SELECT
MONTH(`data`) AS mes, SUM(ativo) AS tempo_ativo
FROM
rh.processamento
GROUP BY MONTH(`data`)) AS t2 ON t1.mes = t2.mes
ORDER BY mes DESC;
How can i make months start on 23rd of the last month and end on 22nd of the current month.
For example, April starting on March 23rd and end on April 22nd.
Simply subtract 22 days from your date and add a month:
(`data` - interval 22 day) + interval 1 month
March 22 => February 28 or 29 => March 28 or 29
March 23 => March 1 => April 1
April 22 => March 31 => April 30
April 23 => April 1 => May 1
SQL fiddle: http://sqlfiddle.com/#!9/9eecb7d/54883
Your query joins records regardless of the year by the way. I don't think this is desired, so in below query I've corrected this.
SELECT t1.ano, t1.mes, tempo_extra, tempo_ativo, tempo_extra/tempo_ativo AS volume_extra
FROM
(
SELECT
YEAR(data - interval 22 day + interval 1 month) AS ano,
MONTH(data - interval 22 day + interval 1 month) AS mes,
SUM(tempo) AS tempo_extra
FROM rh.aprovacoes
WHERE (tipo = 'BH' OR tipo = 'HE')
AND estado = 1
AND YEAR(aprovacoes.data - interval 22 day + interval 1 month) = 2016
GROUP BY
YEAR(data - interval 22 day + interval 1 month),
MONTH(data - interval 22 day + interval 1 month)
) AS t1
LEFT JOIN
(
SELECT
YEAR(data - interval 22 day + interval 1 month) AS ano,
MONTH(data - interval 22 day + interval 1 month) AS mes,
SUM(ativo) AS tempo_ativo
FROM rh.processamento
GROUP BY
YEAR(data - interval 22 day + interval 1 month),
MONTH(data - interval 22 day + interval 1 month)
) AS t2 ON t1.ano = t2.ano AND t1.mes = t2.mes
ORDER BY t1.ano DESC, t1.mes DESC;

Retrieving Data from database on quaterly basis condition But it should be based on current year data only

I had tried this code:
Its works also fine,but the issue is, if current month is feb and fire this query then it considers past 3 months from now and hence starts from past year i.e 2012 nov or dec say i want only current year data,if it is feb now and i fire this query then it should only show jan and feb records.
SELECT CROEmailId,
(
SELECT COUNT(LeadId)
FROM LeadStatus
WHERE DATE(`LeadTime`)> DATE_SUB(now(),
INTERVAL 3 MONTH
)
AND Generated=1 and AssignedTo=a.CROEmailId)
AS 'NEW LEAD',(
SELECT COUNT(LeadId)
FROM LeadHistory
WHERE DATE(UpdatedAt)> DATE_SUB(now(),
INTERVAL 3 MONTH
) AND AssignedTo=a.CROEmailId)
AS 'Lead Updated',
(
SELECT SUM(TotalEmails)
FROM MailJobs
WHERE DATE(CompletedAt)> DATE_SUB(now(),
INTERVAL 3 MONTH
)
AND MailFrom=a.CROEmailId)
AS 'Email Uploaded',
(
SELECT SUM(TotalSent)
FROM MailJobs
WHERE DATE(CompletedAt)> DATE_SUB(now(),
INTERVAL 3 MONTH)
AND MailFrom=a.CROEmailId
)
AS 'Email Sent',
(
SELECT SUM(NetTotal)
FROM Invoice
WHERE Status='PAID'
AND DATE(CreatedAt)> DATE_SUB(now(), INTERVAL 3 MONTH)
AND CROEmailId=a.CROEmailId)
AS 'Payment Today' FROM CustomersManager a;
Try change
DATE_SUB(now(), INTERVAL 3 MONTH)
to
IF(MONTH(CURDATE()) < 4, DATE_FORMAT(CURDATE(), '%Y-01-01'), CURDATE() - INTERVAL 3 MONTH)
in all subqueries.
SELECT CROEmailId,
(SELECT COUNT(LeadId)
FROM LeadStatus
WHERE DATE(`LeadTime`)> IF(MONTH(CURDATE()) < 4, DATE_FORMAT(CURDATE(), '%Y-01-01'), CURDATE() - INTERVAL 3 MONTH)
AND Generated=1
AND AssignedTo=a.CROEmailId) AS 'NEW LEAD',
(SELECT COUNT(LeadId)
FROM LeadHistory
WHERE DATE(UpdatedAt)> IF(MONTH(CURDATE()) < 4, DATE_FORMAT(CURDATE(), '%Y-01-01'), CURDATE() - INTERVAL 3 MONTH)
AND AssignedTo=a.CROEmailId) AS 'Lead Updated',
(SELECT SUM(TotalEmails)
from MailJobs
WHERE DATE(CompletedAt)> IF(MONTH(CURDATE()) < 4, DATE_FORMAT(CURDATE(), '%Y-01-01'), CURDATE() - INTERVAL 3 MONTH)
AND MailFrom=a.CROEmailId) AS 'Email Uploaded',
(SELECT SUM(TotalSent)
FROM MailJobs
WHERE DATE(CompletedAt)> IF(MONTH(CURDATE()) < 4, DATE_FORMAT(CURDATE(), '%Y-01-01'), CURDATE() - INTERVAL 3 MONTH)
AND MailFrom=a.CROEmailId) AS 'Email Sent',
(SELECT SUM(NetTotal)
FROM Invoice
WHERE Status='PAID'
AND DATE(CreatedAt)> IF(MONTH(CURDATE()) < 4, DATE_FORMAT(CURDATE(), '%Y-01-01'), CURDATE() - INTERVAL 3 MONTH)
AND CROEmailId=a.CROEmailId) AS 'Payment Today'
FROM CustomersManager a;
use this in your query to find record filter by year
YEAR( '20013-12-12' )
example,
SELECT * FROM TABLE WHERE YEAR(DATE_FIELD) = 2013