이전 월말 값을 기준으로 누락 된 데이터 채우기


12

다음과 같은 데이터가 제공됩니다.

create table #histories
(
    username varchar(10),
    account varchar(10),
    assigned date  
);

insert into #histories 
values 
('PHIL','ACCOUNT1','2017-01-04'),
('PETER','ACCOUNT1','2017-01-15'),
('DAVE','ACCOUNT1','2017-03-04'),
('ANDY','ACCOUNT1','2017-05-06'),
('DAVE','ACCOUNT1','2017-05-07'),
('FRED','ACCOUNT1','2017-05-08'),
('JAMES','ACCOUNT1','2017-08-05'),
('DAVE','ACCOUNT2','2017-01-02'),
('PHIL','ACCOUNT2','2017-01-18'),
('JOSH','ACCOUNT2','2017-04-08'),
('JAMES','ACCOUNT2','2017-04-09'),
('DAVE','ACCOUNT2','2017-05-06'),
('PHIL','ACCOUNT2','2017-05-07') ; 

... 주어진 사용자가 계정에 할당 된시기를 나타냅니다.

매월 마지막 날에 지정된 계정을 소유 한 사람 (할당 된 날짜는 계정이 소유권을 이전 한 날짜) 을 설정하고 누락 된 월말이 채워진 경우 (사용 가능한 편리한 dates테이블 에서 생성 될 수 있음) 유용한 열 DateKey, DateLastDayOfMonth,) @AaronBertrand 호의] 1 .

원하는 결과는 다음과 같습니다.

PETER, ACCOUNT1, 2017-01-31
PETER, ACCOUNT1, 2017-02-28
DAVE, ACCOUNT1, 2017-03-31
DAVE, ACCOUNT1, 2017-04-30
FRED, ACCOUNT1, 2017-05-31
FRED, ACCOUNT1, 2017-06-30
FRED, ACCOUNT1, 2017-07-31
JAMES, ACCOUNT1, 2017-08-31
PHIL, ACCOUNT2, 2017-01-31
PHIL, ACCOUNT2, 2017-02-28
PHIL, ACCOUNT2, 2017-03-31
JAMES, ACCOUNT2, 2017-04-30
PHIL, ACCOUNT2, 2017-05-31

윈도우 기능으로 이것의 초기 부분을 수행하는 것은 사소한 일입니다.


당신은 필이 마지막 날에 계정을 가지고 있다고 가정하고 있습니다. 2017-05왜냐하면 그는 2017-05-07보유자가 없었기 때문입니까?
Evan Carroll

그렇습니다, 그것은 논리입니다
Philᵀᴹ

답변:


9

이 문제에 대한 한 가지 접근 방식은 다음을 수행하는 것입니다.

  1. LEADSQL Server 2008에서 에뮬레이션 APPLY하십시오. 또는이를 위해 suquery를 사용할 수 있습니다 .
  2. 다음 행이없는 행의 경우 계정 날짜에 한 달을 추가하십시오.
  3. 월 종료일이 포함 된 차원 테이블에 조인하십시오. 이렇게하면 최소 한 달에 걸쳐 있지 않은 모든 행이 제거되고 필요에 따라 간격을 채우기 위해 행이 추가됩니다.

결과를 결정적으로 만들기 위해 테스트 데이터를 약간 수정했습니다. 또한 색인을 추가했습니다.

create table #histories
(
    username varchar(10),
    account varchar(10),
    assigned date  
);

insert into #histories 
values 
('PHIL','ACCOUNT1','2017-01-04'),
('PETER','ACCOUNT1','2017-01-15'),
('DAVE','ACCOUNT1','2017-03-04'),
('ANDY','ACCOUNT1','2017-05-06'),
('DAVE','ACCOUNT1','2017-05-07'),
('FRED','ACCOUNT1','2017-05-08'),
('JAMES','ACCOUNT1','2017-08-05'),
('DAVE','ACCOUNT2','2017-01-02'),
('PHIL','ACCOUNT2','2017-01-18'),
('JOSH','ACCOUNT2','2017-04-08'), -- changed this date to have deterministic results
('JAMES','ACCOUNT2','2017-04-09'),
('DAVE','ACCOUNT2','2017-05-06'),
('PHIL','ACCOUNT2','2017-05-07') ;

-- make life easy
create index gotta_go_fast ON #histories (account, assigned);

모든 시간의 가장 늦은 날짜 측정 기준 테이블은 다음과 같습니다.

create table #date_dim_months_only (
    month_date date,
    primary key (month_date)
);

-- put 2500 month ends into table
INSERT INTO #date_dim_months_only WITH (TABLOCK)
SELECT DATEADD(DAY, -1, DATEADD(MONTH, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)), '20000101'))
FROM master..spt_values;

1 단계에는 에뮬레이션하는 방법이 많이 LEAD있습니다. 한 가지 방법이 있습니다.

SELECT 
  h1.username
, h1.account
, h1.assigned
, next_date.assigned
FROM #histories h1
OUTER APPLY (
    SELECT TOP 1 h2.assigned
    FROM #histories h2
    WHERE h1.account = h2.account
    AND h1.assigned < h2.assigned
    ORDER BY h2.assigned ASC
) next_date;

2 단계에서는 NULL 값을 다른 것으로 변경해야합니다. 각 계정의 마지막 달을 포함 시키려면 시작 날짜에 한 달을 추가하면 충분합니다.

ISNULL(next_date.assigned, DATEADD(MONTH, 1, h1.assigned))

3 단계에서는 날짜 차원 테이블에 조인 할 수 있습니다. 차원 테이블의 열은 결과 집합에 필요한 열입니다.

INNER JOIN #date_dim_months_only dd ON
    dd.month_date >= h1.assigned AND
    dd.month_date < ISNULL(next_date.assigned, DATEADD(MONTH, 1, h1.assigned))

나는 함께 모을 때 얻은 쿼리를 좋아하지 않았다. 결합 할 때 조인 순서에 문제가있을 수 있습니다 OUTER APPLYINNER JOIN. 조인 순서를 얻으려면 하위 쿼리로 다시 작성했습니다.

SELECT 
  hist.username
, hist.account
, dd.month_date 
FROM
(
    SELECT 
      h1.username
    , h1.account
    , h1.assigned
    , ISNULL(
        (SELECT TOP 1 h2.assigned
            FROM #histories h2
            WHERE h1.account = h2.account
            AND h1.assigned < h2.assigned
            ORDER BY h2.assigned ASC
        )
        , DATEADD(MONTH, 1, h1.assigned)
    ) next_assigned
    FROM #histories h1
) hist
INNER JOIN #date_dim_months_only dd ON
    dd.month_date >= hist.assigned AND
    dd.month_date < hist.next_assigned;

데이터가 얼마나 있는지 잘 모르므로 중요하지 않을 수 있습니다. 그러나 계획은 내가 원하는 방식으로 보입니다.

좋은 계획

결과는 당신과 일치합니다.

╔══════════╦══════════╦════════════╗
 username  account   month_date 
╠══════════╬══════════╬════════════╣
 PETER     ACCOUNT1  2017-01-31 
 PETER     ACCOUNT1  2017-02-28 
 DAVE      ACCOUNT1  2017-03-31 
 DAVE      ACCOUNT1  2017-04-30 
 FRED      ACCOUNT1  2017-05-31 
 FRED      ACCOUNT1  2017-06-30 
 FRED      ACCOUNT1  2017-07-31 
 JAMES     ACCOUNT1  2017-08-31 
 PHIL      ACCOUNT2  2017-01-31 
 PHIL      ACCOUNT2  2017-02-28 
 PHIL      ACCOUNT2  2017-03-31 
 JAMES     ACCOUNT2  2017-04-30 
 PHIL      ACCOUNT2  2017-05-31 
╚══════════╩══════════╩════════════╝

500k 행 야간 ETL의 일부이므로 밀리 초 단위로 실행할 필요가 없습니다. :)
Philᵀᴹ

4

여기서는 달력 테이블을 사용하지 않고 자연수 테이블 nums.dbo.nums를 사용합니다 (그렇지 않으면 쉽게 생성 할 수 있기를 바랍니다)

데이터에 다음 두 행이 포함되어 있기 때문에 귀하와 약간 다른 답변 ( 'JOSH'<-> 'JAMES')이 있습니다.

('JOSH','ACCOUNT2','2017-04-09'),
('JAMES','ACCOUNT2','2017-04-09'),

동일한 계정과 지정된 날짜를 가지고 있으며 어떤 상황을 취해야하는지 정확히 알지 못했습니다.

declare @eom table(account varchar(10), dt date); 

with acc_mm AS
(
select account, min(assigned) as min_dt, max(assigned) as max_dt
from #histories
group by account
),

acc_mm1 AS
(
select account,
       dateadd(month, datediff(month, '19991231', min_dt), '19991231') as start_dt,
       dateadd(month, datediff(month, '19991231', max_dt), '19991231') as end_dt
from acc_mm
)

insert into @eom (account, dt) 
select account, dateadd(month, n - 1, start_dt)
from acc_mm1
      join nums.dbo.nums            
           on n - 1 <= datediff(month, start_dt, end_dt); 

select eom.dt, eom.account, a.username
from @eom eom 
     cross apply(select top 1 *
                 from #histories h 
                 where eom.account = h.account
                   and h.assigned <= eom.dt
                 order by h.assigned desc) a
order by eom.account, eom.dt;                          

2

이것은 결코 깨끗한 솔루션이 아니지만 원하는 결과를 제공하는 것 같습니다 (다른 사람들이 훌륭하고 깨끗하며 완전히 최적화 된 쿼리를 가질 것이라고 확신합니다).

create table #histories
(
    username varchar(10),
    account varchar(10),
    assigned date  
);

insert into #histories 
values 
('PHIL','ACCOUNT1','2017-01-04'),
('PETER','ACCOUNT1','2017-01-15'),
('DAVE','ACCOUNT1','2017-03-04'),
('ANDY','ACCOUNT1','2017-05-06'),
('DAVE','ACCOUNT1','2017-05-07'),
('FRED','ACCOUNT1','2017-05-08'),
('JAMES','ACCOUNT1','2017-08-05'),
('DAVE','ACCOUNT2','2017-01-02'),
('PHIL','ACCOUNT2','2017-01-18'),
('JOSH','ACCOUNT2','2017-04-09'),
('JAMES','ACCOUNT2','2017-04-09'),
('DAVE','ACCOUNT2','2017-05-06'),
('PHIL','ACCOUNT2','2017-05-07') ; 


IF (SELECT OBJECT_ID(N'tempdb..#IncompleteResults')) IS NOT NULL
    DROP TABLE #IncompleteResults;

DECLARE @EOMTable TABLE ( EndOfMonth DATE );
DECLARE @DateToWrite DATE = '2017-01-31';
WHILE @DateToWrite < '2017-10-31'
    BEGIN
        INSERT  INTO @EOMTable
                ( EndOfMonth )
                SELECT  @DateToWrite;

        SELECT  @DateToWrite = EOMONTH(DATEADD(MONTH, 1, @DateToWrite));
    END

    ;
WITH    cteAccountsByMonth
          AS ( SELECT   EndOfMonth ,
                        account
               FROM     @EOMTable e
                        CROSS JOIN ( SELECT DISTINCT
                                            account
                                     FROM   #histories
                                   ) AS h
             ),
        cteHistories
          AS ( SELECT   username ,
                        account ,
                        ROW_NUMBER() OVER ( PARTITION BY ( CAST(DATEPART(YEAR,
                                                              assigned) AS CHAR(4))
                                                           + ( RIGHT('00'
                                                              + CAST(DATEPART(MONTH,
                                                              assigned) AS VARCHAR(10)),
                                                              2) ) ), account ORDER BY assigned DESC ) AS rownum ,
                        CAST(DATEPART(YEAR, assigned) AS CHAR(4)) + RIGHT('00'
                                                              + CAST(DATEPART(MONTH,
                                                              assigned) AS VARCHAR(10)),
                                                              2) AS PartialDate ,
                        assigned ,
                        EOMONTH(assigned) AS EndofMonth
               FROM     #histories
             )
    SELECT  username ,
            e.EndOfMonth ,
            e.account
    INTO #IncompleteResults
    FROM    cteAccountsByMonth e
            LEFT JOIN cteHistories c ON e.EndOfMonth = c.EndofMonth
                                        AND c.account = e.account
                                        AND c.rownum = 1
SELECT  CASE WHEN username IS NULL
             THEN ( SELECT  username
                    FROM    #IncompleteResults i2
                    WHERE   username IS NOT NULL
                            AND i.account = i2.account
                            AND i2.EndOfMonth = ( SELECT    MAX(EndOfMonth)
                                                  FROM      #IncompleteResults i3
                                                  WHERE     i3.EndOfMonth < i.EndOfMonth
                                                            AND i3.account = i.account
                                                            AND i3.username IS NOT NULL
                                                )
                  )
             ELSE username
        END AS username ,
        EndOfMonth ,
        account 
FROM    #IncompleteResults i
ORDER BY account ,
        i.EndOfMonth;

2

귀하의 질문에 언급 한 바와 같이 Aaron Bertrand 의 날짜 차원 테이블 을 사용했으며 (이러한 시나리오에는 매우 유용한 테이블입니다) 다음 코드를 작성했습니다.

다음 코드를 사용하여 테이블에 EndOfMonth열을 추가했습니다 ( 열 #dim바로 FirstOfMonth다음에).

 EndOfMonth as dateadd(s,-1,dateadd(mm, datediff(m,0,[date])+1,0)),

그리고 해결책 :

if object_id('tempdb..#temp') is not null drop table #temp
create table #temp (nr int, username varchar(100), account varchar(100), eom date)

;with lastassignedpermonth as
(
    select 
           month(assigned) month
         , account
         , max(assigned) assigned
    from 
           #histories 
    group by month(assigned), account 
)
insert into #temp
select 
       distinct row_number() over (order by d.account, d.eom) nr
     , h.username
     , d.account
     , d.eom
from ( 
        select distinct month, cast(d.endofmonth as date) eom, t.account 
        from #dim d cross apply (select distinct account from #histories) t 
     ) d
            left join lastassignedpermonth l on d.month = l.month and l.assigned <= d.eom and d.account = l.account 
            left join #histories h on h.assigned = l.assigned and h.account = l.account 
where d.eom <=  dateadd(s,-1,dateadd(mm, datediff(m,0,getdate())+1,0)) -- end of current month
order by d.account, eom 

-- This could have been done in one single statement with the lead() function but that is available as of SQL Server 2012
select case when t.username is null then (select username from #temp where nr = previous_username.nr) else t.username end as username, t.account, t.eom 
from #temp as t cross apply ( 
                                select max(nr) nr 
                                from #temp as t1
                                where t1.nr < t.nr and t1.username is not null
                            ) as previous_username

/*
   Note: You get twice JAMES and JOSH for April/ACCOUNT2, because apparently they are both assigned on the same date(2017-04-09)... 
   I guess your data should be cleaned up of overlapping dates.
*/

2

승리를 위해 삼각형 가입하세요!

SELECT account,EndOfMonth,username
FROM (
    SELECT Ends.*, h.*
        ,ROW_NUMBER() OVER (PARTITION BY h.account,Ends.EndOfMonth ORDER BY h.assigned DESC) AS RowNumber
    FROM (
        SELECT [Year],[Month],MAX(DATE) AS EndOfMonth
        FROM #dim
        GROUP BY [Year],[Month]
        ) Ends
    CROSS JOIN (
        SELECT account, MAX(assigned) AS MaxAssigned
        FROM #histories
        GROUP BY account
        ) ac
    JOIN #histories h ON h.account = ac.account
        AND Year(h.assigned) = ends.[Year]
        AND Month(h.assigned) <= ends.[Month] --triangle join for the win!
        AND EndOfMonth < DATEADD(month, 1, Maxassigned)
    ) Results
WHERE RowNumber = 1
ORDER BY account,EndOfMonth;

결과는 다음과 같습니다.

account     EndOfMonth  username

ACCOUNT1    2017-01-31  PETER
ACCOUNT1    2017-02-28  PETER
ACCOUNT1    2017-03-31  DAVE
ACCOUNT1    2017-04-30  DAVE
ACCOUNT1    2017-05-31  FRED
ACCOUNT1    2017-06-30  FRED
ACCOUNT1    2017-07-31  FRED
ACCOUNT1    2017-08-31  JAMES

ACCOUNT2    2017-01-31  PHIL
ACCOUNT2    2017-02-28  PHIL
ACCOUNT2    2017-03-31  PHIL
ACCOUNT2    2017-04-30  JAMES
ACCOUNT2    2017-05-31  PHIL

대화식 실행 계획은 여기입니다.

I / O 및 TIME 통계 (논리적 읽기 후 모든 0 값이 잘림) :

(13 row(s) affected)

Table 'Worktable'.  Scan count 3, logical reads 35.
Table 'Workfile'.   Scan count 0, logical reads  0.
Table '#dim'.       Scan count 1, logical reads  4.
Table '#histories'. Scan count 1, logical reads  1.

SQL Server Execution Times:
    CPU time = 0 ms,  elapsed time = 3 ms.

필수 '임시 테이블을 작성하고 제안하는 T-SQL 문을 테스트하기 위해 쿼리하십시오.

IF OBJECT_ID('tempdb..#histories') IS NOT NULL
    DROP TABLE #histories

CREATE TABLE #histories (
    username VARCHAR(10)
    ,account VARCHAR(10)
    ,assigned DATE
    );

INSERT INTO #histories
VALUES
('PHIL','ACCOUNT1','2017-01-04'),
('PETER','ACCOUNT1','2017-01-15'),
('DAVE','ACCOUNT1','2017-03-04'),
('ANDY','ACCOUNT1','2017-05-06'),
('DAVE','ACCOUNT1','2017-05-07'),
('FRED','ACCOUNT1','2017-05-08'),
('JAMES','ACCOUNT1','2017-08-05'),
('DAVE','ACCOUNT2','2017-01-02'),
('PHIL','ACCOUNT2','2017-01-18'),
('JOSH','ACCOUNT2','2017-04-08'),
('JAMES','ACCOUNT2','2017-04-09'),
('DAVE','ACCOUNT2','2017-05-06'),
('PHIL','ACCOUNT2','2017-05-07');

DECLARE @StartDate DATE = '20170101'
    ,@NumberOfYears INT = 2;

-- prevent set or regional settings from interfering with 
-- interpretation of dates / literals
SET DATEFIRST 7;
SET DATEFORMAT mdy;
SET LANGUAGE US_ENGLISH;

DECLARE @CutoffDate DATE = DATEADD(YEAR, @NumberOfYears, @StartDate);

-- this is just a holding table for intermediate calculations:
IF OBJECT_ID('tempdb..#dim') IS NOT NULL
    DROP TABLE #dim

CREATE TABLE #dim (
    [date] DATE PRIMARY KEY
    ,[day] AS DATEPART(DAY, [date])
    ,[month] AS DATEPART(MONTH, [date])
    ,FirstOfMonth AS CONVERT(DATE, DATEADD(MONTH, DATEDIFF(MONTH, 0, [date]), 0))
    ,[MonthName] AS DATENAME(MONTH, [date])
    ,[week] AS DATEPART(WEEK, [date])
    ,[ISOweek] AS DATEPART(ISO_WEEK, [date])
    ,[DayOfWeek] AS DATEPART(WEEKDAY, [date])
    ,[quarter] AS DATEPART(QUARTER, [date])
    ,[year] AS DATEPART(YEAR, [date])
    ,FirstOfYear AS CONVERT(DATE, DATEADD(YEAR, DATEDIFF(YEAR, 0, [date]), 0))
    ,Style112 AS CONVERT(CHAR(8), [date], 112)
    ,Style101 AS CONVERT(CHAR(10), [date], 101)
    );

-- use the catalog views to generate as many rows as we need

INSERT #dim ([date])
SELECT d
FROM (
    SELECT d = DATEADD(DAY, rn - 1, @StartDate)
    FROM (
        SELECT TOP (DATEDIFF(DAY, @StartDate, @CutoffDate)) rn = ROW_NUMBER() OVER (
                ORDER BY s1.[object_id]
                )
        FROM sys.all_objects AS s1
        CROSS JOIN sys.all_objects AS s2
        -- on my system this would support > 5 million days
        ORDER BY s1.[object_id]
        ) AS x
    ) AS y;

/* The actual SELECT statement to get the results we want! */

SET STATISTICS IO, TIME ON;

SELECT account,EndOfMonth,username
FROM (
    SELECT Ends.*, h.*
        ,ROW_NUMBER() OVER (PARTITION BY h.account,Ends.EndOfMonth ORDER BY h.assigned DESC) AS RowNumber
    FROM (
        SELECT [Year],[Month],MAX(DATE) AS EndOfMonth
        FROM #dim
        GROUP BY [Year],[Month]
        ) Ends
    CROSS JOIN (
        SELECT account, MAX(assigned) AS MaxAssigned
        FROM #histories
        GROUP BY account
        ) ac
    JOIN #histories h ON h.account = ac.account
        AND Year(h.assigned) = ends.[Year]
        AND Month(h.assigned) <= ends.[Month] --triangle join for the win!
        AND EndOfMonth < DATEADD(month, 1, Maxassigned)
    ) Results
WHERE RowNumber = 1
ORDER BY account,EndOfMonth;

SET STATISTICS IO, TIME OFF;

--IF OBJECT_ID('tempdb..#histories') IS NOT NULL DROP TABLE #histories
--IF OBJECT_ID('tempdb..#dim') IS NOT NULL DROP TABLE #dim
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.