2008-09-02 9 views
3

月、日、年別にテーブルグループから選択すると、レコードのある行のみが返され、レコードはなくなり、一目で表示されます毎日または毎月に活動がある場合は、ギャップのために積極的に日付列を見なければなりません。 T-SQLでは、データが存在しない場合でも、毎日、毎月、または1年ごとに行を取得するにはどうすればよいですか?DAY、MONTH、YEARでグループ化したときにsqlが欠落している行

答えて

1

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 
4

カレンダーテーブルと外側には、そのテーブルの上に

0

に参加作成はnumbers tableを使用して調べてください。それはハッキングすることができますが、欠落したデータをすばやく照会したり、すべての日付を表示したり、その範囲内のすべての値が使用されているかどうかにかかわらず、

0

タスクは、テーブル値関数tvfUtilGenerateIntegerListが

として定義される


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]

として左接合されるデータに日付の完全なセットを要求します
-- 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

関連する問題