文章分类 -  SQL

摘要:存储过程中: exec pro 这种语法执行存储过程 exec(@xxx) 这种执行一条普通sql语句 如果要返回值,需要声明 @tpl varchar(100) OUTPUT 这样一个入参 比如执行查询 set @sql = N'SELECT * from '+@tablename exec(@s 阅读全文
posted @ 2022-06-03 09:55 叨叨的蜗牛 阅读(1128) 评论(0) 推荐(0) 编辑
摘要: 阅读全文
posted @ 2022-03-05 10:36 叨叨的蜗牛 阅读(4) 评论(0) 推荐(0) 编辑
摘要: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 合并两个表得数据 阅读全文
posted @ 2022-02-24 13:44 叨叨的蜗牛 阅读(2515) 评论(0) 推荐(0) 编辑
摘要:获取今日的日期的函数是curdate DATE_SUB函数的作用是返回从一个日期减去指定的时间间隔的结果,例如昨天select date_sub(curdate(), interval 1 day) from dual; Mysql中常用的几种时间类型有:date、datetime、time、yea 阅读全文
posted @ 2022-02-23 18:03 叨叨的蜗牛 阅读(347) 评论(0) 推荐(0) 编辑
摘要: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, ( 阅读全文
posted @ 2022-02-18 11:25 叨叨的蜗牛 阅读(127) 评论(0) 推荐(0) 编辑
摘要:第一种常规方法,利用子查询和集合计算:select lineCode, SUM(CASE tmp.alarmSubType WHEN '1' THEN tmp.count ELSE 0 END) '1', SUM(CASE tmp.alarmSubType WHEN '2' THEN tmp.cou 阅读全文
posted @ 2022-02-16 17:26 叨叨的蜗牛 阅读(919) 评论(0) 推荐(0) 编辑
摘要:dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值 例如:向日期加上2天 select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000 如果加0天,就是当前字符串要转成的日期格式(yyyymmdd) se 阅读全文
posted @ 2022-02-10 10:11 叨叨的蜗牛 阅读(3167) 评论(0) 推荐(0) 编辑
摘要:使用子查询,多层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_ 阅读全文
posted @ 2022-01-24 15:57 叨叨的蜗牛 阅读(401) 评论(0) 推荐(0) 编辑
摘要:GetDate select DateName(year,GetDate()) as '年'; select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) as '月',DateName(d 阅读全文
posted @ 2021-12-27 16:19 叨叨的蜗牛 阅读(595) 评论(0) 推荐(0) 编辑
摘要: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 阅读全文
posted @ 2021-09-08 13:05 叨叨的蜗牛 阅读(82) 评论(0) 推荐(0) 编辑
摘要: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 阅读全文
posted @ 2021-08-25 13:07 叨叨的蜗牛 阅读(37) 评论(0) 推荐(0) 编辑