I would like to check the last update date and time (recordTime) for every treehugger id (TreeHuggerId) and below is what I did. The output was not the last update time according to the below query. Please advice. Thank you.
SELECT `recordTime`, DISTINCT `TreeHuggerId` FROM `SENSOR_TREEHUGGERS`
WHERE `TreeHuggerId` < 20000 and `TreeHuggerId` > 10000
ORDER BY `recordTime` desc
You have to select MAX(recordTime) for every TreeHuggerId for that and you don't need distinct
SELECT TreeHuggerId,MAX(recordTime) FROM SENSOR_TREEHUGGERS
Then at the end
GROUP BY TreeHuggerId
Like this
SELECT TreeHuggerId,MAX(recordTime) FROM SENSOR_TREEHUGGERS
WHERE TreeHuggerId BETWEEN 10000 AND 20000
GROUP BY TreeHuggerId
Related
Can anyone please help me. I am trying to calculate the difference between record entry 1 and record entry 2 and so and so on date and time in two separate records
I want to add another row to calculate the difference Between Entry 1 and Entry 2. I know that if i did have a check in and check out then i could just subtract time out from time in and its sorted. But have never tried between records
Update And the Code that i'm using is:
SELECT USERINFO.name, CHECKINOUT.CHECKTIME
FROM CHECKINOUT, USERINFO
WHERE (((USERINFO.name)=[Enter Name]))
ORDER BY CHECKINOUT.CHECKTIME DESC;
Select
name,
DateValue(CHECKTIME) AS Date,
TimeValue(CHECKTIME) AS Time
From CHECKOUT
It could be:
Select *,
(CheckTime - (Select Top 1 CheckTime
From [Employee Check] As T
Where T.Name = [Employee Check].Name
And T.CheckTime < [Employee Check].CheckTime
Order By T.CheckTime Desc)) As CheckTimeDiff
From [Employee Check]
I have a table with every login by all users.
I want to run a query that will pull the number of times each user logs in but limit it to 4 if the user logged in more than 4 times on a day.
And then do a sum to get the total number of logins.
Further to this I want to pull back the time frame for the total number of logins. So I specify the total number of logins as 100 then the query must pull back the earliest date, going back from today and counting the number of logins (limited at 4 if above 4) per user.
My query so far to get the list of totals limited to 4 per user:
SELECT (case when (count(l.user_id) > 4) then 4 else count(l.user_id) end) as cappedcount
FROM `logins` l
where l.store_id = 908
and l.login_dt > '2018-04-17 00:00:00' and l.login_dt < '2018-04-18 23:59:59'
group by l.user_id order by cappedcount desc
I'm specifying the date range at the moment but don't want to do that in the final query.
If I understand correctly, you only want to look at the last four logins per user and day and ignore their earlier logins. From this set you want the last 100 logins.
So the first task is to get the four last logins per user and day, which would usually be solved with window functions, but MySQL doesn't feature them. So count in a subquery instead (which may take long):
select *
from logins
where
(
select count(*)
from logins later
where later.user_id = logins.user_id
and date(later.login_dt) = date(logins.login_dt)
and later.login_dt > logins.login_dt
) < 4
order by login_dt desc
limit 100;
I suggest to provide the following index for this query:
create index idx_logins on logins (user_id, login_dt);
What is the version of MySQL you user? Because as far as I know with clause is only supported in recent versions of MySQL.
I believe the answer to your first request is something like :
select sum(cntx) from (
select user_id, date(login_time), least(count(*), 4) cntx
from logins
where login_time between '2018-04-10 00:00:00' and '2018-04-17 00:00:00'
group by user_id, date(login_time)
) x
as you can view it in sqlfiddle.com.
For your second question, I have following answer, I believe it's not the best solution, but it works on MySQL 5.6. In next MySQL version (MySQL 8) you can use with clause which provides better solution for this question. I use views in the solution to skip duplicate queries:
create view xlogins as
select user_id, date(login_time) xdt, least(count(*), 4) xcnt
from logins
group by user_id, date(login_time);
create view xxlogins as
select distinct xdt, (select sum(x2.xcnt)
from xlogins x2
where x2.xdt >= x1.xdt) sumx
from xlogins x1;
select min(x1.xdt)
from xxlogins x1
join xxlogins x2 on x1.xdt < x2.xd
where x1.sumx >= 100
and x2.sumx <= 100
Find the solution in this sqlfiddle.com, I've just changed the 100 to 10.
I have a table, described like so:
Table1
id (int),
link (varchar512),
text (varchar80),
status (varchar10),
created (timestamp),
updated (timestamp),
user (varchar)
What I need to do is get the total count of rows per user between two timestamps.
So, for example, let's say I want to get the total number of rows for users in the database. That is just a simple
SELECT user, COUNT(*) FROM table_name GROUP BY user;
If I want to get all the rows, for say October, I can do:
SELECT * FROM table_name WHERE created > "2016-10-01 00:00:00" and created < "2016-11-31 23:59:59"
My problem, is I cannot combine the two. I try, and I get syntax errors. I think that I need to run the where query, and then do a count based on that, but I'm not sure how do to that.
Hope this helps.
SELECT user, count(*)
FROM table_name
WHERE created > "2016-10-01 00:00:00" and created < "2016-11-31 23:59:59"
GROUP BY user;
SELECT user, COUNT(*)
FROM table_name
WHERE created >= '2016-10-01'
and created < '2016-12-01'
GROUP BY user;
BTW there is no date 2016-11-31 since November has only 30 days.
This honestly sounds like a job for a function in MySql but I'm wondering if there's a way to make a query that selects the date of the record that achieves the count = x
Setup: 1000 records each having the same qualifying conditions.. lets say user_id and visit information and a create_date
Desired Query result: Select the date of the 100th visit
SELECT create_date
FROM user_visits
HAVING COUNT(id) = 100;
You can use order by on your auto_increment column and limit 99,1 to pick 100th visit
SELECT create_date
FROM user_visits
ORDER BY your_auto_increment_column
LIMIT 99,1
I need to query a database which will return the number of people subscribed to a particular service during that month.
I use the below query
select subscriberid, count(*) from ABC where updated time like '2013-05-%' ;
In this query I need to update the Updatedtime field to be 2013-06-% when the next month comes and then 07 when the next to next month comes. I want the query to be updated automatically when the next month comes instead of manually changing it every time.
Also note that I want the data for a particular month at a time, so please don't suggest grouping by month as an answer.
One way to do it
SELECT subscriberid, COUNT(*)
FROM ABC
WHERE YEAR(updated_time) = YEAR(CURDATE())
AND MONTH(updated_time) = MONTH(CURDATE())
or
SELECT subscriberid, COUNT(*)
FROM ABC
WHERE updated_time BETWEEN ADDDATE(LAST_DAY(SUBDATE(CURDATE(), INTERVAL 1 MONTH)), 1)
AND LAST_DAY(CURDATE())
The following should work fine:
SELECT
subscriberid,
count(*)
from
ABC
where
updatedtime LIKE CONCAT(DATE_FORMAT(NOW(),'%Y-%m'), '-%')
I think you can use DATE_FORMAT function
SELECT subscriberid, count(*) as total
FROM ABC
WHERE DATE_FORMAT(updated_time, "%Y-%m") = "2013-05";
Use the following query:
SELECT subscribersid, updated, COUNT(*) from subscribers
WHERE YEAR(updated) = YEAR(NOW())
AND MONTH(updated) = MONTH(NOW())
You can see it working in this SQL Fiddle
Hope this helps