如果我从月份,日期,年份 中选择一个表组,它只会返回包含记录的行并且不包含任何记录的组合,使其一目了然每天或每月都有活动,您必须积极查看日期列以查找差距。如何在T-SQL中每天/每月/每年获得一行,即使没有数据存在?按日,月,年分组时,sql缺少行
回答
My developer回来给我这个代码,下划线转换为破折号,因为StackOverflow的是重整下划线 - 不需要数字表。我们的例子通过与另一个表的连接而变得复杂一些,但也许代码示例有一天会帮助别人。
declare @career-fair-id int
select @career-fair-id = 125
create table #data ([date] datetime null, [cumulative] int null)
declare @event-date datetime, @current-process-date datetime, @day-count int
select @event-date = (select careerfairdate from tbl-career-fair where careerfairid = @career-fair-id)
select @current-process-date = dateadd(day, -90, @event-date)
while @event-date <> @current-process-date
begin
select @current-process-date = dateadd(day, 1, @current-process-date)
select @day-count = (select count(*) from tbl-career-fair-junction where attendanceregister <= @current-process-date and careerfairid = @career-fair-id)
if @current-process-date <= getdate()
insert into #data ([date], [cumulative]) values(@current-process-date, @day-count)
end
select * from #data
drop table #data
创建日历表外连接表上的
考虑使用一个numbers table。虽然这可能是个骇人听闻的问题,但是,无论是否使用该范围内的所有值,它都是我快速查询缺失数据,显示所有日期或任何想要检查某个范围内的值的最佳方法。
基于SQLMenace的说法,您可以使用CROSS JOIN快速填充表或在内存中有效地创建它。
http://www.sitepoint.com/forums/showthread.php?t=562806
一套完整日期的任务调用为左加入到你的数据,如
DECLARE @StartInt int
DECLARE @Increment int
DECLARE @Iterations int
SET @StartInt = 0
SET @Increment = 1
SET @Iterations = 365
SELECT
tCompleteDateSet.[Date]
,AggregatedMeasure = SUM(ISNULL(t.Data, 0))
FROM
(
SELECT
[Date] = dateadd(dd,GeneratedInt, @StartDate)
FROM
[dbo].[tvfUtilGenerateIntegerList] (
@StartInt,
,@Increment,
,@Iterations
)
) tCompleteDateSet
LEFT JOIN tblData t
ON (t.[Date] = tCompleteDateSet.[Date])
GROUP BY
tCompleteDateSet.[Date]
所在的表值函数tvfUtilGenerateIntegerList被定义为
-- Example Inputs
-- DECLARE @StartInt int
-- DECLARE @Increment int
-- DECLARE @Iterations int
-- SET @StartInt = 56200
-- SET @Increment = 1
-- SET @Iterations = 400
-- DECLARE @tblResults TABLE
-- (
-- IterationId int identity(1,1),
-- GeneratedInt int
--)
-- =============================================
-- Author: 6eorge Jetson
-- Create date: 11/22/3333
-- Description: Generates and returns the desired list of integers as a table
-- =============================================
CREATE FUNCTION [dbo].[tvfUtilGenerateIntegerList]
(
@StartInt int,
@Increment int,
@Iterations int
)
RETURNS
@tblResults TABLE
(
IterationId int identity(1,1),
GeneratedInt int
)
AS
BEGIN
DECLARE @counter int
SET @counter= 0
WHILE (@counter < @Iterations)
BEGIN
INSERT @tblResults(GeneratedInt) VALUES(@StartInt + @counter*@Increment)
SET @counter = @counter + 1
END
RETURN
END
--Debug
--SELECT * FROM @tblResults
- 1. 按日/月/年/日/月/日/年等搜索SQL查询
- 2. LINQ如何按日期分组日期和月份和年份
- 3. 组按年,月,日的SQLAlchemy的
- 4. 分组生日按月份
- 5. 按月分组SAS日期
- 6. 如何按月份+年份分组?
- 7. 按月份和年份分组
- 8. 如何按年份和月份分组?
- 9. 按月份和年份分组
- 10. MySQL中按年份和月份分组
- 11. 按年份,月份和日期分组设置
- 12. Ruby:按年份和月份分组日期范围
- 13. SQL:按月份和年份
- 14. Linq to SQL筛选日期时间字段按年/月
- 15. Informix DB-SQL Group按年,月,周,日,小时
- 16. 按月份和星期分组,按日期时间列分组,按每月或每周的第一个分组
- 17. LINQ:在日期时间字段内按月份和年份分组
- 18. 11年3月20日上午11:24(月/日/年时:MIN)
- 19. 按年份,月份分组时关闭计数值
- 20. 按月分割日期范围SQL Server
- 21. SQL填写缺少日期
- 22. SQL查询按日期分组时间
- 23. 按月分组生日结果
- 24. 按星期,月份,日期分组
- 25. 将日期时间拆分为日期/年/月/日名称和计数年份
- 26. SQL按月份组
- 27. SQL Server组按月
- 28. SQL按周分组日期
- 29. LINQ to SQL按年龄分组的人
- 30. 转换日期时间年月日通过T-SQL DDMMYYYY直接