Need to build a quuerly groupby - mysql

Hi I am trying to build a query not getting my desired result please help me.
Here is my table:
SELECT count(*)
FROM
t_user_segment
WHERE
updatedOn
BETWEEN '2015-11-03 00:00:00' AND '2015-11-03 23:59:59'
i want to get data here from here of field updatedon from last 5days to current date.

Check below answer if it useful
SELECT count(*) as cnt, date(updatedOn) as updatedOnDate FROM t_user_segment
WHERE updatedOnDate >= DATE_SUB(CURRENT_DATE, INTERVAL 5 DAY) group by updatedOnDate;

Related

Distinct values when already using count and group by

This is currently the SQL query I am using:
SELECT DAY(time), count(id)
FROM users
WHERE time >= now() - INTERVAL 7 DAY
GROUP BY DAY(TIME)
But it includes repeated email values. How can I get the count by day for only unique email address entries?
Thanks
Solution:
SELECT DAY(time), count(distinct email)
FROM userWHERE time >= now() - INTERVAL 7 DAY
GROUP BY DAY(TIME)

Query results based on the current date

I am trying to figure out how to select results based on the current date. The script will be ran daily but I am unsure of how to do it automatically.
SELECT * FROM Orders WHERE OrderDate='2008-11-11'
What I am trying now
SELECT * FROM Orders WHERE OrderDate=NOW() - INTERVAL 1 DAY
SELECT *
FROM Orders
WHERE OrderDate >= DATE(NOW())
AND OrderDate < DATE(NOW()) + INTERVAL 1 DAY
EDIT
I always tend to write the above style of query when I expect the column that I am checking against to have a time component as well.
If your OrderDate column does not have a time component, then as Lennart pointed out, you can simply do:
SELECT *
FROM Orders
WHERE OrderDate = DATE(NOW())
EDIT 2: Mihai's comment on your question is also relevant. You can simply use CURDATE() instead of DATE(NOW()).
You could do
SELECT * FROM Orders WHERE (OrderDate > DATE_SUB(NOW(),INTERVAL 1 DAY))

Select all rows except those from today?

I'm selecting all rows except those from last 24hs. Instead I'd like to select those which are not from today. I'm already getting the day from a datetime column using DATE().
How can I change the query to do it?
select Date, DATE(Date) AS Day
WHERE Date < DATE_SUB(CURDATE(),INTERVAL 1 DAY)
It's easier than you think:
SELECT Date, DATE(Date) AS Day
WHERE Date < CURDATE()
OP said:
I'd like to select those which are not from today
bfavaretto's solution:
SELECT Date, DATE(Date) AS Day
WHERE Date < CURDATE()
would answer the question:
I'd like to select those which date is before today
To properly answer OP's question this query should be run (assuming date is a datetime because of the OP's cast, I'm casting it to a date field, but the cast is unnecesary if Date is actually a date field):
SELECT Date, DATE(Date) AS Day FROM aTable
WHERE Date(date) != CURDATE()

MySQL - select data from database between two dates

I have saved the dates of a user's registration as a datetime, so that's for instance 2011-12-06 10:45:36. I have run this query and I expected this item - 2011-12-06 10:45:36 - will be selected:
SELECT `users`.* FROM `users` WHERE created_at >= '2011-12-01' AND
created_at <= '2011-12-06'
But is not. Exist any elegant way, how to select this item? As a first idea that I got was like 2011-12-06 + 1, but this doesn't looks very nice.
Your problem is that the short version of dates uses midnight as the default. So your query is actually:
SELECT users.* FROM users
WHERE created_at >= '2011-12-01 00:00:00'
AND created_at <= '2011-12-06 00:00:00'
This is why you aren't seeing the record for 10:45.
Change it to:
SELECT users.* FROM users
WHERE created_at >= '2011-12-01'
AND created_at <= '2011-12-07'
You can also use:
SELECT users.* from users
WHERE created_at >= '2011-12-01'
AND created_at <= date_add('2011-12-01', INTERVAL 7 DAY)
Which will select all users in the same interval you are looking for.
You might also find the BETWEEN operator more readable:
SELECT users.* from users
WHERE created_at BETWEEN('2011-12-01', date_add('2011-12-01', INTERVAL 7 DAY));
SELECT users.* FROM users WHERE created_at BETWEEN '2011-12-01' AND '2011-12-07';
You need to use '2011-12-07' as the end point as a date without a time default to time 00:00:00.
So what you have actually written is interpreted as:
SELECT users.*
FROM users
WHERE created_at >= '2011-12-01 00:00:00'
AND created_at <= '2011-12-06 00:00:00'
And your time stamp is: 2011-12-06 10:45:36 which is not between those points.
Change this too:
SELECT users.*
FROM users
WHERE created_at >= '2011-12-01' -- Implied 00:00:00
AND created_at < '2011-12-07' -- Implied 00:00:00 and smaller than
-- thus any time on 06
Another alternative is to use DATE() function on the left hand operand as shown below
SELECT users.* FROM users WHERE DATE(created_at) BETWEEN '2011-12-01' AND '2011-12-06'
Have you tried before and after rather than >= and <=? Also, is this a date or a timestamp?
Searching for created_at <= '2011-12-06' will search for any records that where created at or before midnight on 2011-12-06
. You want to search for created_at < '2011-12-07'.
Maybe use in between better. It worked for me to get range then filter it
You can use MySQL DATE function like below
For instance, if you want results between 2017-09-05 till 2017-09-09
SELECT DATE(timestamp_field) as date FROM stocks_annc WHERE DATE(timestamp_field) >= '2017-09-05' AND DATE(timestamp_field) <= '2017-09-09'
Make sure to wrap the dates within single quotation ''
Edit:
A better solution would be this. It would make sure that it uses the index if any exists.
select date(timestamp_field) as date from stocks_annc where time_stamp_field >= '2022-01-01 00:00:00' and time_stamp_field <= '2022-01-10 00:00:00'
Hope this helps.

SQL Count Totals Within Date Ranges

I have a CHANGES table with fields VALUE(integer) and CREATED_AT(timestamp). I want to know the total of the VALUE column grouped by each of the past 30 days (without making 30 queries).
So if yesterday there were records created with VALUEs of 10, -7, and 12; I would want a record returned with CREATED_AT = yesterday and TOTAL = 15.
Any help?
SELECT date(created_at) as CREATED_AT, sum(value) as TOTAL
FROM changes
WHERE created_at >= curdate() - interval 30 day
GROUP BY date(created_at);
Well, it slightly depends on what kind the timestamp is formatted in (SQL/ Unix/ etc). But this type of query might help you along:
SELECT
DATE_FORMAT(CREATED_AT, '%Y-%m-%d') ym,
COUNT(VALUE)
FROM foo
GROUP BY ym