Select * Where Date 1 and Date 2 elapsed time is greater than - mysql

I have 2 Columns in the same table: CREATED DATE and APPROVED DATE
In my Select statement, I would like to return only records Where the elapsed time between CREATED DATE and APPROVED DATE is greater than 2 days, or where the APPROVED DATE is greater than CREATED DATE by 2 days.
I really appreciate the help!

You can use DATEDIFF
WHERE DATEDIFF(created_date, approved_date) > 2 OR DATEDIFF(approved_date, created_date) > 2

Depending on how precisely you need to know:
If the "date" columns are dates:
WHERE DATEDIFF(`APPROVED DATE`, `CREATED DATE`) > 2
If the "date" columns are timestamps and you want to know the second 2 days have passed:
WHERE TIMESTAMPDIFF(DAY, `CREATED DATE`, `APPROVED DATE`) > 2

Related

MySQL query group month into two periods

I want to do group select in MySQL, which is group my timestamp column into 2 periods of month and count each rows. just say, if there are rows in a month in date 1, 2 ,3, and also 17,19 in Sept it would be :
Period count
Sept 1 3
Sept 2 2
Anyone can help me please?
Thanks!
I assume you have 1 column period
SELECT period, count(period) AS count FROM table_name GROUP BY period

MySQL select all entries in a recurring time interval from a table with a timestamp column

Lets say i have a table in a MySQL db with the following schema.
+-----------+--------+
| timestamp | string |
+-----------+--------+
And in this table are many entries per day for a month.
Now i want to count the entries between 6am and 10am per day for every day in the table and group them by this timeframe for each day.
The response should be something like this
+------+------------------------------+
| date | count in the given timeframe |
+------+------------------------------+
My Problem is how to select a recurring time frame. I know how to count the entries and how to group them.
I had success to do that on an intervall. But the intervall is not the same for every day.
SELECT count(timestamp) as count,
ROUND(UNIX_TIMESTAMP(timestamp)/(60 * 60 * ", hours, ")) AS timekey
FROM event_data,
GROUP BY timekey
Assuming that you are working on MySQL, you can use the following query:
SELECT
date(timestamp1) AS entrydate
, COUNT(*) FROM mytable
WHERE
EXTRACT(HOUR FROM TIME(timestamp1)) BETWEEN 6 AND 10
GROUP BY
entrydate
Where
timestamp1 --> timestamp in your example
entrydate --> date in your desired output column
mytable --> the table which holds the data
And, please provide some sample entries next time and try to be a bit specific.
psuedo code will be like
select timestamp ,
sum(case when hours of timestamp between 6am and 10am then 1 else 0 end) timeframe_count
from yourtable
group by timestamp

How to retrieve sum of data form last 7 days as a separate sums

In my CRM system I have table with leads. I would like to make a chart to see how many leads were added in last 7 days. For that purpose I need to have separete sums for every day from last week.
How to do that in MySQL?
My table called tab_leads it have lead_id (integer) and lead_create_date (time stamp, format: 0000-00-00 00:00:00)
So I need something like:
Day 1 - 10
Day 2 - 0
Day 3 - 5
Day 4 - 1
Day 5 - 9
Day 6 - 15
Day 7 (today) - 2
Just use a GROUP BY query:
SELECT
DATE(lead_create_date) AS `Date`,
COUNT(*) AS `Leads`
FROM
tab_leads
WHERE
lead_create_date >= CURRENT_DATE - INTERVAL 6 DAY
GROUP BY
DATE(lead_create_date)
The above query assumes that there are no future records and current day is counted as the 7th day.
Try this Mysql Query
SELECT * FROM tab_leads WHERE DATE(lead_create_date) = DATE_SUB(DATE(NOW()), INTERVAL 7 DAY) GROUP BY DATE(lead_create_date);
Try this
SELECT COUNT(ead_id) from tab_leads GROUP BY DAY(lead_create_date)
( or as per your requirement )
SELECT SUM(ead_id) from tab_leads GROUP BY DAY(lead_create_date)

Select between dates with count condition

I have table user with column login_time.
I want to select all the users that have logged in more than 10 times in a month.
I tried something like this:
SELECT login_time, count(id) as loginCount FROM user
WHERE login_time between DATE_SUB(login_time INTERVAL 1 month) AND login_time
GROUP BY id, MONTH(login_time) HAVING loginCount > 10;
Im not sure about my selection between dates.
How can I select with a month intervals avoiding double records.
For example if I have this values for login_time:
1. '2015-02-01 14:05:19'
2. '2015-01-21 14:05:19'
3. '2015-01-11 14:05:19'
Both 3 and 2 are within month range of 1.
So will I get double records for that values?
To find the users who have logged in more than ten times in the month ending right now, do this.
SELECT COUNT(*) times_logged_in,
userid
FROM user
WHERE login_time >= NOW() - INTERVAL 1 MONTH
GROUP BY user_id
HAVING COUNT(*)> 10
To find the users who have logged in more than ten times in any calendar month in your table, do this.
SELECT COUNT(*) times_logged_in,
DATE(DATE_FORMAT(login_time, '%Y-%m-01')) month_beginning,
userid
FROM user
GROUP BY user_id, DATE(DATE_FORMAT(login_time, '%Y-%m-01'))
HAVING COUNT(*)> 10
The trick here is the expression DATE(DATE_FORMAT(login_time, '%Y-%m-01')), which converts any timestamp to the first day of the month in which it occurs.
Your question mentioned this WHERE condition:
WHERE login_time between DATE_SUB(login_time INTERVAL 1 month) AND login_time
This doesn't do anything interesting because it always comes back true. Each given login_time always falls in the interval you specified.
Edit: You can GROUP BY MONTH(dt) if you want. But the way I have shown it automatically accounts for years as well as months, so in my opinion it's much better for accurate reporting.
Another edit: This formula yields the preceding Sunday for any given date or timestamp item.
FROM_DAYS(TO_DAYS(login_time) -MOD(TO_DAYS(login_time) -1, 7))
If Monday is the first day of the week in your jurisdiction, change the -1 to -2. Grouping by this function is superior to doing GROUP BY WEEK(login_time) because WEEK() does odd things at the beginnings and ends of calendar years.
This is all written up in more detail here: http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/

selecting the month day and year from a transaction table

I want to create two queries for my table which has fields name,surname and amount paid,the first query should select the day,month and the amount paid,the second query should select a month,year in that year and the total amount paid in that month,lets say john paid on 2013-05-01, on 2013-05-03,while peter paid on 2013-04-08, i want the first query to output
month and day amount
05-01 200
05-03 400
04-08 50
and the second query should output:
month and year total
2013-05 600
2013-04 50
I know I can use the sum aggregate function to select the total but the tricky part is how to select the day and the month in the format above,
first query will be
SELECT DATE_FORMAT(date, "%m-%d") AS 'month and day',price as amount FROM `tablename`
and second query will be
SELECT DATE_FORMAT(date, "%Y-%m") AS 'month and year' , SUM(price) AS total FROM `tablename` GROUP BY YEAR(date), MONTH(date)