文章分类 - SQL
摘要:存储过程中: exec pro 这种语法执行存储过程 exec(@xxx) 这种执行一条普通sql语句 如果要返回值,需要声明 @tpl varchar(100) OUTPUT 这样一个入参 比如执行查询 set @sql = N'SELECT * from '+@tablename exec(@s
阅读全文
摘要:select sum(f1) fa from ( select sum(f2) fb from t1 union all select sum(f3) fc from t2 ) 两个表t1和t2得 f2 , f3,同一个意义,可以一起sum,通过 union all 合并两个表得数据
阅读全文
摘要:获取今日的日期的函数是curdate DATE_SUB函数的作用是返回从一个日期减去指定的时间间隔的结果,例如昨天select date_sub(curdate(), interval 1 day) from dual; Mysql中常用的几种时间类型有:date、datetime、time、yea
阅读全文
摘要:select t1.c1, t2.c2, cast(cast(t2.c2 - t1.c1 as decimal(10, 2)) / cast(t2.c2 as int) as decimal(10, 2)) from (select count(*) realCount from t1) t1, (
阅读全文
摘要:第一种常规方法,利用子查询和集合计算:select lineCode, SUM(CASE tmp.alarmSubType WHEN '1' THEN tmp.count ELSE 0 END) '1', SUM(CASE tmp.alarmSubType WHEN '2' THEN tmp.cou
阅读全文
摘要:dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值 例如:向日期加上2天 select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000 如果加0天,就是当前字符串要转成的日期格式(yyyymmdd) se
阅读全文
摘要:SET IDENTITY_INSERT xxxx ON
阅读全文
摘要:使用子查询,多层group by思路来解决 select tmp.Line_Code, count(*)from (select Bus_Code, Line_Code from Suqian_data.dbo.DD_Real_Time_Detail group by Line_Code, Bus_
阅读全文
摘要:GetDate select DateName(year,GetDate()) as '年'; select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) as '月',DateName(d
阅读全文
摘要:0)列转行。注意:列多少不限,名称未知。1)有什么 2)要什么3)参考代码--列转行。新写法。--注意:比unpivot更灵活,支持字段名实现未知的情况。with TestData as ( select 1 as id, 1 as f1, 2 as f2, 3 as f3, 4 as f4 uni
阅读全文
摘要:begin if (object_id('tempdb..#Test') is not null) drop table #Test ; create table #Test ([id] [int] identity (1, 1) not null, [name] [nvarchar](50) co
阅读全文