How to select the most spanning ranges - mysql

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.

Related

Remove duplicates SQL while ignoring key and selecting max of specified column

I have the following sample data:
| key_id | name | name_id | data_id |
+--------+-------+---------+---------+
| 1 | jim | 23 | 098 |
| 2 | joe | 24 | 098 |
| 3 | john | 25 | 098 |
| 4 | jack | 26 | 098 |
| 5 | jim | 23 | 091 |
| 6 | jim | 23 | 090 |
I have tried this query:
INSERT INTO temp_table
SELECT
DISTINCT #key_id,
name,
name_id,
#data_id FROM table1,
I am trying to dedupe a table by all fields in a row.
My desired output:
| key_id | name | name_id | data_id |
+--------+-------+---------+---------+
| 1 | jim | 23 | 098 |
| 2 | joe | 24 | 098 |
| 3 | john | 25 | 098 |
| 4 | jack | 26 | 098 |
What I'm actually getting:
| key_id | name | name_id | data_id |
+--------+-------+---------+----------+
| 1 | jim | 23 | NULL |
| 2 | joe | 24 | NULL |
| 3 | john | 25 | NULL |
| 4 | jack | 26 | NULL |
I am able to dedupe the table, but I am setting the 'data_Id' value to NULL by attempting to override the field with '#'
Is there anyway to select distinct on all fields and while keeping the value for 'data_id'? I will take the highest or MAX data_id # if possible.
If you only want one row returned for a specific value (in this case, name), one option you have is to group by that value. This seems like a good approach because you also said you wanted the largest data_id for each name, so I would suggest grouping and using the MAX() aggregate function like this:
SELECT name, name_id, MAX(data_id) AS data_id
FROM myTable
GROUP BY name, name_id;
The only thing you should be aware of is the possibility that a name occurs multiple times under different name_ids. If that is possible in your table, you could group by the name_id too, which is what I did.
Since you stated you're not interested in the key_id but only the name, I just excluded it from the query altogether to get this:
| name | name_id | data_id |
+-------+---------+---------+
| jim | 23 | 098 |
| joe | 24 | 098 |
| john | 25 | 098 |
| jack | 26 | 098 |
Here is the SQL Fiddle example.
RENAME TABLE myTable to Old_mytable,
myTable2 to myTable
INSERT INTO myTable
SELECT *
FROM Old_myTable
GROUP BY name, name_id;
This groups my tables by the values I want to dedupe while still keeping structure and ignoring the 'Data_id' column

Listing results with unique column

I want to list top 6 race records with unique holder only. I mean a holder gets in the list shouldn't be listed with his another record. I currently use the query below to list top 6 times.
mysql> select * from racerecords order by record_time asc, date asc;
+----+---------+------------+-------------+---------------------+----------+
| id | race_id | holder | record_time | date | position |
+----+---------+------------+-------------+---------------------+----------+
| 2 | 10 | Stav | 15 | 2014-08-11 19:43:49 | 1 |
| 1 | 10 | Jennifer | 15 | 2014-08-13 19:43:19 | 1 |
| 4 | 10 | Jennifer | 16 | 2014-08-02 19:44:27 | 1 |
| 5 | 10 | Osman | 17 | 2014-08-04 19:44:57 | 1 |
| 7 | 10 | Gokhan | 18 | 2014-08-15 19:45:37 | 1 |
| 3 | 10 | MotherLode | 25 | 2014-08-01 19:44:11 | 1 |
+----+---------+------------+-------------+---------------------+----------+
6 rows in set (0.00 sec)
As you can see the holder "Jennifer" is listed twice. I want mySQL to skip her after she got in the list. The result I want to be generated is:
+----+---------+------------+-------------+---------------------+----------+
| id | race_id | holder | record_time | date | position |
+----+---------+------------+-------------+---------------------+----------+
| 2 | 10 | Stav | 15 | 2014-08-11 19:43:49 | 1 |
| 1 | 10 | Jennifer | 15 | 2014-08-13 19:43:19 | 1 |
| 5 | 10 | Osman | 17 | 2014-08-04 19:44:57 | 1 |
| 7 | 10 | Gokhan | 18 | 2014-08-15 19:45:37 | 1 |
| 3 | 10 | MotherLode | 25 | 2014-08-01 19:44:11 | 1 |
+----+---------+------------+-------------+---------------------+----------+
I tried everything. GROUP BY holder generates wrong results. It gets the very first record of the holder, even though is not the best. In this table it generates an output like above because id:1 is the first record I inserted for Jennifer.
How can I generate output a result like above?
Desired result can be achieved through this query but it performance intensive. I have reproduced the result in SQLFilddle http://sqlfiddle.com/#!2/f8ee7/3
select * from racerecords
where
(HOLDER, RECORD_TIME) in (
select HOLDER,min(RECORD_TIME) from racerecords
group by HOLDER)
Seems you have missed to include the Where clause in the sub-query. Try this
select * from racerecords
where
(HOLDER, RECORD_TIME) in (
select HOLDER,min(RECORD_TIME) from racerecords where race_id =17
group by HOLDER )
And race_id =17
Order by RECORD_TIME
you should use distinct clause
SELECT DISTINCT column_name,column_name
FROM table_name;
looks this http://www.w3schools.com/sql/sql_distinct.asp

MySQL sum per IDs without subqueries

I'm working on a huge dataset, with a table that looks like this :
+----+---------+--------+--------+
| id | otherid | value1 | value2 |
+----+---------+--------+--------+
| 1 | 1 | 2 | 5 |
| 1 | 1 | 4 | 8 |
| 1 | 2 | 3 | 6 |
| 2 | 123 | 1 | 4 |
+----+---------+--------+--------+
I need to multiply value1 and value2 for each row, and sum values per id and otherid. A result table might be:
+----+---------+-----+
| id | otherid | sum |
+----+---------+-----+
| 1 | 1 | 42 | ((2*5)+(4*8))
| 1 | 2 | 18 | (3*6)
| 2 | 123 | 4 | (1*4)
+----+---------+-----+
My question is if it is possible to avoid subqueries to do this, I only found solutions that used them.
Thanks!
it's easy.
SELECT id,
otherid,
SUM(value1*value2) AS sum
FROM your_table
GROUP BY id, otherid;
Try Below Query
SELECT ID,otherid ,SUM(value1 * value2) sum
FROM TABLE1
GROUP BY ID,otherid

MySQL counting total number of zeros

I have a table that looks like
| id | day1 | day2 | day3 | day4 | day5 |
| 1 | 4 | 0 | 5 | 0 | 0 |
| 2 | 2 | 0 | 0 | 4 | 1 |
and I want to find to total number of zero entries for each id to give
| id | total_zeros |
| 1 | 3 |
| 2 | 2 |
SELECT id, (day1=0)+(day2=0)+(day3=0)+(day4=0)+(day5=0) total_zeroes
FROM table
Try this one:
select
id, if(day1=0,1,0)+if(day2=0,1,0)+ if(day3=0,1,0)+if(day4=0,1,0)+if(day5=0,1,0) as total
from test
DEMO HERE
Why do people insist on making such un-usable tables?
You will have to use a case statement, and evaluate each column individually, and then add up the results.

Get maximum value of a GROUP BY query without subquery in mySQL

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