How to add date condition to my query? - mysql

I have this SQL statement. It works, and I need to add another one condition.
I need to sort it by date. occurence - is my date row.
SELECT dd.caption, COUNT(t.occurence)
FROM transaction t
INNER JOIN dict_departments dd
ON dd.id = t.terminal_id
GROUP BY dd.caption
How to add this condition:
WHERE t.occurence BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH)
to my query.

Try this:
WHERE t.occurrece BETWEEN current_date() AND dateadd(month,1,current_date())
The function dateadd is a SQL SERVER function, but the rest of the clause is standard SQL.

BETWEEN requires two arguments, a start point and an end point. If your end point is the current time, you have two options:
Using BETWEEN:
WHERE t.occurence BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND NOW()
Using simple comparison operator:
WHERE t.occurence >= (CURRENT_DATE() - INTERVAL 1 MONTH)

If you want to filter dates from 1 month ago till now:
WHERE (t.occurrece BETWEEN DATE_ADD(CURDATE(), INTERVAL -1 MONTH) AND CURDATE()) = 1
or
WHERE (t.occurrece BETWEEN ADDATE(CURDATE(), INTERVAL -1 MONTH) AND CURDATE()) = 1

Related

MySql - where condition on select if statment result

I have following example
SELECT
oa.country_id,
s.country_id,
(IF(
s.country_id = oa.country_id,
DATE_ADD(DATE(so.created_at), INTERVAL 2 DAY),
DATE_ADD(DATE(so.created_at), INTERVAL 5 DAY)
)
) AS inter
And I would like to add where condition as follow:
WHERE inter >= CURDATE() - INTERVAL DAYOFWEEK(CURDATE())+6 DAY
AND inter > CURDATE() - INTERVAL DAYOFWEEK(CURDATE())-1 DAY;
Is it possible add where to "inter" result?
Or I need to repeat the same if statement query in WHERE twice?
Thnak you.
You cannot use inter in the WHERE clause because column aliases are not understood there. You could use a CTE or subquery.
However, MySQL extends the use of HAVING for non-aggregation queries. So you can add a HAVING clause:
HAVING inter >= CURDATE() - INTERVAL DAYOFWEEK(CURDATE())+6 DAY AND
inter > CURDATE() - INTERVAL DAYOFWEEK(CURDATE())-1 DAY;
Note: The logic of the expression does not look correct -- but this is the expression in your question. The first part is more restrictive, so the second part is redundant. Perhaps you intend < for one of them.

Counting all rows in column with two different date conditions

I'm trying to turn two count queries with date conditions (the ones below) into one query.
SELECT COUNT(*) as yesterday FROM orders WHERE DATE(timedate) = DATE(NOW() - INTERVAL 1 DAY)
SELECT COUNT(*) as yesterday FROM orders WHERE DATE(timedate) = DATE(NOW() - INTERVAL 2 DAY)
Following the advice of another answer I created the following, but that doesn't seem to work syntax-wise, and I'm not quite sure why. Is there another way to do this? I can't find a similar question on this
SELECT
SUM(IF(DATE(timedate) = DATE(NOW() - INTERVAL 1 DAY))) AS testcount1,
SUM(IF(DATE(timedate) = DATE(NOW() - INTERVAL 2 DAY))) AS testcount2
FROM
orders
You're missing the output values for the IF expression. Also you should use CURRENT_DATE() so you don't need to convert to a DATE:
SELECT
SUM(IF(DATE(timedate) = CURRENT_DATE() - INTERVAL 1 DAY, 1, 0)) AS testcount1,
SUM(IF(DATE(timedate) = CURRENT_DATE() - INTERVAL 2 DAY, 1, 0)) AS testcount2
FROM
orders
Note that MySQL treats boolean expressions as 1 (true) or 0 (false) in a numeric context, so you can actually SUM the expression without needing the IF:
SELECT
SUM(DATE(timedate) = CURRENT_DATE() - INTERVAL 1 DAY) AS testcount1,
SUM(DATE(timedate) = CURRENT_DATE() - INTERVAL 2 DAY) AS testcount2
FROM
orders
You want conditional aggregation. I would phrase the query as follows:
SELECT
SUM(
timedate >= CURRENT_DATE - INTERVAL 1 DAY
and timedate < CURRENT_DATE
) AS testcount1,
SUM(
timedate >= CURRENT_DATE - INTERVAL 2 DAY
and timedate < CURRENT_DATE- INTERVAL 1 DAT
) AS testcount2
FROM orders
Details:
this uses a nice feature of MySQL, that evaluates false/true conditions as 0/1 in numeric context
no date functions are applied on the timedate column : instead, we do litteral date comparisons. This is much more efficient, since the database can possibly take advantage of an index on the datetime column
You might also want to add a WHERE clause to the query:
WHERE
timedate >= CURRENT_DATE - INTERVAL 2 day
AND timedate< CURRENT_DATE

How do I subtract two results from a SELECT statement within that statement?

This pulls back two int values of yesterday and today. I'd like to subtract the two results from within the statement in a third column called difference:
SELECT (
SELECT COUNT(*)
FROM collectors_users
WHERE DATE(dateadded) = CURDATE() - INTERVAL 1 DAY
) AS yesterday, COUNT(*) AS today
FROM collectors_users
WHERE DATE(dateadded) = CURDATE()
You need to repeat the expressions. SQL (in general) does not allow you to re-use column aliases in the same SELECT. You can simplify the logic to:
SELECT SUM(DATE(dateadded) = CURDATE() - INTERVAL 1 DAY) AS yesterday,
SUM(DATE(dateadded) = CURDATE()) as today,
(SUM(DATE(dateadded) = CURDATE()) -
SUM(DATE(dateadded) = CURDATE() - INTERVAL 1 DAY)
) as diff
FROM collectors_users
WHERE dateadded >= CURDATE() - INTERVAL 1 DAY AND
dateadded < CURDATE() + INTERVAL 1 DAY;
Note that the logic for the WHERE clause covers two days. Also, it does not use DATE(). This would allow the query to use an index, if available.

How to get data prior to one month of what user enters in SQL

current query
SELECT col1 FROM table1 where
id=1234 and (date(sys_time)
between "2015-07-01" AND "2015-07-10")
;
I want to get the data prior to one month from the dates mentioned
here. Is there any SQL query to do it?
Just use date_sub():
SELECT col1
FROM table1
WHERE id = 1234 AND
sys_time >= date_sub('2015-07-01', interval 1 month) and
sys_time < date_add(date_sub('2015-07-01', interval 1 month), interval 1 day)
Notice that I removed the date() function from sys_time. This helps MySQL use an index for the expression, if one is available:
Try this
SELECT GETDATE() AS CurrentDate, DATEADD(dd,-30,getdate())
You could set the "current date" and the previous date as parameters and include those in your query as the critera
Use DATE_ADD("2015-07-03", INTERVAL 1 MONTH) for the adding interval
Example :
SELECT a,b FROM table WHERE from_date between DATE_ADD("2015-07-01", INTERVAL 1 MONTH) AND DATE_ADD("2015-07-03", INTERVAL 1 MONTH)

Combine 2 MySQL queries

I have this query
SELECT COUNT(*) from `login_log` where from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL 1 WEEK);
and the same one with 1 diff. it's not 1 WEEK , but 1 MONTH
how can I combine those two and assign them to aliases?
I would do this with conditional aggregation:
SELECT SUM(from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL 1 WEEK)),
SUM(from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL 1 MONTH))
FROM `login_log`;
MySQL treats boolean values as integers, with 1 being "true" and 0 being "false". So, using sum() you can count the number of matching values. (In other databases, you would do something similar using case.)
Use the where condition with one month internal and add the same where condition with one week internal as a Boolean column return.
I mean
Select count (*) all_in_month, (from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL 1 WEEK)) as in_week from `login_log` where from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL 1 a MONTH) GROUP BY in_week;
P.s. haven't tested but afaik it should work
Even though it's pretty tough to understand what you ask:
If you want them in the same column use OR
SELECT COUNT(*) from 'login_log' where from_unixtime('date') >= DATE_SUB(NOW(), INTERVAL 1 WEEK) OR from_unixtime('date') >= DATE_SUB(NOW(), INTERVAL 1 MONTH) ;
If you don't want duplicate answers: use GROUP BY