Multiple select query on sql - mysql

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.

Related

Find users, which are logged on the same Car

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.

How to display the current recent rows according to time?

The following database generated by (select * from users).How can i display the recent rows according to time(i.e for time i had used date +%s).But here it displays all timing .I nee dthe rows with the recent update time.
| time | userid | groupid |
+------------+----------+-----------+
| 1477044791 | spolishe | MEMS |
| 1477044791 | ssarkar | HCG |
| 1477044791 | svaranas | PDSP_DES |
| 1476868065 | dnehra | HCG |
| 1476868065 | dprabhu | PDSP_DES |
My expected Output:(but it sould be generated by using some linux commands like date +%s).Is there any linux commands which fetches the only the last recent time rows (or) Is it possible to use inside insert mysql query by storing in some variable name.
| time | userid | groupid |
| 1477044791 | spolishe | MEMS |
| 1477044791 | ssarkar | HCG |
| 1477044791 | svaranas | PDSP_DES |
Do you want to represent the time in proper date formant? if so;
select from_unixtime(time), * from user order by time desc
You could specify that you only want items where the time is equal to that of the largest value that column holds in the table.
SELECT * FROM users where time = (Select max(time) from users);

MySQL Query isn't showing most recent username

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.

Return one record with multiple entries on the same date

I have a table that looks like
| OPDNo |DispensedDate | Drugname | CreatedBy|
| 011650/16 | 6/29/2016 |folic acid | admin |
| 011650/16 | 6/29/2016 |multivite | admin |
| 011650/16 | 6/21/2016 |fersolate | asah |
| 011650/16 | 6/21/2016 |amoxicyllin| eantwi |
| 025343/13 | 5/23/2016 |aspirin | emelia |
And i want the record selection to be like this when i query like
select * from dispensary where OPNo='011650/16'
| OPDNo |DispensedDate | DrugName | CreatedBy|
| 011650/16 | 6/29/2016 |folic acid| admin |
| 011650/16 | 6/21/2016 |fersolate | asah |
That is I want only one record for every OPNo on a particular date. If an OPDNo has one or more entries on the same date, then the query should return only entry. Probably the first entry for each date if an OPDNo has more entries on that particular date.
You can try this...
select * from dispensary do
where do.Drugname in (select max(Drugname) from dispensary di
where di.OPDNo = do.OPDNo
and di.DispensedDate = do.DispensedDate)
and do.OPDNo = '011650/16'
but keep in mind that the result will not be the first drug
Try this
select * from (select * from dispensary order by DispensedDate desc) as a where OPNo='011650/16'
group by DispensedDate;
If your DispensedDate is of type "DateTime" then you can try below
select * from (select * from dispensary order by DispensedDate desc) as a where OPNo='011650/16'
group by date_format(DispensedDate, '%m/%d/%Y');
Let me know if this is working.

Controlling what value appears in a column while doing a join

This one's kind of complicated, so hopefully I can make it clear.
I have two tables:
views:
+---------------------+-------------+------------------+
| time | remote_host | referer |
+---------------------+-------------+------------------+
| 0000-00-00 00:00:00 | 10.0.13.2 | http://foo.com/a |
| 0000-00-00 00:00:00 | 10.0.13.1 | http://foo.com/b |
| 0000-00-00 00:00:00 | 10.0.13.2 | http://moo.com |
| 0000-00-00 00:00:00 | 10.0.13.2 | http://hi.com |
| 0000-00-00 00:00:00 | 10.0.13.1 | http://foo.com/c |
+---------------------+-------------+------------------+
test_websites:
+----+----------------+------+
| id | url | name |
+----+----------------+------+
| 1 | http://foo.com | |
| 2 | http://moo.com | |
+----+----------------+------+
I have a query that very nearly does what I want:
SELECT COUNT(*) as count, remote_host, url FROM test_websites
JOIN views ON referer LIKE CONCAT(url, '%')
GROUP BY test_websites.url
ORDER BY count DESC LIMIT 10;
Results look like this:
+-------+-------------+----------------+
| count | remote_host | url |
+-------+-------------+----------------+
| 3 | 10.0.13.2 | http://foo.com |
| 1 | 10.0.13.2 | http://moo.com |
+-------+-------------+----------------+
To explain, I'm trying to get the top 10 viewed websites, however the website URLs are defined in test_websites. Since http://foo.com is an entry in test_websites, all entries that start with http://foo.com should be counted as "one website." Hence the join is based on a LIKE condition, and it's correctly counting 3 for http://foo.com in the results.
So, the problem is that I want remote_host to be that entry that appears the most for those rows in views that start with http://foo.com. In this case, there are two rows starting with http://foo.com in the views table that have 10.0.13.1 as the remote_host, and so the results should show that 10.0.13.1 the remote_host column, and not the remote_host that appears with the first entry that starts with http://foo.com, as it is doing now.
Thanks.
UPDATED
Please try the following corrected query:
SELECT
COUNT(*) as count,
(
SELECT A.remote_host
FROM views AS A
WHERE A.referer LIKE CONCAT(test_websites.url, '%')
GROUP BY A.remote_host
ORDER BY COUNT(1) DESC
LIMIT 1
) AS max_count_remote_host,
test_websites.url
FROM
test_websites
JOIN views ON views.referer LIKE CONCAT(test_websites.url, '%')
GROUP BY
test_websites.url
ORDER BY
count DESC LIMIT 10;
Here you could find a working SQL Fiddle example.