mysql - Different select statement for different condition -
this current mysql query.
current table query in sqlfiddle http://sqlfiddle.com/#!2/6e0420
if select * table
, below
rn pc pc1 grp e_id 111 a1 a1 175 100 112 a1 a2 100 90 113 b1 b3 101 90 114 b1 b1 100 90 115 b1 b5 100 90 116 c1 c1 100 90
but trying output
rn pc pc1 grp e_id 111 a1 a1 175 100 112 a1 a2 113 b1 b3 114 b1 b1 100 90 115 b1 b5 116 c1 c1 100 90
so condition if pc=pc1 grp , e_id should shown, otherwise if pc!=pc1 grp , e_id should empty
current query select * table1 rn in(select rn table1 group rn having count(*)=1) , (pc = pc1) solution select rn, pc, pc1, case when pc = pc1 grp else null end grp, case when pc = pc1 e_id else null end e_id table1 rn in(select rn table1 group rn having count(*)=1)
qn2: above solution have added count = 1 because have sql query if count > 1. how combine both query, split count =1 , count >1 below sql query
select * table1 rn in(select rn table1 group rn having count(*)>1) , (pc = pc1) , grp in (select max(grp) table1) , e_id in( select min(e_id) table1)
the conditions count>1 shld not affect count = 1 results.
i found solution already, union them. first part.
you can use case when
, , display null (or else) when pc <> pc1
select rn, pc, pc1, case when pc = pc1 grp else null end grp, case when pc = pc1 e_id else null end e_id table1
which can simplified (for null replacement)
select rn, pc, pc1, case when pc = pc1 grp end grp, case when pc = pc1 e_id end e_id table1
Comments
Post a Comment