I'm trying to set up a matching system that selects the highest values of two different queries. I've looked at other answers on the site and they don't help. What I basically want to compare is:
I want to grab the highest value from the version
column where type
is equal to, for example " type1
". I also want to grab the highest value from the version
column where type
is equal to " type2
". I then want to compare these with PHP and find out if type2
's version
is higher than type1
's version
. Is there a way I can do this?
EDIT: This question has been answered by Brian Demilia. Can a moderator mark it as answered?
I believe this will do what you want:
select version, type
from tbl
where version =
(select max(version) from tbl where type in ('type1', 'type2'))
The type result column will show 'type1' if the max(version) was associated with a type1 record, or 'type2' if the max(version) was associated with a type2 record.
If you want to know the highest version for type1 and the highest version for type2 (as in, you want the values brought back in output), and have the query identify which one is higher, you can use the following:
select t1.version as type1_max_version,
t2.version as type2_max_version,
case when t1.version > t2.version
then t1.version
else t2.version end as highest_version_num,
case when t1.version > t2.version
then 'type1'
else 'type2' end as highest_version_type
from tbl t1
cross join tbl t2
where t1.version = (select max(version) from tbl where type = 'type1')
and t2.version = (select max(version) from tbl where type = 'type2')
Based on your comment if you wanted to filter in on only those rows where a field called "usrips" had certain values, you could use the following. This also assumes that you want to apply this filter to BOTH the type1 and type2 rows. Let me know if that is not the case. Also if you want to do additional fields in the WHERE clause, you'll need to add them to the JOIN clause between t1 and t2 as well, otherwise you'd have to repeat everything in the WHERE clause twice, once for t1 and once for t2
select t1.version as type1_max_version,
t2.version as type2_max_version,
case when t1.version > t2.version
then t1.version
else t2.version end as highest_version_num,
case when t1.version > t2.version
then 'Firewall'
else 'FBypass' end as highest_version_type
from tbl t1
join tbl t2 on t1.usrips = t2.usrips
where t1.version = (select max(version) from tbl
where type = 'Firewall' and usrips LIKE '%1.1.1.1%')
and t2.version = (select max(version) from tbl
where type = 'FBypass' and usrips LIKE '%1.1.1.1%')
and t1.usrips LIKE '%1.1.1.1%'
See fiddle: http://sqlfiddle.com/#!2/2525a/7/0
Is this what you want?
select max(case when t.type = 'type1' then version end) as type1_maxversion
max(case when t.type = 'type2' then version end) as type2_maxversion
from table t;
You can do the comparison in SQL, the easiest way is with a subquery:
select (case when type1_maxversion is null then 'No Type1'
when type2_maxversion is null then 'No Type2'
when type1_maxversion > type2_maxversion then 'Type1 Bigger'
when type2_maxversion > type1_maxversion then 'Type2 Bigger'
else 'equal'
end) as comp
from (select max(case when t.type = 'type1' then version end) as type1_maxversion
max(case when t.type = 'type2' then version end) as type2_maxversion
from table t
) x;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.