i have a database table with unique column containing value like
invid
----------
500
1000
B2222
A9998
A9999
A10000
the problem whenever my query max(invid) is return A9999 what is the solution
try this
SELECT CONCAT('A', MAX(0+SUBSTRING(invid,2))) FROM your_table
The problem is you're trying to find the max value of the alphanumeric, in this case A9999 is greater than A10000, it's not the same with 9999 and 10000.
The best solution is to correct the problem permanently by changing the column to an auto_increment and remove the A letter.
Otherwise you could use following query:
SELECT CONCAT('A'
, MAX(0+SUBSTRING(invid,2)))
FROM table
You can SELECT the maximum of the numerical value in the invid column:
SELECT t.invid, MAX(t.value) AS maxValue
FROM
(
SELECT invid,
CASE WHEN invid LIKE '[A-Z]%'
THEN CAST(SUBSTRING(invid, 2) AS UNSIGNED)
ELSE CAST(invid AS UNSIGNED)
END AS value
FROM yourTable
) t
This assumes that each entry in invid will have at most one letter prefixing the number. This does not take into account the possibility of ties for maximum value.
Related
I have a table with field min, max and amount.
The last value of the column max is always null and it denotes min value and above as per my logic.
During BETWEEN query in sql I tried as:
Select t.amount from my_table t where 25000 between t.min and NVL(t.max, ~0);
Also, I tried:
Select t.amount from my_table t where 25000 between t.min and COALESCE(t.max, ~0);
None of them return me a row if the value lies between min and NULL.
The sample Data is :
How about:
25000 between t.min and nvl(t.max, 25000)
If t.max is null, the nvl() function returns 25000, which will satisfy the higher bound of the between operator.
If the value to compare is stored in a column, say col1, then you can do:
col1 between t.min and nvl(t.max, col1)
I am not certain that I completely glean what you are trying to do here, but I sense that you want logic for dealing with unknown max values in your table, and these unknown values are represented by NULL. Assuming you want to query under the assumption that an unknown max value means just don't restrict by max, then use:
SELECT amount
FROM my_table
WHERE min <= 25000 AND (max >= 25000 OR max IS NULL);
If max happens to be NULL/unknown, then only a minimum restriction would be applied. See the demo below.
Demo
I have a table "abcd" with column name as "avg" and values "100" and "83".
When I try
select max(avg) from abcd -- Returns 83
select min(avg) from abcd -- Returns 100
seems quite weird to me. I have never imagined that I will be posting something like this in SO. It might be a minor thing to look but it's kicking my day out to solve it.
Am using MySQL and phpMyAdmin
Sounds like a string. A simple solution is to turn it to a number:
select max(avg + 0)
This uses "silent conversion", so it will not raise an error if the value is not numeric.
A better solution might be to turn it into an actual number in the data:
alter table t modify column avg int;
(The values appear to be integers.)
change the datatype of your column avg
if you using varchar its give wrong output on number function
using below query alter the column
ALTER TABLE `abcd` CHANGE `avg` `avg` INT(11) NOT NULL;
OR try this
SELECT max( cast(avg as unsigned) ) as avg FROM `abcd`
SELECT min( cast(avg as unsigned) ) as avg FROM `abcd`
In a give table I want to select the row with the maximum value in column-A, but in the same time if there are 2 or more rows that have a maximum value a want to get the row where the value in column-B is not null, if there is one, otherwise I just get the first row with the maximum value even if the value in Column B is null.
In summary:
first choice : Column-A maximum, Column-B Not null
in case such a row does not exist
second choice : Column-A maxim, Column-B Null
Is it possible to write a single query with this constrain or do I have to create 2 queries with some logic in between?
You can use multiple columns with ORDER BY like below
SELECT columnA,columnB
FROM YourTable
ORDER BY columnA DESC, columnB DESC
It'll order by columnA first descending order, then columnB in descending order the null will be the last choice.
add a LIMIT 1 at end of query if you just one row returned.
Below Query will do the trick
SELECT * FROM tableA where columnA=(select max(columnA) from tableA) order by columnB desc
You could use an inline view to return the maximum value of col_a, and then do a join to get all the rows that have the same col_a value, then get the maximum value of col_b from those rows.
For example:
SELECT s.col_a
, MAX(t.col_b) AS col_b
FROM ( SELECT MAX(r.col_a) AS col_a
FROM mytable r
) s
JOIN mytable t
ON t.col_a = s.col_a
I need to perform an avg on a column, but I know that most of the values in that column will be zero. Out of all possible rows, only two will probably have positive values. How can I tell mySQL to ignore the zeros and only average the actual values?
Assuming that you might want to not totally exclude such rows (perhaps they have values in other columns you want to aggregate)
SELECT AVG(NULLIF(field ,0))
from table
You could probably control that via the WHERE clause:
select avg( field ) from table where field > 0
select avg(your_column)
from your_table
where your_column != 0
You can convert zeros to NULL, then AVG() function will work only with not NULL values.
UPDATE table SET column = NULL WHERE column='0';
SELECT AVG(column) FROM table;
I have a query with many wheres and orders criterias. One of the fields of the select is 'price' (element price) but I would like to have also (in every row) the maximun price of all the selected elements.
I tried to include MAX aggregate function on select hoping that this will return desired value, but insetead of that, price and MAX(price) returns the same. Searching into MySql doc I found the reason:
If you use a group function in a
statement containing no GROUP BY
clause, it is equivalent to grouping
on all rows.
Is there a way to solve this problem?
Thanks in advance!
There's a similar question (but not resolving this): find max value without aggregate operator in mysql
You can do this:
SELECT
id,
price,
(SELECT MAX(price) FROM your_table) AS max_price
FROM your_table
I'm not sure why you'd want to return that value on every row though... I'd probably do this in two separate queries, or else use a UNION ALL:
SELECT id, price FROM your_table
UNION ALL
SELECT NULL, MAX(price) FROM your_table
Maybe you could use a stored procedure like so:
CREATE PROCEDURE QueryWithMax ([parameter list if necessary])
BEGIN
-- Obtain the maximum price
DECLARE x INT UNSIGNED; -- change datatype if appropriate
SET x = SELECT
MAX(price)
FROM
...
WHERE
...
;
-- Now do your query
SELECT
price,
[other columns],
x AS MaxPrice
FROM
...
WHERE
...
GROUP BY
...
;
END
I haven't tried this, but if you had a subquery that extracts the maximum price (so you get one row), and then do a cross join (cartesian product) with it. You should get something like what you want. I can't vouch for how fast this would be.