16

希望する階段状の出力に一致する MS SQL 2012 クエリを作成するための助けが必要です。行は 1 つの日付範囲 (アカウント登録日月) ごとにデータを要約し、列は別の日付範囲 (支払日月) ごとに要約します。

表 1:アカウントは、コレクション用に配置されたアカウントを追跡します。

CREATE TABLE [dbo].[Accounts](
    [AccountID] [nchar](10) NOT NULL,
    [SubmissionDate] [date] NOT NULL,
    [Amount] [money] NOT NULL,
CONSTRAINT [PK_Accounts] PRIMARY KEY CLUSTERED (AccountID ASC))

INSERT INTO [dbo].[Accounts] VALUES ('1000', '2012-01-01', 1999.00)
INSERT INTO [dbo].[Accounts] VALUES ('1001', '2012-01-02', 100.00)
INSERT INTO [dbo].[Accounts] VALUES ('1002', '2012-02-05', 350.00)
INSERT INTO [dbo].[Accounts] VALUES ('1003', '2012-03-01', 625.00)
INSERT INTO [dbo].[Accounts] VALUES ('1004', '2012-03-10', 50.00)
INSERT INTO [dbo].[Accounts] VALUES ('1005', '2012-03-10', 10.00)

表 2: Trans track の支払い

CREATE TABLE [dbo].[Trans](
    [TranID] [int] IDENTITY(1,1) NOT NULL,
    [AccountID] [nchar](10) NOT NULL,
    [TranDate] [date] NOT NULL,
    [TranAmount] [money] NOT NULL,
CONSTRAINT [PK_Trans] PRIMARY KEY CLUSTERED (TranID ASC))

INSERT INTO [dbo].[Trans] VALUES (1000, '2012-01-15', 300.00)
INSERT INTO [dbo].[Trans] VALUES (1000, '2012-02-15', 300.00)
INSERT INTO [dbo].[Trans] VALUES (1000, '2012-03-15', 300.00)
INSERT INTO [dbo].[Trans] VALUES (1002, '2012-02-20', 325.00)
INSERT INTO [dbo].[Trans] VALUES (1002, '2012-04-20', 25.00)
INSERT INTO [dbo].[Trans] VALUES (1003, '2012-03-24', 625.00)
INSERT INTO [dbo].[Trans] VALUES (1004, '2012-03-28', 31.00)
INSERT INTO [dbo].[Trans] VALUES (1004, '2012-04-12', 5.00)
INSERT INTO [dbo].[Trans] VALUES (1005, '2012-04-08', 7.00)
INSERT INTO [dbo].[Trans] VALUES (1005, '2012-04-28', 3.00)

目的の出力は次のようになります

                                 *Total Payments in Each Month*
SubmissionYearMonth TotalAmount | 2012-01  2012-02  2012-03  2012-04 
--------------------------------------------------------------------
2012-01             2099.00     |  300.00   300.00   300.00     0.00
2012-02             350.00      |           325.00     0.00    25.00
2012-03             685.00      |                    656.00    15.00

最初の 2 つの列は、月ごとにグループ化された Account.Amount の合計です。

最後の 4 列は、現在の行の特定の月に配置されたアカウントの Tran.TranAmount を月ごとに合計します。

私が取り組んできたクエリは身近に感じます。ラグが正しくないだけです。これまでに取り組んでいるクエリは次のとおりです。

Select SubmissionYearMonth, 
       TotalAmount,
       pt.[0] AS MonthOld0,
       pt.[1] AS MonthOld1,
       pt.[2] AS MonthOld2,
       pt.[3] AS MonthOld3,
       pt.[4] AS MonthOld4,
       pt.[5] AS MonthOld5,
       pt.[6] AS MonthOld6,
       pt.[7] AS MonthOld7,
       pt.[8] AS MonthOld8,
       pt.[9] AS MonthOld9,
       pt.[10] AS MonthOld10,
       pt.[11] AS MonthOld11,
       pt.[12] AS MonthOld12,
       pt.[13] AS MonthOld13

From (
       SELECT Convert(Char(4),Year(SubmissionDate)) + '-' + Right('00' + Convert(VarChar(2), DatePart(Month, SubmissionDate)),2) AS SubmissionYearMonth, 
       SUM(Amount) AS TotalAmount
       FROM   Accounts
       GROUP BY Convert(Char(4),Year(SubmissionDate)) + '-' + Right('00' + Convert(VarChar(2), DatePart(Month, SubmissionDate)),2)
     ) 
AS AccountSummary
OUTER APPLY
(
SELECT *
FROM (
       SELECT CASE WHEN DATEDIFF(Month, SubmissionDate, TranDate) < 13
                   THEN DATEDIFF(Month, SubmissionDate, TranDate)
                   ELSE 13
              END AS PaymentMonthAge,
              TranAmount
       FROM Trans INNER JOIN Accounts ON Trans.AccountID = Accounts.AccountID
       Where Convert(Char(4),Year(TranDate)) + '-' + Right('00' + Convert(VarChar(2), DatePart(Month, TranDate)),2)
             = AccountSummary.SubmissionYearMonth
       ) as TransTemp
       PIVOT (SUM(TranAmount)
              FOR PaymentMonthAge IN ([0],
                                      [1],
                                      [2],
                                      [3],
                                      [4],
                                      [5],
                                      [6],
                                      [7],
                                      [8],
                                      [9],
                                      [10],
                                      [11],
                                      [12],
                                      [13])) as TransPivot
) as pt

次の出力が生成されます。

SubmissionYearMonth TotalAmount MonthOld0 MonthOld1 MonthOld2 MonthOld3 ...
2012-01             2099.00     300.00    NULL      NULL      NULL      ...
2012-02             350.00      325.00    300.00    NULL      NULL      ...
2012-03             685.00      656.00    NULL      300.00    NULL      ...

列の日付ヘッダーについて。ここで何が最善の選択肢なのかわかりません。追加の列セットを追加して、結果のレポートで使用できる計算値を作成できます。

SQL フィドル: http://www.sqlfiddle.com/#!6/272e5/1/0

4

4 に答える 4

5

SQL Server 2012 を使用しているため、Format 関数を使用して日付をきれいにすることができます。文字列でグループ化する必要はありません。代わりに、できるだけ適切なデータ型を使用し、Format または Convert on display のみを使用する (またはまったく使用せず、中間層に表示を処理させる) と便利です。

このソリューションでは、最も早い TransDate を任意に想定し、その月の 1 日を抽出します。ただし、その式を目的の開始日の静的な値に簡単に置き換えることができ、このソリューションにはそれと次の 12 か月がかかります。

With SubmissionMonths As
  (
  Select DateAdd(d, -Day(A.SubmissionDate) + 1, A.SubmissionDate) As SubmissionMonth
    , A.Amount
  From dbo.Accounts As A
  )
  , TranMonths As
  (
  Select DateAdd(d, -Day(Min( T.TranDate )) + 1, Min( T.TranDate )) As TranMonth
      , 1 As MonthNum
  From dbo.Accounts As A
    Join dbo.Trans As T
      On T.AccountId = A.AccountId
    Join SubmissionMonths As M
      On A.SubmissionDate >= M.SubmissionMonth
        And A.SubmissionDate < DateAdd(m,1,SubmissionMonth)
  Union All
  Select DateAdd(m, 1, TranMonth), MonthNum + 1
  From TranMonths
  Where MonthNum < 12
  )
  , TotalBySubmissionMonth As
  (
  Select M.SubmissionMonth, Sum( M.Amount ) As Total
  From SubmissionMonths As M
  Group By M.SubmissionMonth
  )
Select Format(SMT.SubmissionMonth,'yyyy-MM') As SubmissionMonth, SMT.Total
  , Sum( Case When TM.MonthNum = 1 Then T.TranAmount End ) As Month1
  , Sum( Case When TM.MonthNum = 2 Then T.TranAmount End ) As Month2
  , Sum( Case When TM.MonthNum = 3 Then T.TranAmount End ) As Month3
  , Sum( Case When TM.MonthNum = 4 Then T.TranAmount End ) As Month4
  , Sum( Case When TM.MonthNum = 5 Then T.TranAmount End ) As Month5
  , Sum( Case When TM.MonthNum = 6 Then T.TranAmount End ) As Month6
  , Sum( Case When TM.MonthNum = 7 Then T.TranAmount End ) As Month7
  , Sum( Case When TM.MonthNum = 8 Then T.TranAmount End ) As Month8
  , Sum( Case When TM.MonthNum = 9 Then T.TranAmount End ) As Month9
  , Sum( Case When TM.MonthNum = 10 Then T.TranAmount End ) As Month10
  , Sum( Case When TM.MonthNum = 11 Then T.TranAmount End ) As Month11
  , Sum( Case When TM.MonthNum = 12 Then T.TranAmount End ) As Month12
From TotalBySubmissionMonth As SMT
  Join dbo.Accounts As A
    On A.SubmissionDate >= SMT.SubmissionMonth
      And A.SubmissionDate < DateAdd(m,1,SMT.SubmissionMonth)
  Join dbo.Trans As T
    On T.AccountId = A.AccountId
  Join TranMonths As TM
    On T.TranDate >= TM.TranMonth
      And T.TranDate < DateAdd(m,1,TM.TranMonth)
Group By SMT.SubmissionMonth, SMT.Total

SQL フィドルのバージョン

于 2012-06-27T15:57:47.473 に答える
1

トーマス、私はあなたの応答を、私が最終的に使用した次のソリューションのインスピレーションとして使用しました.

最初に、SubmissionDate、TranDate クロス結合スケルトン日付マトリックスを作成します。これは、後で AccountSummary および TranSummary データを結合するために使用します。

結果のクエリ出力は、TranDate 月ごとに列に書式設定されません。むしろ、SQL Server Reporting Services マトリックスで出力を使用し、TranSummaryMonthNum 列に基づいて列グループ化を使用して、目的の書式設定された出力を取得しています。

SQL フィドルのバージョン

;
WITH 
    --Generate a list of Dates, from the first SubmissionDate, through today.
    --Note: Requires the use of: 'OPTION (MAXRECURSION 0)' to generate a list with more than 100 dates.
    CTE_AutoDates AS
    ( Select Min(SubmissionDate) as FiscalDate
      From Accounts
      UNION ALL
      SELECT DATEADD(Day, 1, FiscalDate)
      FROM CTE_AutoDates
      WHERE DATEADD(Day, 1, FiscalDate) <= GetDate()
    ),

    FiscalDates As
    ( SELECT FiscalDate,
             DATEFROMPARTS(Year(FiscalDate), Month(FiscalDate), 1) as FiscalMonthStartDate  
      FROM CTE_AutoDates
      --Optionaly filter Fiscal Dates by the last known Math.Max(SubmissionDate, TranDate)
      Where FiscalDate <= (Select Max(MaxDate)
                           From (Select Max(SubmissionDate) as MaxDate From Accounts
                                 Union All 
                                 Select Max(TranDate) as MaxDate From Trans
                                ) as MaxDates
                         )
    ),

    FiscalMonths as
    ( SELECT Distinct FiscalMonthStartDate 
      FROM FiscalDates
    ),

    --Matrix to store the reporting date groupings for the Account submission and payment periods.
    SubmissionAndTranMonths AS
    ( Select AM.FiscalMonthStartDate as SubmissionMonthStartDate,
             TM.FiscalMonthStartDate as TransMonthStartDate,
             DateDiff(Month, (Select Min(FiscalMonthStartDate) From FiscalMonths), TM.FiscalMonthStartDate) as TranSummaryMonthNum
      From   FiscalMonths AS AM
             Join FiscalMonths AS TM
             ON TM.FiscalMonthStartDate >= AM.FiscalMonthStartDate
    ),

    AccountData as
    ( Select A.AccountID, 
             A.Amount,
             FD.FiscalMonthStartDate as SubmissionMonthStartDate
      From   Accounts as A
           Inner Join FiscalDates as FD
             ON A.SubmissionDate = FD.FiscalDate
    ),


    TranData as
    ( Select T.AccountID,
             T.TranAmount,
             AD.SubmissionMonthStartDate,
             FD.FiscalMonthStartDate as TranMonthStartDate
      From   Trans as T
           Inner Join AccountData as AD
             ON T.AccountID = AD.AccountID
           Inner Join FiscalDates AS FD
             ON T.TranDate = FD.FiscalDate
    ),

    AccountSummaryByMonth As
    ( Select ASM.FiscalMonthStartDate,
             Sum(AD.Amount) as TotalSubmissionAmount
      From   FiscalMonths as ASM
           Inner Join AccountData as AD
             ON ASM.FiscalMonthStartDate = AD.SubmissionMonthStartDate
      Group By
             ASM.FiscalMonthStartDate
    ),

    TranSummaryByMonth As
    ( Select STM.SubmissionMonthStartDate,
             STM.TransMonthStartDate,
             STM.TranSummaryMonthNum,
             Sum(TD.TranAmount) as TotalTranAmount
      From   SubmissionAndTranMonths as STM
           Inner Join TranData as TD
             ON STM.SubmissionMonthStartDate = TD.SubmissionMonthStartDate
                AND STM.TransMonthStartDate = TD.TranMonthStartDate
      Group By
             STM.SubmissionMonthStartDate,
             STM.TransMonthStartDate,
             STM.TranSummaryMonthNum
    )

--#Inspect 1
--Select * From SubmissionAndTranMonths
--OPTION (MAXRECURSION 0)

--#Inspect 1 Results
--SubmissionMonthStartDate TransMonthStartDate TranSummaryMonthNum
--2012-01-01               2012-01-01          0
--2012-01-01               2012-02-01          1
--2012-01-01               2012-03-01          2
--2012-01-01               2012-04-01          3
--2012-02-01               2012-02-01          1
--2012-02-01               2012-03-01          2
--2012-02-01               2012-04-01          3
--2012-03-01               2012-03-01          2
--2012-03-01               2012-04-01          3
--2012-04-01               2012-04-01          3

--#Inspect 2
--Select * From AccountSummaryByMonth
--OPTION (MAXRECURSION 0)

--#Inspect 2 Results
--FiscalMonthStartDate TotalSubmissionAmount
--2012-01-01           2099.00
--2012-02-01           350.00
--2012-03-01           685.00

--#Inspect 3
--Select * From TranSummaryByMonth
--OPTION (MAXRECURSION 0)

--#Inspect 3 Results
--SubmissionMonthStartDate TransMonthStartDate TranSummaryMonthNum TotalTranAmount
--2012-01-01               2012-01-01          0                   300.00
--2012-01-01               2012-02-01          1                   300.00
--2012-01-01               2012-03-01          2                   300.00
--2012-02-01               2012-02-01          1                   325.00
--2012-02-01               2012-04-01          3                   25.00
--2012-03-01               2012-03-01          2                   656.00
--2012-03-01               2012-04-01          3                   15.00

Select STM.SubmissionMonthStartDate,
       ASM.TotalSubmissionAmount,
       STM.TransMonthStartDate,
       STM.TranSummaryMonthNum,
       TSM.TotalTranAmount
From   SubmissionAndTranMonths as STM
     Inner Join AccountSummaryByMonth as ASM
       ON STM.SubmissionMonthStartDate = ASM.FiscalMonthStartDate
     Left Join TranSummaryByMonth AS TSM
       ON STM.SubmissionMonthStartDate = TSM.SubmissionMonthStartDate
          AND STM.TransMonthStartDate = TSM.TransMonthStartDate
Order By STM.SubmissionMonthStartDate, STM.TranSummaryMonthNum
OPTION (MAXRECURSION 0)

--#Results
--SubmissionMonthStartDate TotalSubmissionAmount TransMonthStartDate TranSummaryMonthNum TotalTranAmount
--2012-01-01               2099.00               2012-01-01           0                  300.00
--2012-01-01               2099.00               2012-02-01           1                  300.00
--2012-01-01               2099.00               2012-03-01           2                  300.00
--2012-01-01               2099.00               2012-04-01           3                  NULL
--2012-02-01               350.00                2012-02-01           1                  325.00
--2012-02-01               350.00                2012-03-01           2                  NULL
--2012-02-01               350.00                2012-04-01           3                  25.00
--2012-03-01               685.00                2012-03-01           2                  656.00
--2012-03-01               685.00                2012-04-01           3                  15.00
于 2012-07-02T15:24:01.117 に答える
1

次のクエリは、必要なものをほとんど返します。to 操作は個別に行う必要があります。結果を結合するだけです:

 select a.yyyymm, a.Amount,
        t201201, t201202, t201203, t201204
 from (select LEFT(convert(varchar(255), a.submissiondate, 121), 7) as yyyymm,
              SUM(a.Amount) as amount
       from Accounts a
       group by  LEFT(convert(varchar(255), a.submissiondate, 121), 7) 
      ) a left outer join
      (select LEFT(convert(varchar(255), a.submissiondate, 121), 7) as yyyymm,
              sum(case when trans_yyyymm = '2012-01' then tranamount end) as t201201,
              sum(case when trans_yyyymm = '2012-02' then tranamount end) as t201202,
              sum(case when trans_yyyymm = '2012-03' then tranamount end) as t201203,
              sum(case when trans_yyyymm = '2012-04' then tranamount end) as t201204
       from Accounts a join
            (select t.*, LEFT(convert(varchar(255), t.trandate, 121), 7) as trans_yyyymm
             from trans t
            ) t
            on a.accountid = t.accountid
       group by LEFT(convert(varchar(255), a.submissiondate, 121), 7)
      ) t
      on a.yyyymm = t.yyyymm
 order by 1

2 つのセルに 0.00 がある NULL を取得しています。

于 2012-06-26T21:34:10.770 に答える
1

次のクエリは、独自の回答で最終的なクエリの結果を正確に複製しますが、CPU の 1/30 (またはそれ以上) しか使用せず、さらにはるかに単純です。

時間とエネルギーがあれば、さらに改善できると確信していAccountsます。しかし、いずれにせよ、これは大きな改善であり、非常に大きな結果セットに対しても非常にうまく機能するはずです.

SqlFiddle を参照してください。

WITH L0 AS (SELECT 1 N UNION ALL SELECT 1),
L1 AS (SELECT 1 N FROM L0, L0 B),
L2 AS (SELECT 1 N FROM L1, L1 B),
L3 AS (SELECT 1 N FROM L2, L2 B),
L4 AS (SELECT 1 N FROM L3, L2 B),
Nums AS (SELECT N = Row_Number() OVER (ORDER BY (SELECT 1)) FROM L4),
Anchor AS (
   SELECT MinDate = DateAdd(month, DateDiff(month, '20000101', Min(SubmissionDate)), '20000101')
   FROM dbo.Accounts
),
MNums AS (
   SELECT N
   FROM Nums
   WHERE
      N <= DateDiff(month,
         (SELECT MinDate FROM Anchor),
         (SELECT Max(TranDate) FROM dbo.Trans)
      ) + 1
),
A AS (
   SELECT
      AM.AccountMo,
      Amount = Sum(A.Amount)
   FROM
      dbo.Accounts A
      CROSS APPLY (
         SELECT DateAdd(month, DateDiff(month, '20000101', A.SubmissionDate), '20000101')
      ) AM (AccountMo)
   GROUP BY
      AM.AccountMo
), T AS (
   SELECT
      AM.AccountMo,
      TM.TranMo,
      TotalTranAmount = Sum(T.TranAmount)
   FROM
      dbo.Accounts A
      CROSS APPLY (
         SELECT DateAdd(month, DateDiff(month, '20000101', A.SubmissionDate), '20000101')
      ) AM (AccountMo)
      INNER JOIN dbo.Trans  T
         ON A.AccountID = T.AccountID
      CROSS APPLY (
         SELECT DateAdd(month, DateDiff(month, '20000101', T.TranDate), '20000101')
      ) TM (TranMo)
   GROUP BY
      AM.AccountMo,
      TM.TranMo
)
SELECT
   SubmissionStartMonth = A.AccountMo,
   TotalSubmissionAmount = A.Amount,
   M.TransMonth,
   TransMonthNum = N.N - 1,
   T.TotalTranAmount
FROM
   A
   INNER JOIN MNums N
      ON N.N >= DateDiff(month, (SELECT MinDate FROM Anchor), A.AccountMo) + 1
   CROSS APPLY (
      SELECT TransMonth = DateAdd(month, N.N - 1, (SELECT MinDate FROM Anchor))
   ) M
   LEFT JOIN T
      ON A.AccountMo = T.AccountMo
      AND M.TransMonth = T.TranMo
ORDER BY
   A.AccountMo,
   M.TransMonth;
于 2012-12-30T07:33:15.047 に答える