i have some queries which group datasets and count them, e.g.
SELECT COUNT(*)
FROM `table`
GROUP BY `column`
now i have the number of rows for which column is the same, so far so good.
problem is: how do i get the aggregate (min/max/avg/sum) values for those “grouped” counts. using a subquery sure is the easiest, but i was wondering if this is possible within this single query
For min and max you can ORDER BY and fetch the first row. For sum/avg/other aggregates you would need a subquery.
In MySQL you should be able to do this all at once. My tests seem to indicate that this works.
| date | hits |
|-------------------|
| 2009-10-10 | 3 |
| 2009-10-10 | 6 |
| 2009-10-10 | 1 |
| 2009-10-10 | 3 |
| 2009-10-11 | 12 |
| 2009-10-11 | 4 |
| 2009-10-11 | 8 |
-------------------
SELECT COUNT(*), MAX(hits), SUM(hits) FROM table GROUP BY date
| COUNT(*) | MAX(hits) |
|-----------|-----------|
| 4 | 6 |
| 3 | 12 |
-----------------------
SUM, MIN and AVG also work. Is this what you are looking for?
| date | hits |
|-------------------|
| 2009-10-10 | 3 |
| 2009-10-10 | 6 |
| 2009-10-10 | 1 |
| 2009-10-10 | 3 |
| 2009-10-11 | 12 |
| 2009-10-11 | 4 |
| 2009-10-11 | 8 |
I think knittl was trying to do something like this:
select min(hits), max(hits), avg(hits), sum(hits)<br>
from table
group by date
Related
My table looks like
+----+------+----+
| Id | from | to |
+----+------+----+
| 1 | 1 | 10 |
| 1 | 10 | 12 |
| 1 | 12 | 23 |
| 1 | 24 | 26 |
| 2 | 2 | 8 |
| 2 | 3 | 4 |
| 2 | 4 | 10 |
+----+------+----+
Now I want to group by Id and select the most spanning range.
So the result table should look like this:
+----+-------------+
| Id | range |
+----+-------------+
| 1 | 1-23, 24-26 |
| 2 | 2-10 |
+----+-------------+
I do not even know how to start.
Thanks in advance for the help!
You can achieve something similar to this by writing a MySQL select query with the CONCAT() function. You can use GROUP BY to categorize the data. But keep in mind that from,to and range are reserved words in MySQL. Therefore, I'll use fooFrom, fooRange and fooTo as column name for this example
Example:
SELECT Id, CONCAT(fooFrom, "-", fooTo) AS "fooRange" FROM fooTable GROUP BY id;
This example code will output:
id | fooRange
1 | 5-6
2 | 12-88
But I'm not sure about how to concat all the content into a one-column value.
How can I make a difference to get time between first and last line during my SQL query ?
Datas are the following ones:
|--------|-----------|---------------------|
| EST_Id | EST_Token | EST_Datetime |
|--------|-----------|---------------------|
| 1 | vexef | 2020-10-17 16:13:01 |
| 2 | vexef | 2020-10-17 16:13:21 |
| 3 | vexef | 2020-10-17 16:14:31 |
| 4 | vexef | 2020-10-17 16:13:51 |
| 5 | fardd | 2020-10-17 17:00:11 |
| 6 | fardd | 2020-10-17 17:00:17 |
| 7 | fardd | 2020-10-17 17:00:19 |
|--------|-----------|---------------------|
For example for the token vexef I should have 50 seconds.
This is what I have tried:
SELECT *, TIMEDIFF(MAX(EST_Datetime), MIN(EST_Datetime)) AS diff FROM table GROUP BY EST_Token ORDER BY EST_Id ASC
The query looks working but diff returns me 0.
Thanks.
You seem to want aggregation:
select est_token, timestampdiff(second, min(est_datetime), max(est_datetime)) diff
from mytable
group by est_token
This gives you one row per est_token, with the difference between the earliest and latest est_datetime, expressed in seconds.
I am in need of some help with mysql since I'm a novice with it. I need to count the values of different tables from different databases and put them all in one table, after that make an index for each count, for example:
_______________________
| | |
| Indexes | Counts |
|___________|___________|
| | |
| Index 1 | Count 1 |
|___________|___________|
| | |
| Index 2 | Count 2 |
|___________|___________|
| | |
| Index 3 | Count 3 |
|___________|___________|
| | |
| Index 4 | Count 4 |
|___________|___________|
But like I'm doing it right now (with UNION) it appears like:
___________
| |
| Counts |
|___________|
| |
| Count 1 |
|___________|
| |
| Count 2 |
|___________|
| |
| Count 3 |
|___________|
| |
| Count 4 |
|___________|
Any idea? Thanks in advance.
EDIT:
The code I'm currently using is this
SELECT COUNT(*) AS "db1" FROM `db1`.`tb1`
UNION
SELECT COUNT(*) AS "db2" FROM `db2`.`tb2`
Try this.
SELECT "db1.tb1" as "INDEXES", COUNT(*) AS "COUNTS" FROM `db1`.`tb1`
UNION
SELECT "db2.tb2" as "INDEXES", COUNT(*) AS "COUNTS" FROM `db2`.`tb2`
I want to find the average of the following data via mysql query (assume these are 719 rows).
| 1 |
| 3 |
| 1 |
| 2 |
| 2 |
| 1 |
| 1 |
| 2 |
| 1 |
| 1 |
| 2 |
| 1 |
| 2 |
| 1 |
| 2 |
| 1 |
| 1 |
+----------+
719 rows in set (2.43 sec)
SELECT COUNT(*) FROM osdial_agent_log WHERE DATE(event_time)='2015-11-01' GROUP BY lead_id;
I ran this query to get that data
Can someone help me to find the average for the above data.
Use
SELECT AVG(total)
FROM (SELECT COUNT(*) AS total
FROM osdial_agent_log
WHERE DATE(event_time)='2015-11-01'
GROUP BY lead_id) t
In my access database, we keep track of two sets of dates. One set is for date of membership dues payments, the other set is date of other contributions (a non-membership donation.) There are multiple dates for each person depending on number of payments made for each type.
Example:
+----+---------------+---------------+
| ID | Dues_Date | Cont_Date |
+----+---------------+---------------+
| 1 | 01/01/15 | 09/12/11 |
| | 01/01/14 | |
| | 01/01/13 | |
| 2 | 07/30/14 | 06/20/13 |
| | | 11/12/11 |
+----+---------------+---------------+
First I needed to know the most recent payment for each of the two fields so I ran a query that tells me the MAX (most recent) date for each field.
Example Query:
+----+---------------+---------------+
| ID | Max Dues_Date | Max Cont_Date |
+----+---------------+---------------+
| 1 | 01/01/15 | 09/12/11 |
| 2 | 07/30/14 | 06/20/13 |
| 3 | 02/11/13 | 09/16/14 |
| 4 | 07/30/12 | 06/20/11 |
| 5 | 12/13/13 | 11/12/14 |
+----+---------------+---------------+
Now I need a third field in the same query to compare the results of the first two fields and show which is the MAX of those two.
I have column 2 and 3 in the query; how can I take that and create column 4 in the same query?
Example Query:
+----+---------------+---------------+-----------------+
| ID | Max Dues_Date | Max Cont_Date | Max Date(DD&CD) |
+----+---------------+---------------+-----------------+
| 1 | 01/01/15 | 09/12/11 | 01/01/15 |
| 2 | 07/30/14 | 06/20/13 | 07/30/14 |
| 3 | 02/11/13 | 09/16/14 | 09/16/14 |
| 4 | 07/30/12 | 06/20/11 | 07/30/12 |
| 5 | 12/13/13 | 11/12/14 | 11/12/14 |
+----+---------------+---------------+-----------------+
Try adapting this to your own scenario:
SELECT tblTest.DueDate, tblTest.ContDate, [DueDate]-[ContDate] AS Test, IIf([Test]<0,[ContDate],[DueDate]) AS MaxRes
FROM tblTest;
"Test" finds which is the later date, ContDate or Due Date. The IIf statement selects the later date.
Does this help?