工作中常用SQL语句汇总(一)

转载原创博客为:https://blog.csdn.net/XieTTian/article/details/80639009?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-1.nonecase

销售统计(一列今日量,一列累计量)

饮品/门店     门店1    门店2    累计销量
可乐    10    10    20
雪碧    20    30    80
Select ProductCode ,ShopNo,sum(case when MakeTime like '%2017-12-12%' then TotalSalesOrderAmount end) as '本日',
sum(case when MakeTime < '2017-12-12 00:00:00' then TotalSalesOrderAmount end) as '累计' from DMS_TotalSalesOrder
group by ProductCode ,ShopNo
修改时间字段的部分内容

--修改d表日期字段的年份
update [test].[dbo].[L_AlarmLog]
set Operatetime=STUFF(convert(nvarchar(23),Operatetime,120),1,4,'2012')
 
--修改d表日期字段的月份
update [test].[dbo].[L_AlarmLog]
set Operatetime=STUFF(convert(nvarchar(23),Operatetime,120),6,2,'3')
 
--修改d表日期字段的天数
update [test].[dbo].[L_AlarmLog]
set Operatetime=STUFF(convert(nvarchar(23),Operatetime,120),9,2,'25')
 
--修改d表日期字段的小时数
update [test].[dbo].[L_AlarmLog]
set Operatetime =STUFF(convert(nvarchar(23),Operatetime,120),12,2,'9')
 
--修改d表日期字段的分钟数
update [test].[dbo].[L_AlarmLog]
set Operatetime =STUFF(convert(nvarchar(23),Operatetime,120),15,2,'15')
 
--修改d表日期字段的秒数
update [test].[dbo].[L_AlarmLog]
set Operatetime =STUFF(convert(nvarchar(23),Operatetime,120),18,2,'30')
 
--修改d表日期字段的年份、小时
update [test].[dbo].[L_AlarmLog]
set Operatetime=Convert(datetime,stuff(STUFF(Convert(char,Operatetime,120), 1,4, '2011'),12,2, '02'))
 
--修改表日期字段的年份

update [test].[dbo].[L_AlarmLog] set Operatetime=dateadd(year,datediff(year,Operatetime,'2016-01-01'),Operatetime) 
where year(Operatetime)<>2012
 
--修改表日期字段的小时
update [test].[dbo].[L_AlarmLog] set Operatetime=dateadd(hour,3,Operatetime)

--DATEADD函数
update [test].[dbo].[L_AlarmLog] set OperateTime=dateadd(date,datediff(dd,'2017.11.20',getdate()),OperateTime) where OperateTime like '2017.11.10%'

--更新某一个值在某一个范围内的随机数
update TimingSaveVolume set OilQty = cast(rand(checksum(newid()))*(100005-100000)+100000 as float(6))
 

你可能感兴趣的:(数据库,sql)