获取制定日期区间 declare @d as date declare @d2 as date set @d = '2014-06-03' set @d2 ='2014-06-10' ),dateadd(dd,number,@d)) as dt ,dateadd(dd,-,convert(varchar(),dateadd(mm,,@d),)+') from master..spt_values where type='P' and dateadd(dd,number,@d)<=@d2 获取制定
表tariff_info, 原始数据: 想要的结果:以start_time时间倒序排序, 以code分类, 查询每一类最新的一条记录 sql: SELECT a.* FROM TARIFF_INFO a, ( SELECT code, MAX (start_time) start_time FROM TARIFF_INFO GROUP BY code ) b WHERE a.start_time = b.start_time AND a.code = b.code ORDER BY a.code
有两个导数据的需求,1.一张表里面每一个订单号可能对应多条数据,每个单号返回时间最早的那条. 2.根据条件查询某个字段并按照逗号,合并在一起. 表类似结构如下: 第一条sql:select c.Id,c.OrderNum, c.CreateTime FROM (select t.*,row_number() over(partition by t.OrderNum order by t.CreateTime ) rn from Payinfo t WHERE t.OrderNum IN('20
* FROM sys.objects WHERE name=N'uF_IsRange_Date' AND [type]='FN') DROP FUNCTION uF_IsRange_Date GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: hehai -- Create date: 2015-11-19 11:01:01
以前经常看到人查询数据库采用left join及case方式,一条一条的枚举查询整个数据的数据区间方法可行,但是数据一但很大,枚举就死悄悄,在网上查看,几乎全是照抄case ,left join枚举无耐自己写了个存储过程,希望给大家抛砖引玉! set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROC pro_UserList @jisum int,--基数 @step int,--步长 @max int--列举最大值 as SET NOCO