1、在查询结果中添加一行数据
select * from TPRG_DemandFrom_ZDunion allselect '0','全部',1,'全部'
2、left join 与查看空值转换;
ISNULL 使用指定的替换值替换 NULL。
语法:ISNULL ( check_expression , replacement_value )
select A.id,A.ProgressName, ISNULL(B.ProgressCount,0)from TPRG_DemandProgress_ZD Aleft join(select count(*) ProgressCount,Progressfrom demand group by Progress) Bon A.id = B.Progress
3、计算百分比
select CONVERT(decimal(18, 1),除数 1.0 / 被除数) 100 a from 表名
decimal(18, 1) 代表取一位小数
declare @allcout char(50)set @allcout = (select count(*) from demand where state=1)select @allcoutselect count(*) fromBfb,B.fromName from demand A,TPRG_DemandFrom_ZD B where A.demandFrom= B.id and A.state =1 group by fromNameselect CONVERT(decimal(18, 1),count(*) * 1.0 /@allcout) * 100 fromBfb,B.fromName from demand A,TPRG_DemandFrom_ZD B where A.demandFrom= B.id and A.state =1 group by fromName
4、数据趋势统计
select COUNT(*),A.times from (select CONVERT(varchar(100),createTime, 23) times from demand ) Agroup by A.times
5、数据表结构备份与数据拷贝
--表结构数据备份select * into StandingBooks_backup_20220609from [dbo].[StandingBooks]--将一张表的数据插入另一张表insert into [dbo].[StandingBooks]select *,'' from StandingBooks_backup_20220609
