I have a special problem with my statistics query. I want to get the most recent username, duration, and connection count of every client that connected to a server. This is the query:
SELECT name, SUM(duration) AS time, COUNT(auth) AS connections
FROM analytics
WHERE duration IS NOT NULL
GROUP BY auth
ORDER BY time DESC;
The problem is that they query isn't showing the most recent username. It takes the first entrance of the client (identified by auth) in the database even if the client changed their username already.
Is there a way to get the most recent username of the client in the query above without slowing it down a lot?
Example Table:
| id | auth | name | duration |
|----|------|------|----------|
| 1 | u123 | Fire | 50 |
| 2 | u555 | Dan | 20 |
| 3 | u123 | Ice | 30 |
What I get:
| name | time | connections |
|------|----------|-------------|
| Fire | 80 | 2 |
| Dan | 20 | 1 |
What I want
| name | time | connections |
|------|----------|-------------|
| Ice | 80 | 2 |
| Dan | 20 | 1 |
I assume that you want most recent auth order by ID coz there is no other data to get it.
SELECT A.name, B.time, B.connections
FROM analytics AS A
INNER JOIN
(
SELECT auth, MAX(ID) AS MAXID, SUM(duration) AS time, COUNT(auth) as connections
FROM analytics B
WHERE duration IS NOT NULL
GROUP BY auth
) AS B
ON A.auth = B.auth AND A.ID = B.MAXID
If you have a TimeStamp column for the recent registred user.
you can get it using the max()
select * from database where registered like (select max(registered) from database;
I wish it will help. Just analyze the idea.
Related
I have table with Columns:
---------------------------+---------+----------------------------+--------+
LogIn | CarNr |LogOut | UserID |
---------------------------+---------+----------------------------+--------+
2017-11-18-18.00.56.167000 | 7457518 | 2017-11-18-18.01.22.000000 | u39 |
2017-11-18-18.01.10.443000 | 7456618 | 2017-11-18-18.01.22.000000 | u2 |
2017-11-18-18.01.25.361000 | 7456586 | 2017-11-18-18.01.29.000000 | u64 |
2017-11-18-18.01.32.008000 | 7456612 | 2017-11-18-18.01.49.000000 | u17 |
2017-11-18-18.01.34.185000 | 7456257 | 2017-11-18-18.01.43.000000 | u2 |
2017-11-18-18.01.49.247000 | 7456345 | 2017-11-18-18.02.16.000000 | u64 |
----------------------------+---------+----------------------------+---------+
In fact, the data is much more.
Is there any way to find all users which are logged in the same CarNr within one Minute?
I need SQL-Query to find these users.
Try like this;
SQL Server
select CarNr, DATEADD(ms, -DATEPART(ms, LogIn), LogIn) from Table
group by CarNr, DATEADD(ms, -DATEPART(ms, LogIn), LogIn) having count(*) > 1
MySQL
select CarNr, DATE_ADD(LogIn, INTERVAL(-MICROSECOND(LogIn)) MICROSECOND) from Table
group by CarNr, DATE_ADD(LogIn, INTERVAL(-MICROSECOND(LogIn)) MICROSECOND) having count(*) > 1
Just Group LogIn datetime by subtracting milliseconds.
I want to get this kind of result from a mysql query:
Name | Asked Date | Granted Date | Duration
Joe | 2016-07-01 10:02:01 | 2016-07-01 10:02:05 | 10
Ben | 2016-07-01 10:04:24 | 2016-07-01 10:04:26 | 12
....
Every entries are stored in a table that look like this:
id | action_date | action_type | unique_instance | name
12 | 2016-07-01 10:02:01 | Asked | 6546532161654 | Joe
13 | 2016-07-01 10:02:06 | Granted | 6546532161654 | Joe
14 | 2016-07-01 10:05:12 | Asked | 6546532161654 | Ben
15 | 2016-07-01 10:05:15 | Granted | 6546532161654 | Ben
16 | 2016-07-01 10:06:06 | Finished | 6546532161654 | Joe
I've tried to do this query, but it didn't worked:
Select table.name as Name,
table.action_date as Asked,
g.action_date as Granted,
TIMESTAMPDIFF(SECOND, g.action_date, q.action_date) as Duration
FROM table
LEFT JOIN table g ON table.unique_instance = g.unique_instance AND g.action_type = 'Granted'
LEFT JOIN table q ON table.unique_instance = q.unique_instance AND q.action_type = 'Finished'
WHERE table.action_type = 'Asked'
AND table.unique_instance = '6546532161654'
GROUP BY table.action_date;
As stated in the comments.
Your design of your database seem incorrect as you are not even making use of your ID.
You aren't able to identify which record is unique and related to what, however you can still use the name but it's not unique, since later on in the future you are still going to have records with the same name.
My suggestion would be having action_type and ID as primary key. That way you can always use the same ID and changing the action_type accordingly.
Example (note that you can't have more than 1 record containing this):
ID: 1, action_type: Asked
ID: 1, action_type: Granted
ID: 1, action_type: Finished
In order to debug your code, this query should return just 1 record:
select * from table where unique_instance = '6546532161654' AND action_type = 'Granted'
But since unique_instance is not unique after all, it is going to grab other unwanted data.
Note: Your query is correct.
I have below table -
mysql> select * from appdata;
+------------+----------+-----------+
| timestamp | appName | Bytes |
+------------+----------+-----------+
| 1454169299 | DNS | 123456 |
| 1454169292 | DNS | 12342 |
| 1454169292 | Facebook | 1234222 |
| 1454169297 | Youtube | 123422233 |
+------------+----------+-----------+
I wanted to sum the byte and show the single application and consumed bytes
I executed
mysql> select appName,Sum(Bytes) from appdata group by appName;
+----------+------------+
| appName | Sum(Bytes) |
+----------+------------+
| DNS | 135798 |
| Facebook | 1234222 |
| Youtube | 123422233 |
+----------+------------+
now I want to run a single query which gives me the app which has consumed more bytes
youtube 123422233
Facebook 1234222
DNS 135798
I am running
select appName,Sum(Bytes) from appdata group by appName
(select appName,Bytes FROM appdata ORDER BY Bytes desc limit 3);
But its not working, Please let me know how can i achieve this.
Thanks.
Try this:
SELECT appname, SUM(bytes) FROM appdata GROUP BY appname ORDER BY SUM(bytes) DESC LIMIT 3;
You need to order by the sum of Bytes descending, this way you'll get the highest numbers first.
I have a bidding system in place. The user enters how much he wants to bid, which then sends a request via ajax to a PHP script, which then gets what rank that bid would place under the existing bids, and then displays it back to the bidder. This allows him to increase his bid to get the rank he wants.
For example
+-----------+------------+
| bidder_id | bid_amount |
+-----------+------------+
| 1 | 20 |
| 2 | 20 |
| 3 | 30 |
| 5 | 40 |
| 6 | 10 |
+-----------+------------+
The user bids 15$, the query gets the rank as 5th.
How would this query look like? Is is possible to insert a fake row with the new user's bid and then order everything?
Something simple like this should do it;
SELECT COUNT(*)+1 rank
FROM bids
WHERE bid_amount > 15
An SQLfiddle to test with.
I have two MySql tables:
users(id_user, name, age, gender ).
ways(#id_user,id_way, start, end, date).
What I want is to retrieve all the ways with their corresponding users details.
So my result would be like this:
id_way | start | end | date | id_user | name | age | gender
---------------------------------------------------------------------------
2 | place1 | place2 | 12/06/2013 | 145 | john | 28 | m
Have you tried JOIN?
SELECT ways.id_way, ways.start, ways.end, ways.date, users.*
FROM ways JOIN users USING (id_user)