have this portion of a large table T;
+---------------+------------+
| session_id | visit_time |
+---------------+------------+
| 4f89cebc109f9 | 1334431476 |
| 4f89cf283d21c | 1334431528 |
| 4f89cf283d21c | 1334431534 |
| 4f89cf3b350a6 | 1334431547 |
| 4f89cf42ab640 | 1334431554 |
+---------------+------------+
I want to find number of session_id weekday-wise. Session_id is not primary key. So I tried:
select count(distinct(session_id)) from T group by weekday(from_unixtime(time))
But it won't work because if same session_id has visit_time on two different sundays, then it counts them as 1, although it rightly counts 1 when same session_id has visit_time on same sunday.
The expected thing is : I want to know how many session-ids have visit day as sun, mon etc. If a session_id has visited on two different sundays, then they are counted twice, but if on same sunday, then only 1 count.
So how can I do it in Mysql ?
use WEEK instead of WEEKDAY
SELECT WEEK(FROM_UNIXTIME(time)) WeekNo,
DATE_FORMAT(FROM_UNIXTIME(time),'%a') WeekName,
COUNT(DISTINCT(session_id))
FROM T
GROUP BY WEEK(FROM_UNIXTIME(time)),
DATE_FORMAT(FROM_UNIXTIME(time),'%a')
WEEK
Related
I want to get the count of all packages ordered for the whole week and get the package_id of the one with the highest frequency and also has the status='active' in my package table
these are my database tables
sales
+------------+------------------+
| package_id | datesales |
+------------+------------------+
| 1 | timestamp |
| 2 | timestamp |
| 1 | timestamp |
| 1 | timestamp |
| 2 | timestamp |
| 2 | timestamp |
| 3 | timestamp |
+------------+------------------+
packages
+------------+------------------+
| package_id | status |
+------------+------------------+
| 1 | inactive |
| 2 | active |
| 3 | active |
+------------+------------------+
I tried using this sql but I'm not really good with aggregation
SELECT count(product_id) as product_id from i.sales
where [i dunno how to put the sql for package table here]
i.date(datesales) <= curdate() and
i.date(datesales) >= curdate() - interval 6 day
group by product_id
with the above example in sales table, since I have 3 counts of package_id=1 and also 3 counts of package_id=2,
I want to get the id for package_id=2 since it is the highest frequency of orders and it has the status='active' in my package table
I think you basically want order by and limit and join:
select package_id, count(*) as cnt
from sales i join
packages p
using (package_id)
where -- i.date(i.datesales) <= curdate() and -- I doubt you have future start dates
i.datesales >= curdate() - interval 6 day and
p.status = 'active'
group by package_id
order by count(*) desc
limit 1;
Here is a db<>fiddle.
I'm trying to do a query to get first and last timestamp of each unique user.
Database looks like this:
| ID | EventID | Timestamp | Person | Number |
--------------------------------------------------------
| 1 | 2 | 2015-01-08 17:31:40 | 7 | 5 |
| 2 | 2 | 2015-01-08 17:35:40 | 7 | 4 |
| 3 | 2 | 2015-01-08 17:38:40 | 7 | 7 |
--------------------------------------------------------
I'm trying to put together a MySQL query that will do the following:
SUM of number field for each unique user.
Time difference (in hours) between first and last row for each unique user.
I would imagine that if I could get the first and last timestamp for each user, I should be able to use timediff to get the time difference in hours.
What I've got so far:
SELECT
person,
SUM(number) AS 'numbers_all_sum'
FROM database
WHERE eventid = 2
GROUP BY person
ORDER BY numbers_all_sum DESC
Any help would be greatly appreciated.
Something like this:
SELECT
Person
MIN(Timestamp),
MAX(Timestamp),
SUM(number) AS 'numbers_all_sum'
FROM database
WHERE eventid = 2
GROUP BY person
I have a table that contains two fields name and date
+-------+------------+
| name | date |
+-------+------------+
| B | 28-09-2015 |
| A | 28-09-2015 |
| B | 29-09-2015 |
| A | 29-09-2015 |
| B | 30-09-2015 |
| A | 30-09-2015 |
| B | 01-10-2015 |
| C | 01-10-2015 |
| B | 02-10-2015 |
| B | 03-10-2015 |
| C | 03-10-2015 |
| B | 04-10-2015 |
+-------+------------+
I went compare date now with date for my data and get this table
+-------+------------+
| name | Number |
+-------+------------+
| A | -4 day |
| C | -1 day |
| B | 0 day |
+-------+------------+
Thank you
You should group by each name, get the max date and use curdate() to get the difference. Use DATE() to convert from from datetime to date for calculation.
select name, max(DATE(datecolumn)) - curdate()
from tablename
group by name
order by max(DATE(datecolumn)) - curdate()
Step one is to get a list of the newest dates. You can use this with MAX(date) but that alone will just get you the newest date in the table. You can tell the database you want the newest date per name with a GROUP BY clause. In this case, GROUP BY name.
SELECT name, MAX(date)
FROM names
GROUP BY name
Now you can do some date math on MAX(date) to determine how old it is. MySQL has DATEDIFF to get the difference between two dates in days. CURRENT_DATE() gives the current date. So DATEDIFF(MAX(date), CURRENT_DATE()).
SELECT name, DATEDIFF(MAX(date), CURRENT_DATE()) as Days
FROM names
GROUP BY name
Finally, to append the "days" part, use CONCAT.
SELECT name, CONCAT(DATEDIFF(MAX(date), CURRENT_DATE()), " days") as Days
FROM names
GROUP BY name
You can play around with it in SQLFiddle.
I would recommend not doing that last part in SQL. You won't get the formatting quite right ("1 days" is bad grammar) and the data is more useful as a number. Instead, do the formatting at the point you want to display the data.
I have a Visits table, structured like the below:
+--------------------------------------+
| ID | Date | Time | Session |
+--------------------------------------+
| 1 | 05-18-2014 | 20:15:10 | 1 |
| 2 | 05-18-2014 | 20:15:20 | 1 |
| 3 | 05-18-2014 | 21:40:20 | 2 |
| 4 | 05-18-2014 | 21:45:30 | 1 |
| 5 | 05-18-2014 | 21:50:50 | 3 |
+--------------------------------------+
The session column is the user's session ID. I would like to query the table to get the hourly total and unique visitors, to get a result like:
+-----------------------+
| Time | Total | Unique |
+-----------------------+
| 20 | 2 | 1 |
| 21 | 3 | 2 |
+-----------------------+
Unique visitors are visitors with sessions that have never been seen before, anywhere in the Visits table.
The below only selects unique visitors inside each hour:
SELECT COUNT(*) Total, COUNT(DISTINCT Session) Unique, HOUR(Time) Time
WHERE Date = '05-18-2014'
FROM Visits
GROUP BY HOUR(Time)
The following seems to work, however requires two queries, and a sub-query:
SELECT COUNT(*) Total, HOUR(Time) Time
FROM Visits
GROUP BY HOUR(Time);
SELECT COUNT(*) Unique, HOUR(Time) Time
FROM (
SELECT *
FROM Visits
GROUP BY Session
ORDER BY Date, Time DESC
) UniqueVisits
WHERE Date = '05-18-2014'
GROUP BY HOUR(Time);
Is there a simpler way to get the two totals?
I think by "distinct" you mean that you only want one session counted once (during the first hour). If so, you can do this:
select max(h.total) as total, count(firstvisit.session) as Firsts, h.hr
from (select hour(time) as hr, count(*) as total
from visits v
where Date = '05-18-2014'
group by hour(time)
) h left outer join
(select session, min(hour(time))as hr
from visits v
where Date = '05-18-2014'
group by session
) firstvisit
on h.hr = firstvisit.hr
GROUP BY h.hr;
I want to get the count of the registered users in the past 7 days, grouped.
+-----+------------+--------------+
| id | username | created |
+-----+------------+--------------+
| 1 | Vlad | 1360168194 |
+-----+------------+--------------+
| 2 | Test | 1360168194 |
+-----+------------+--------------+
This is my table. I want to have 7 rows of results with the date of the day, and count(id) as the result for the users that registered.
I tried different solutions and none of them really fitted my needings. Are there any ideas?
SELECT DATE(FROM_UNIXTIME(columName)), COUNT(ID) totalCOunt
FROM tableName
WHERE DATE(FROM_UNIXTIME(columName)) BETWEEN CURDATE() + INTERVAL -7 DAY AND CURDATE()
GROUP BY DATE(FROM_UNIXTIME(columName))
SQLFiddle Demo
Other Source(s)
MySQL Date and Time Functions