getting maximum value of a varchar column in database - mysql

how can i get the maximum value of my column that is a varchar with these values
for example i have a fieldname of myid which is varchar and what i want is to get the maximum value of the myid field . How can i query to get the 1-10 value of myid column?
myid
1-1
1-2
1-3
1-4
1-5
1-6
1-7
1-8
1-10
1-9

I would suggest using this trick:
order by length(myid) desc, myid desc
This will work for the data in the question. A more general answer is:
order by substring_index(myid, '-', 1) + 0, substring_index(myid, '-', -1) + 0

For the data you've shown:
select myid
from data_table
order by cast(substr(myid, 3, 2) as int) desc
limit 1;
In this case the ordering function is the integer value of the portion of the identifier following the dash. In general--i.e., for different or more complex data--you simply need to determine what the appropriate odering function is.

Related

SQL SELECT ORDER BY multiple columns depending on value of other column

I have a table with the following columns:
id | revisit (bool) | FL (decimal) | FR (decimal) | RL (decimal) | RR (decimal) | date
I need to write a SELECT statement that will ORDER BY on multiple columns, depending on the value of the 'revisit' field.
ORDER BY 'revisit' DESC - records with this field having the value 1 will be first, and 0 will be after
If 'revisit' = 1 order by the lowest value that exists in FL, FR, RL and RR. So if record 1 has values 4.6, 4.6, 3.0, 5.0 in these fields, and record 2 has values 4.0, 3.1, 3.9, and 2.8 then record 2 will be returned first as it holds a lowest value within these four columns.
If 'revisit' = 0 then order by date - oldest date will be first.
So far I have the 'revisit' alone ordering correctly, and ordering by date if 'revisit' = 0, but ordering by the four columns simultaneously when 'revisit' = 1 does not.
SELECT *
FROM vehicle
ORDER BY
`revisit` DESC,
CASE WHEN `revisit` = 1 THEN `FL` + `FR` + `RR` + `RL` END ASC,
CASE WHEN `revisit` = 0 THEN `date` END ASC
Instead it seems to be ordering by the total of the four columns (which would make sense given addition symbols), so how do I ORDER BY these columns simultaneously, as individual columns, rather than a sum.
I hope this makes sense and thanks!
In your current query, you order by the sum of the four columns. You can use least to get the lowest value, so your order by clause could look like:
SELECT *
FROM vehicle
ORDER BY
`revisit` DESC,
CASE WHEN `revisit` = 1 THEN LEAST(`FL`, `FR`, `RR`, `RL`) END ASC,
CASE WHEN `revisit` = 0 THEN `date` END ASC
Of course this would sort only by the lowest value. If two rows would both share the same lowest value, there is no sorting on the second-lowest value. To do that is quite a bit harder, and I didn't really get from your question whether you need that.

I want to get the maximum value from my S_ID column which is declared as varchar type

My S_ID values are
S_1
S_2
S_3,...., S_11.
I use SELECT MAX(S_ID) FROM stock_detail to get the maximum value.
It is working till S_9 but when it reaches S_11: that query only give me S_9 as the maximum value. How can I do to get S_11 as the maximum value ? Please help me, I am just a beginner in programming.
You can get the maximum value by using this construct:
select s_id
from stock_detail
order by length(s_id) desc, s_id desc
limit 1;
This puts the longer values first.
If you want to use max(), then you need to deconstruct the number. Something like:
select concat('S_', max(replace(s_id, 'S_', '') + 0))
from stock_detail;
This allows you to get a numeric maximum value rather than a character maximum value, which is the root of your problem.

mysql select in aescending or descending order

my id or primary key is and the data type of is VARCHAR(50)
0.0.01
0.0.100
0.0.101
0.0.1011
0.0.201
0.0.501
0.0.99
0.0.999
0.01.0
0.01.10
0.02.10
0.02.20
0.02.99
01.0.0
01.0.99
01.02.99
01.03.444
01.05.88
10.02.99
100.100.100
25.45.1001
99.99.99
I have to get it in sorted order
so i tried this
select id from table order by cast(id as decimal) desc;
but it does not work
the expected order is after running the query
0.0.01
0.0.99
0.0.100
0.0.101
0.0.201
0.0.501
0.0.999
0.0.1011
0.01.0
0.01.10
0.02.10
0.02.20
0.02.99
01.0.0
01.0.99
01.02.99
01.03.444
01.05.88
10.02.99
25.45.1001
99.99.99
100.100.100
i am using mysql for this
Not an easier one but you can use substring_index for each decimal places
select *
from t
order by
substring_index(id,'.',1) * 1,
substring_index(substring_index(id,'.',-2),'.',1) * 1,
substring_index(id,'.',-1) * 1
Explanation
I have use substring_index what it does it will return the piece of string in provided column like in above case i have use id column by the occurrence of delimiter i.e(.) for example a string like 0.1.2 for above 3 sunstring_index usage will return as below
substring_index('0.1.2','.',1) will give result as 0
substring_index(substring_index('0.1.2','.',-2),'.',1) will give result as 1
substring_index('0.1.2','.',-1) will give result as 2
For type casting to number i have multiplied the result of substring_index to 1 so the order by expression will first order the results by the number before first dot then with number before second dot and last the number after second dot in ascending manner
Demo
Sources:
http://www.w3resource.com/mysql/string-functions/mysql-substring_index-function.php
Your ID column has invalid decimal value so casting could not work here.
Try without casting this should work:
select id from table order by id desc;
DEMO

how can I tell if the last x rows of 'state' = 1

I need help with a SQL query.
I have a table with a 'state' column. 0 means closed and 1 means opened.
Different users want to be notified after there have been x consecutive 1 events.
With an SQL query, how can I tell if the last x rows of 'state' = 1?
If, for example, you want to check if the last 5 consecutive rows have a state equals to 1, then here's you could probably do it :
SELECT IF(SUM(x.state) = 5, 1, 0) AS is_consecutive
FROM (
SELECT state
FROM table
WHERE Processor = 3
ORDER BY Status_datetime DESC
LIMIT 5
) as x
If is_consecutive = 1, then, yes, there is 5 last consecutive rows with state = 1.
Edit : As suggested in the comments, you'll have to use ORDER BY in your query, to get the last nth rows.
And for more accuracy, since you have a timestamp column, you should use Status_datetime to order the rows.
You should be able to use something like this (replace the number in the HAVING with the value of x you want to check for):
SELECT Processor, OpenCount FROM
(
SELECT TOP 10 Processor, DateTime, Sum(Status) AS OpenCount
FROM YourTable
WHERE Processor = 3
ORDER BY DateTime DESC
) HAVING OpenCount >= 10

how to sort varchar numeric columns by DESC or ASC?

I write ...
ORDER BY column ASC
but my column is VARCHAR and it sorts wrong like 1, 10, 2, instead of 1, 2, 10.
How can I do it to sort like 1, 2, 10?
order by
cast(column as float)
Notes:
Assumed you only have numbers in the columns. No "fish" or "bicycle"
empty strings CAST to zero
Edit: For MySQL. You can not cast to float
order by
cast(column as decimal(38,10))
You can cast to int...
order by cast(column as int)
DEMO
DECLARE #q as table(
name varchar(50),
columnn varchar(10)
)
insert into #q
VALUES('one','1'),('one','10'),('one','20'),('one','3'),('one','2'),('one','20')
select * from #q order by cast (columnn as int) desc
prints
-------------------------------------------------- ----------
one 20
one 20
one 10
one 3
one 2
one 1
So, Daniel, yes, it works :)
UPDATE:
order by cast(column as decimal(20,6))
Will cast the column values to decimal numbers with 20 digits max and 6 decimal places. Adjust it to your actual requirements.
Try this:
order by CAST(column as UNSIGNED)
i used this way
multiply it with one the query is :
ORDER BY columnname * 1 ASC
Example: Table user have value with column value [varchar(20)].
Then you can query it:
SELECT * FROM user ORDER BY value * 1
After we multiply it MySQL will treat it like a number but this way is not recommended for a heavy load.