I have events table. I want latest event which event type='appointment' and group on type and instruction_id. Problem is event_date come first '2013-12-02' instead of '2013-12-05'. More information required give comment I explain in detail.
My Expected output :
ID INSTRUCTION_ID TYPE COMMENT EVENT_DATE
3 2 appointment at home December, 05 2013 00:00:00+0000
10 1 appointment at home November, 22 2013 00:00:00+0000
5 3 appointment office September, 17 2013 00:00:00+0000
For more information check SQL fiddle:
try below query:
select *
from
(SELECT *
FROM EVENTS
WHERE EVENTS.type='appointment'
ORDER BY EVENTS.event_date DESC) EVENTS
GROUP BY EVENTS.type,EVENTS.instruction_id;
Query 2:
select *
from
(SELECT *
FROM EVENTS
WHERE EVENTS.type='appointment'
ORDER BY EVENTS.event_date DESC) EVENTS
GROUP BY EVENTS.type;
If you want the latest of only "appointment type" grouping by event type:
SELECT max(EVENT.event_date) -- use other fields
FROM EVENTS
WHERE EVENTS.type='appointment'
GROUP BY EVENTS.instruction_id;
Because if you filter by one specific type you will only get the max date of that type. If you want to get the max date of each type
SELECT max(EVENT.event_date) -- use other fields
FROM EVENTS
GROUP BY EVENTS.type, EVENTS.instruction_id
EDIT: if you add the rest of the fields is working as you expected. Anyway, I paste you the query tested:
SELECT ID, INSTRUCTION_ID, TYPE, COMMENT, max(EVENT_DATE) EVENT_DATE
FROM EVENTS
WHERE EVENTS.type='appointment'
GROUP BY EVENTS.instruction_id
ORDER BY EVENT_DATE;
Related
The following code gives users who contributed tags to an online community from March 1, 2016 to February 28, 2017.
SELECT userid, COUNT(*) AS tags
FROM tag_events
WHERE tstamp >= ‘2016-03-01’ AND tstamp <= ‘2017-03-01’
GROUP BY userid
ORDER BY tags DESC;
tag_events is the table, tstamp is the timestamp of the tag, userid is the user id, and each entry in the table contains information about one tag.
I'm interested in users whose first contribution within that period was at least two calendar days before their last contribution (so July 1 and 3 would count, July 1,2,3 would count, but July 1 and 2 would not count).
How can I modify the code?
You should get the result you want by calculating the difference in days between the first and last timestamp using the datediff function, like this:
SELECT userid, COUNT(*) AS tags
FROM tag_events
WHERE tstamp >= '2016-03-01' AND tstamp <= '2017-03-01'
GROUP BY userid
HAVING DATEDIFF(MAX(tstamp), MIN(tstamp)) > 1
ORDER BY tags DESC;
Sample SQL Fiddle
I have a small problem with selecting some results from the database.
The results I'm trying to select have a start_date and end_date which store the full date. My client wants a feature on his site to filter all of the records by month.
Here are a couple of example records:
id start_date end_date
01 01.03.13 20.05.13
02 12.04.13 30.06.13
03 24.05.13 29.07.13
04 10.05.13 30.05.13
05 19.06.13 13.08.13
06 03.07.13 18.09.13
If the month is 05, then records id 01-04 should be displayed.
Any ideas how this would be done with MySQL?
This query will return all events whose range includes May 2013.
SELECT *
FROM MyTable
WHERE start_date < '2013-06-01'
AND end_date >= '2013-05-01'
try
select id from yourTable where month(start_date) == 5 or month(end_date)==5
In mySQL what I am trying to do is to query my table and find out how many people registered each day. In other words, I want to be able to produce the following output for one month:
1 January: 10 registrations
2 January: 150 registrations
3 January: 50 registrations
select created, regID
from registrations
Dates are in the following format in the DB: 2012-11-01 00:00:00
To get registration counts for each day of January, use this select:
select daymonth(registration_date), count(*)
from registrations
where registration_date >= '01/01/2012' and registration_date <= '01/31/2012'
group by daymonth(registration_date)
You usually use a grouping operator:
SELECT COUNT(*) AS registrations, DATE(created) AS created_date GROUP BY created_date
If created is already a DATE column, then the conversion isn't required.
Try this:
SELECT created, count(regID) FROM registrations GROUP BY created ORDER BY created ASC
SELECT DATE(DATE_REGISTERED) DATE, COUNT(*) totalRegistered
FROM tableName
GROUP BY DATE
I'm trying to write a query that returns a list of dates and the DISTINCT COUNT of User IDs for the 7 days preceding each date. The table I'm working with is simple, and looks like this:
Started UserId
"2012-09-25 00:01:04" 164382
"2012-09-25 00:01:39" 164382
"2012-09-25 00:02:37" 166121
"2012-09-25 00:03:35" 155682
"2012-09-25 00:04:18" 160947
"2012-09-25 00:08:19" 165806
I can write the query for output of an individual COUNT as follows:
SELECT COUNT(DISTINCT UserId)
FROM Session
WHERE Started BETWEEN '2012-09-18 00:00' AND '2012-09-25 00:00';
But what I'm trying to do is output this COUNT for every day in the table AND the 7 days preceding it. To clarify, the value for September 25th would be the count of DISTINCT User IDs between the 18th and 25th, the 24th the count between 17th and 24th, etc.
I tried the following query but it provides just the COUNT for each day:
SELECT
DATE(A.Started),
Count(DISTINCT A.UserId)
FROM Session AS A
WHERE DATE(A.Started) BETWEEN DATE(DATE_SUB(DATE(DATE(A.Started)),INTERVAL 7 DAY)) AND DATE(DATE(A.Started))
GROUP BY DATE(A.Started)
ORDER BY DATE(A.Started);
And the output looks like this:
DATE(A.Started) "Count(DISTINCT A.UserId)"
2012-09-18 709
2012-09-19 677
2012-09-20 658
2012-09-21 556
2012-09-22 530
2012-09-23 479
2012-09-24 528
2012-09-25 480
...
But as I said, those are just the daily counts. Initially I thought I could just sum the 7 day values, but that will invalidate the DISTINCT clause. I need the DISTINCT UserId counts for each 7 day period preceding a given date.
This query should work for you:
SELECT
DATE_FORMAT(d1.Started, '%Y-%m-%d') AS Started,
COUNT(DISTINCT d2.UserID) Users
FROM
(
SELECT
DATE(Started) AS Started
FROM
Session
GROUP BY
DATE(Started)
) d1
INNER JOIN
(
SELECT DISTINCT
DATE(Started) AS Started,
UserID
FROM
Session
) d2
ON d2.Started BETWEEN d1.Started - INTERVAL 7 DAY AND d1.Started
GROUP BY
d1.Started
ORDER BY
d1.Started DESC
Visit http://sqlfiddle.com/#!2/9339c/5 to see this query in action.
try:
Select Distinct Date(A.Started), Count(B.UserId)
From Session a
Join Session b
On b.Start Between AddDate(A.Start, day, -7) And A.Start
I'm not a MySQL guy, so the syntax might not be correct, but the pattern will work....
I have a date field in my table called date1. if i use the following query.
select * from schedule order by date1 asc
it gives the result like as jan 2011 comes before december 2010. but i need the december 2011 as the first row of the result.
Change your query to
SELECT * FROM schedule ORDER BY date1 DESC
This should do the trick.
James
select * from schedule order by date1 desc