mysql> SELECT
units,
MAX(ind1)
FROM
`tbl_max`
GROUP BY
units
ORDER BY
MAX(ind1) DESC
LIMIT 1;
SELECT
units,
MAX(ind2)
FROM
`tbl_max`
GROUP BY
units
ORDER BY
MAX(ind2) DESC
LIMIT 1;
+-------+-----------+
| units | MAX(ind1) |
+-------+-----------+
| BBB | 88% |
+-------+-----------+
1 row in set
+-------+-----------+
| units | MAX(ind2) |
+-------+-----------+
| AAA | 59% |
+-------+-----------+
1 row in set
this is a great example of what happens when your table design violates first normal form – the result is, awkward, clumsy, and inefficient queries
SELECT units
, MAX(ind) AS maxind
FROM ( SELECT units
, ind1 AS ind
FROM tbl_max
UNION
SELECT units
, ind2
FROM tbl_max ) AS discombobulated
GROUP
BY units;
hi Rudy, thank you for help but the output is always incorrect…
mysql> SELECT units
, MAX(ind) AS maxind
FROM ( SELECT units
, ind1 AS ind
FROM tbl_max
UNION
SELECT units
, ind2
FROM tbl_max ) AS discombobulated
GROUP
BY units;
+-------+--------+
| units | maxind |
+-------+--------+
| AAA | 84% |
| BBB | 88% |
+-------+--------+
2 rows in set
also, please explain the correct output, not just with “expected output” that we have to figure out (because obviously, i figured it out wrong), but using words
for example, in post #1, you want the “max” output, but AAA has two values, 84 and 59, and yet somehow you want 59, not 84 – why?
I need extract the max values of the ind1 and the ind2… for the ind1 the max values is 88% for ind2 the max value is 59%
I’ve got several problems here because the data may not be normalised …
okay thank you … I don’t go beyond this query … the value of units in ind 59% is incorrect… AAA instead of BBB …
mysql> SELECT
units,
ind
FROM
(
SELECT
units,
MAX(ind1) AS ind
FROM
tbl_max
UNION
SELECT
units,
MAX(ind2) AS ind
FROM
tbl_max
) AS p;
+-------+-----+
| units | ind |
+-------+-----+
| AAA | 88% |
| AAA | 59% |
+-------+-----+
2 rows in set
mysql> SELECT
t.units,
t.ind1
FROM
(
SELECT
MAX(ind1) AS max_ind1
FROM
tbl_max
) AS m
INNER JOIN tbl_max AS t ON t.ind1 = m.max_ind1;
SELECT
t.units,
t.ind2
FROM
(
SELECT
MAX(ind2) AS max_ind2
FROM
tbl_max
) AS m
INNER JOIN tbl_max AS t ON t.ind2 = m.max_ind2;
+-------+------+
| units | ind1 |
+-------+------+
| BBB | 88% |
+-------+------+
1 row in set
+-------+------+
| units | ind2 |
+-------+------+
| AAA | 59% |
+-------+------+
1 row in set
mysql> SELECT
t.units,
t.ind1
FROM
(
SELECT
MAX(ind1) AS max_ind1
FROM
tbl_max
) AS m
INNER JOIN tbl_max AS t ON t.ind1 = m.max_ind1
UNION ALL
SELECT
t.units,
t.ind2
FROM
(
SELECT
MAX(ind2) AS max_ind2
FROM
tbl_max
) AS m
INNER JOIN tbl_max AS t ON t.ind2 = m.max_ind2;
+-------+------+
| units | ind1 |
+-------+------+
| BBB | 88% |
| AAA | 59% |
+-------+------+
2 rows in set