We have a list of page url and dateTime record on database.
Is it possible to get the hour where user have access it the most?
The column time displays what time users have access the url the most.
The expected result should look like these:
page URL | # visitor per day | Date | Max visitor/hour| time
----------+-------------------+--------------------+-----------------+--------------
www.url1 | 49 | 07/07/2017 | 23 | 12:00 - 13:00
www.url2 | 88 | 07/07/2017 | 39 | 7:00 - 8:00
In Subquery get the max hour per each URL and day.
Then join that to the main table, to get the required records.
If you need max hour irrespective of day, then just drop Date column from sub query and the join condition.
select *
from maintable
join
( select pageURL, Date, max(MaxVisitorPerHour) as maxhour
from table
group by pageURL, Date) subq
on
(maintable.pageURL=subq.pageURL AND maintable.Date=subq.Date and
maintable.maxVisitorPerHour=subq.maxhour)
Related
I have two tables which I want to perform a query on.
The first table Events has a column of events and a column of dates.
event | date
------------+------------
"Christmas" | 2019-12-25
"Blue moon" | 2020-05-16
"Earth day" | 2020-03-26
The second table Periods has a column with names of periods, and two columns with the start and end dates of the periods.
period | start date | end date
--------------------+-------------+-----------
"Shark week" | 2019-03-20 | 2019-03-27
"Easter weekend" | 2020-04-12 | 2020-04-15
"Ramadan" | 2020-04-20 | 2020-05-24
I want the query to return a list of events and dates from the Events table where the event occurs between the start and end date of any period.
The query would give the following result on the above tables:
event | date
------------+------------
"Blue moon" | 2020-05-16
"Earth day" | 2020-03-26
I have limited knowledge of SQL. The best I can do is to query the events table for events that occurred during a single period. Like this:
SELECT event, date FROM 'Events'
WHERE ("2019-03-20" < date) AND (date < "2019-03-27")
I have looked at related questions for querying between two dates. The related questions don't cover cases where the dates come from columns of a table.
This sounds like a join:
select e.*, p.*
from events e join
periods p
on e.date >= p.start_date and e.date <= p.end_date;
I am trying to create high charts time line graph for my school attendance but I get slightly different records appearing in my result.
The table that stores attendance is :
+++++++++++++++++++++++++++++++++
Id | Date | AttendTime(varchar)
--------------------------------
1 | 20160815 | 7:51
2 | 20160815 | 7:53
3 | 20160815 | 8:01
-------------------------------
I am using following sql to get the data
SELECT
COUNT(Id) as Total
FROM Attendance
WHERE Date = '20160815' AND
HOUR(STR_TO_DATE(AttendTime, '%H:%i')) <= HOUR(STR_TO_DATE('7:30', '%H:%i'))
And I should get 0 as there are none but I get 3 records. What am I doing wrong?
Thank you.
If you are simply trying to see how many attendances were before 7:30, you do not need the HOUR function:
SELECT
COUNT(Id) as Total
FROM Attendance
WHERE Date = '20160815'
AND STR_TO_DATE(AttendTime, '%H:%i') <= STR_TO_DATE('7:30', '%H:%i')
Hello guys I need help with this function, I'm using MySQL database server
version: 5.5.47-0ubuntu0.14.04.1 - (Ubuntu).
I want to be able to get how many "weeks" it took a worker to get hired. i have a table that stores log data whenever the worker's status change.
logid workerid statusid timestamp
1000 10 1(available) 2016-04-10
1001 10 2(Hired) 2016-04-24
what i want to have is :
It took worker 10 two weeks to get hired.
I already looked at DATEDIFF(first date, second date) function but I have to use two different where conditions for each date, I have no idea how to do this?
You need to join same table and to use DATEDIFF():
SELECT
e1.workerid,
DATEDIFF(e2.`timestamp`, e1.`timestamp`) daysDiff,
ROUND(DATEDIFF(e2.`timestamp`, e1.`timestamp`) / 7, 0) weeksDiff
FROM
employees e1
INNER JOIN employees e2 ON e1.workerid = e2.workerid
WHERE
e1.statusid = 1
AND e2.statusid = 2
Output:
+----------+----------+-----------+
| workerid | daysDiff | weeksDiff |
+----------+----------+-----------+
| 10 | 14 | 2 |
+----------+----------+-----------+
1 row in set
I am trying to make a graph that has a point for each day showing the number of horses present per-day.
This is example of data I have (MySQL)
horse_id | start_date | end_date |
1 | 2011-04-02 | 2011-04-03 |
2 | 2011-04-02 | NULL |
3 | 2011-04-04 | 2014-07-20 |
4 | 2012-05-11 | NULL
So a graph on that data should output one row per day starting on 2011-04-02 and ending on CURDATE, for each day it should return how many horses are registered.
I can't quite wrap my head around how I would do this, since I only have a start date and an end date for each item, and I want to know per-day how many was present on that day.
Right now, I do a loop and a SQL query per day, but that is - as you might have guesses - thousands of queries, and I was hoping it could be done smarter.
If a day between 2011-04-02 and now contains nothing, I still want it out but with a 0.
If possible I would like to avoid having a table with a row for each day containing a count.
I hope it makes sense, I am very stuck here.
What you should have, is a table containing just dates from at least the earliest date in your current table till the current date.
Then you can use this table to left join it something like this:
SELECT
dt.date,
COUNT(yt.horse_id)
FROM
dates_table dt
LEFT JOIN your_table yt ON dt.date BETWEEN yt.start_date AND COALESCE(end_date, CURDATE())
GROUP BY dt.date
Be sure to have a column of your_table in the COUNT() function, otherwise it counts the NULL values too.
The COALESCE() function returns the first of its parameter which isn't NULL, so if you don't have an end_date specified, the current date is taken instead.
I have a db table named APPLICATION that has the following columns
applicationnumber varchar,
createddate datetime,
applicantname varchar,
material varchar,
location varchar
I am needed to write a query that would display the number of applications created for each month for the locations
Eg. The query result should be something like below
Location | Jan2012 | Feb2012 | Mar2012 | Apr2012
-----------------------------------------------------------------
London | 34322342 | 4342424 | 54353454 | 5434
Chicago| 43242345 | 9943455 | 85748294 | 544
The result is the number of applications created in each month for the specific location.
Each column will execute the same query logic, with just the month changing.
I tried using the MONTH() function, but I need the month matrix as a column and not as a row.
You can use the DATE_FORMAT function on the createddate field to only get the month and year and then GROUP BY location. Something like this should do it:
SELECT
`location`,
COUNT(`applicationnumber`),
DATE_FORMAT(`createddate`, '%M %Y') AS `date`
FROM `APPLICATION`
GROUP BY `location`;
Should give a result with the count of rows per month.