database - Unable to retrieve a specific value in MySQL while using a normal SELECT with nested SELECT using COUNT with CASE -
i have following query:
select greatest(depressed, non_depressed), stat ( select state stat, count(case when state = "dep" 1 else null end) depressed, count(case when state = "sane" 1 else null end) non_depressed phonecallfeatures ) maximum
i trying retrieve name of state can know of 2 states recurrent.
my output following:
i think better way of phrasing query:
select state, count(*) state_count phonecallfeatures state in ('dep', 'sane') group state order count(*) desc limit 1;
just use group by
, let mysql worry tallying records, @ doing.
if want continue current approach, problem selecting stat
in inner query. doesn't make sense, because sum()
aggregate function , not clear which stat
value chosen. in query below, use case
expression compare counts, , present appropriate label based on that.
select case when depressed > non_depressed 'depressed' else 'non depressed' end stat, greatest(depressed, non_depressed) stat_count ( select state stat, sum(case when state = "dep" 1 else 0 end) depressed, sum(case when state = "sane" 1 else 0 end) non_depressed phonecallfeatures ) maximum
note replaced count()
sum()
. there nothing wrong count()
way have used it, since ignore null
values, sum()
seems more logical me here.
demo here:
Comments
Post a Comment