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:

enter image description here

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.

demo here

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:

rextester


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -