group_concat in mysql with "case when " conditions -
i wrote sql code in mysql environment concat data . but, couldn't correct result , , confusing wrong sql code. sql code follows:
select case when cc.complex_check_id = cmt.comp_o_id cc.status cstatus,sgk.status sgstatus,cc.name complex_check_name,cc.min min_flag,cmt.comp_t_name cmpt_name,group_concat(concat(concat(concat(concat(concat(f.name, ';') , sgk.name),' ') ,cc.operator),' ')) else cc.status cstatus,sgk.status sgstatus,cc.name complex_check_name,cc.min min_flag,'not' cmpt_name,group_concat(concat(concat(concat(concat(concat(f.name, ';') , sgk.name),' ') ,cc.operator),' ')) end res_string complex_check_anag cc,lnksinglechecktocomplexcheck lk,single_check_anag sgk,functionalci f ,lnkconfigurationitemtosinglecheck lkcg,comp_t_anag cmt cc.complex_check_id = lk.complex_check_id , sgk.single_check_id = lk.single_check_id , f.id = lkcg.config_item_id , sgk.single_check_id = lkcg.single_check_id , sgk.status = 'active' group cc.name
could give me suggestions ,please ?...thanks lot of !
the syntax used case
expression not correct, can select 1 expression inside case expression, selected more 1 column, , noticed 1 columns need select based on case condition, moved columns out of case expression except column this:
select cc.status cstatus, sgk.status sgstatus, cc.name complex_check_name, cc.min min_flag, group_concat(concat(concat(concat(concat(concat(f.name, ';') , sgk.name),' ') ,cc.operator),' ')), case when cc.complex_check_id = cmt.comp_o_id cmt.comp_t_name else 'not' end res_string complex_check_anag cc .... .... rest of query here
also, can rewrite query using inner join
instead of old join
syntax this:
select cc.status cstatus, sgk.status sgstatus, cc.name complex_check_name, cc.min min_flag, group_concat(concat(concat(concat(concat(concat(f.name, ';') , sgk.name),' ') ,cc.operator),' ')), case when cc.complex_check_id = cmt.comp_o_id cmt.comp_t_name else 'not' end res_string complex_check_anag cc, comp_t_anag cmt inner join lnksinglechecktocomplexcheck lk on cc.complex_check_id = lk.complex_check_id inner join functionalci f on f.id = lkcg.config_item_id inner join lnkconfigurationitemtosinglecheck lkcg on sgk.single_check_id = lk.single_check_id inner join single_check_anag sgk on sgk.single_check_id = lkcg.single_check_id sgk.status = 'active' group cc.name
note that, didn't specified condition between 2 tables complex_check_anag cc, comp_t_anag cmt
, cartesian product between 2 tables , might not give correct data. check relation between these 2 tables , add proper join type between them correct data looking for.
Comments
Post a Comment