while循环控制表的样式

select AwardID,t.TypeID,
DepartmentName,UserName,
UG_Name,T.AwardType,
T.AwardReason,convert(nvarchar(10),T.AwardTime,121) as 'AwardTime',T.Inactive
into #temp
from T_GLGZ_AwardInfo T
left join  View_UserInfo v on t.userid= v.userid
where inactive = 0

declare @PrimaryIDTable table
(
 AwardID int,
 typeid nvarchar(10),
 DepartmentName nvarchar(50),
 UserName nvarchar(50),
 UG_Name nvarchar(50),
 AwardType nvarchar(50),
 AwardReason nvarchar(100),
 AwardTime nvarchar(50),
 Inactive nvarchar(10)
)

--记录奖励类型
declare @typecount int
set @typecount = 0
while @typecount <=1
begin
 --统计总条数
 declare @count  int
 --记录总条数
 declare @recount  int
 set @count = (select count(1) from T_GLGZ_AwardInfo where TypeID =@typecount)
 set @recount = @count
  WHILE @count >=0
    BEGIN
     if @recount = @count
      begin
       insert into @PrimaryIDTable
       select top 1* from #temp where typeid = @typecount
      end
     else if @recount != 0 and  @count = 0
      begin
       insert into @PrimaryIDTable
       select '','合计',count(1),'','','','','','' from #temp where typeid = @typecount
      end
     else
      begin
       insert into @PrimaryIDTable
       select AwardID,'',DepartmentName,UserName,UG_Name,AwardType,AwardReason,AwardTime,Inactive from #temp
       where AwardID not in(select AwardID from @PrimaryIDTable) and typeid = @typecount
      end
      SET @count = @count - 1 
    END
   SET @typecount = @typecount + 1
end
select AwardID,case typeid when '0' then '奖励' when  '1' then '惩罚' when '合计' then '合计' end as 'TypeID',
DepartmentName,UserName,UG_Name,AwardType,AwardReason,AwardTime,Inactive
from @PrimaryIDTable

drop table #temp

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值