sql union 语句 case语句

1:Union语句: 把两个结果合为一体(但是完全重复的数据会去掉)

Eg1: select name, age,学生无工资 from student

       union  

       select name, age, sal from teacher

Eg2: select top 2  CONVERT(varchar(20),itemId) itemid, fee, addDate  from Orders

         union

         select '合计',SUM(fee),GETDATE() from (select top 2 fee  from Orders) a

 

2:Union All (重复的数据不会去掉)

     工资合计效果:Select  name, sal  from  teacher  

                          union all  

                          select  ‘工资合计’,  sum(sal)  from  teacher

 

3:case语句

    select top 10 

    (case rankId  when 1 then '管理员' when 2 then '编辑' else '客服' end) as '类型',

    adminName,addDate from admin

 

    select name,

    (case  when sal<1000 then ‘低收入’   when sal>=1000  and  sal<2000  then  ‘中等收入’  else  ‘高收入’  end )  as ‘客服类型’ 

    from customer

posted on 2017-05-17 11:59  改变从今天开始  阅读(575)  评论(0编辑  收藏  举报

导航