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`
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.
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?
I am trying to get average of latency for each items that holds into two separate mysql table. Let me more clarify that I have two mysql tables as below,
table: monitor_servers
+-----------+-----------------+
| server_id | label |
+-----------+-----------------+
| 1 | a.com |
| 2 | b.com |
+-----------+-----------------+
table: monitor_servers_uptime
+-------------------+-----------+-----------+
| servers_uptime_id | server_id | latency |
+-------------------+-----------+-----------+
| 1 | 1 | 0.4132809 |
| 3 | 1 | 0.4157769 |
| 6 | 1 | 0.4194210 |
| 9 | 1 | 0.4140880 |
| 12 | 2 | 0.4779439 |
| 15 | 2 | 0.4751789 |
| 18 | 2 | 0.4762829 |
| 22 | 2 | 0.4706681 |
+-------------------+-----------+-----------+
Basically, each domains associated with the same id_number in both tables. While I am running the query below, getting average of each items.
select monitor_servers.label, avg(monitor_servers_uptime.latency)
from monitor_servers,monitor_servers_uptime
where monitor_servers.server_id = monitor_servers_uptime.server_id
group by monitor_servers.server_id;
The query ended up,
+---------------------+-------------------------------------+
| label | avg(monitor_servers_uptime.latency) |
+---------------------+-------------------------------------+
| a.com | 0.41393792995 |
| b.com | 0.47551423171 |
+---------------------+-------------------------------------+
My questions are doing am i in wright way while getting average of the each items and how can i insert new average result of each items into a new column on table monitor_servers ? And also what happens if some of latency rows are NULL ?
**Edit : What i am trying to achieve in one query result is **
+-----------+----------+------------------+
| server_id | label | avg. |
+-----------+----------+------------------+
| 1 | a.com | 0.41393792995 |
| 2 | b.com | 0.47551423171 |
+-----------+-----------------------------+
Thanks in advance,
Your calculation seems to be correct.
You could add another column to the monitor_servers using sql:
ALTER TABLE monitor_servers ADD avg_latency DEFAULT 0.0 NOT NULL
For doing the AVG calculation check this answer.
i have table called sla in mysql, i need to get unique switch and port_no related to perticular switch. i exactly don't know which join will help me.
+-------------------------+---------+
| switch | port_no |
+-------------------------+---------+
| 00:00:00:00:00:00:00:02 | 3 |
| 00:00:00:00:00:00:00:01 | 2 |
| 00:00:00:00:00:00:00:01 | 1 |
| 00:00:00:00:00:00:00:02 | 1 |
| 00:00:00:00:00:00:00:04 | 2 |
Expected output
+-------------------------+---------+
| switch | port_no |
+-------------------------+---------+
| 00:00:00:00:00:00:00:02 | 3,1 |
| 00:00:00:00:00:00:00:01 | 2,1 |
| 00:00:00:00:00:00:00:04 | 2 |
I got expected output by using GROUP_CONCAT function.
select switch,GROUP_CONCAT(port_no) from sla group by switch;
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