一个比较复杂的sql
2008-06-26 10:37
323 查看
declare @statTime datetime, @endTime datetime set @statTime = dateadd(hh,datediff(hh,0,getdate())-1,0) set @endTime = dateadd(hh,datediff(hh,0,getdate()),0)
select ua.SPID,@statTime as statTime,mt.BusinessType , case when r.运营商名称 is null then '未知' else r.运营商名称 end 运营商名称, case when r.地区名称 is null then '未知' else r.地区名称 end 地区名称, count(distinct mt.MTID) as MtOkNum from dbo.SMS_Log_MT mt, sms.SMS_System.dbo.SMS_Users_All ua left join sms.SMS_System.dbo.MS_Rigor r on substring(ua.mobile,1,7) = r.起始号段 where mt.userid = ua.userid and year(mt.time) = year(@statTime) and month(mt.time) = month(@statTime) and day(mt.time) = day(@statTime) and datepart(hh,mt.time) = datepart(hh,@statTime) --匹配时间 and mt.Result = 'DELIVRD'--成功发送 group by ua.SPID,mt.BusinessType ,r.运营商名称,r.地区名称
总结要点:1、declare set的用法.
2、dateadd(dd,datediff(dd,0,someDate),0)的用法
比如:获取当前时间的上一个小时
select dateadd(hh,datediff(hh,0,getdate())-1,0)
--'2008-06-26 09:00:00'
3、语法---case when condition then result1 else result2 end (as) '别名' 4、table left join anotherTable on table.key = anotherTable.key
相关文章推荐
- 【Sql语句】自己摸索出得比较复杂的一个查询语句
- 一个比较复杂的sql
- 一个SQL存储过程面试题(比较简单)
- 使用web3部署一个比较复杂的智能合约
- SQL中条件和比较关键字Case的使用方法(case的结果就是得到了一个值)
- 一个比较复杂的多次拆分字符串的存储过程
- 一个跨库复杂查询的SQL优化的案例
- 比较复杂的动态SQL语句功能一例[不太懂,好好研究一下]
- 复杂sql,ab有一个空
- SQL 比较一个集合是否在另一个集合里存在的方法分享
- 一个比较复杂的RelativeLayout布局---类似于表格布局
- 一个复杂的sql
- 工作流Activiti的学习总结- 整合比较复杂的一个流程
- 一个效率比较高的分页存储过程(SQL)
- (SQL)比较一个集合是否在另一个集合里存在的方法
- poj 3728 merchant 一个比较复杂的lca+dp
- mark: 写了一个复杂的sql
- 一个sql提问,引起的研究。关于union all 中,嵌套查询和直接查询的效率比较。
- 比较复杂的动态SQL语句功能一例
- oracle 用到的一个比较复杂的sql函数