sql server - how to sort according to special case in sql -
i have case have stored procedure persons couples male , female want sort couple according male name try sing
select p.gendertypeid, couple.coupleid, p.personid , p.namear, p.nameen , p.mobile tbl_couples couple left outer join tbl_persons p on p.coupleid = couple.coupleid order case when p.gendertypeid = 1 '1'--male when p.gendertypeid = 2 '2'--female else p.gendertypeid end asc , p.namear
put male first female
i that
gendertypeid coupleid personid namear nameen mobile 1 3 10 أحمد ali 01255678668 1 5 15 سمير samier 01255353563 1 4 17 سيد said 012359989744 2 5 14 سامية samia 01156786868 2 4 16 سعاد suadd 01353563563 2 3 12 منى mona 010264646444
put expect
gendertypeid coupleid personid namear nameen mobile 1 3 10 أحمد ali 01255678668 2 3 12 منى mona 010264646444 1 5 15 سمير samier 01255353563 2 5 14 سامية samia 01156786868 1 4 17 سيد said 012359989744 2 4 16 سعاد suadd 01353563563
use cte first select males, join tbl_couples , tbl_persons tables, give result set in male part of couple duplicated "males" cte.
with tbl_maleonly ( select * tbl_persons gendertypeid = 1 ) select p.gendertypeid, couple.coupleid, p.personid , p.namear, p.nameen , p.mobile tbl_maleonly male left outer join tbl_couples couple on couple.coupleid = male.coupleid left outer join tbl_persons p on p.coupleid = couple.coupleid order male.namear , p.gendertypeid
instead of cte, use inline view: replace "tbl_maleonly" in main select cte select in parenthesis.
Comments
Post a Comment