2

以前のいくつかの質問に目を通しましたが、特定の例に解決策を適用するのに苦労しています。

クエリ 1 とクエリ 2 の組み合わせに問題があります。

私のクエリは、最初に (その他の詳細とともに) 今月のすべてのメンバー/ユーザーの値 " SpentTotal " と " UnderSpent " を返しました。

私の問題は、この元のクエリに 2 つの追加の列を追加することで、これら 2 つの列 (Spent と Overspent) のみを返しますが、の月のデータについては

元のクエリ #1:

set @BPlanKey = '##CURRENTMONTH##'
EXECUTE @RC = Minimum_UpdateForPeriod @BPlanKey

SELECT cm.clubaccountnumber, bp.Description , msh.PeriodMinObligation,  msh.SpentTotal, msh.UnderSpent, msh.OverSpent, msh.BilledDate, msh.PeriodStartDate, msh.PeriodEndDate, msh.OverSpent 
FROM MinimumSpendHistory msh 
     INNER JOIN BillPlanMinimums bpm ON msh.BillingPeriodKey = @BPlanKey and bpm.BillPlanMinimumKey = msh.BillPlanMinimumKey 
     INNER JOIN BillPlans bp ON bp.BillPlanKey = bpm.BillPlanKey 
     INNER JOIN ClubMembers cm ON cm.parentmemberkey is null and cm.ClubMemberKey = msh.ClubMemberKey 
order by cm.clubaccountnumber asc, msh.BilledDate asc

クエリ #2、前の月のすべての列のクエリですが、上記のクエリに追加され、顧客番号に結合された 2 つ (使用済みと超過使用) だけが必要です。

set @BPlanKeyLastMo = '##PREVMONTH##'
EXECUTE @RCLastMo = Minimum_UpdateForPeriod @BPlanKeyLastMo

SELECT cm.clubaccountnumber, bp.Description , msh.PeriodMinObligation, msh.SpentTotal, msh.UnderSpent, msh.OverSpent, msh.BilledDate, msh.PeriodStartDate, msh.PeriodEndDate, msh.OverSpent 
FROM MinimumSpendHistory msh 
     INNER JOIN BillPlanMinimums bpm ON msh.BillingPeriodKey = @BPlanKeyLastMo and bpm.BillPlanMinimumKey = msh.BillPlanMinimumKey 
     INNER JOIN BillPlans bp ON bp.BillPlanKey = bpm.BillPlanKey 
     INNER JOIN ClubMembers cm ON cm.parentmemberkey is null and cm.ClubMemberKey = msh.ClubMemberKey 
order by cm.clubaccountnumber asc, msh.BilledDate asc

喜んで助けと時間を貸してくれるすべての人に感謝します。

乾杯!

  • AJ

CREATE TABLE MinimumSpendHistory(
        [MinimumSpendHistoryKey] [uniqueidentifier] NOT NULL,
        [BillPlanMinimumKey] [uniqueidentifier] NOT NULL,
        [ClubMemberKey] [uniqueidentifier] NOT NULL,
        [BillingPeriodKey] [uniqueidentifier] NOT NULL,
        [PeriodStartDate] [datetime] NOT NULL,
        [PeriodEndDate] [datetime] NOT NULL,
        [PeriodMinObligation] [money] NOT NULL,
        [SpentTotal] [money] NOT NULL,
        [CurrentSpent] [money] NOT NULL,
        [OverSpent] [money] NULL,
        [UnderSpent] [money] NULL,
        [BilledAmount] [money] NOT NULL,
        [BilledDate] [datetime] NOT NULL,
        [PriorPeriodMinimum] [money] NULL,
        [IsCommitted] [bit] NOT NULL,
        [IsCalculated] [bit] NOT NULL,
        [BillPeriodMinimumKey] [uniqueidentifier] NOT NULL,
        [CarryForwardCounter] [smallint] NULL,
        [YTDSpent] [money] NOT NULL,
        [PeriodToAccumulateCounter] [int] NULL,
        [StartDate] [datetime] NOT NULL,
4

2 に答える 2

1
SELECT cm.clubaccountnumber, bp.Description , msh.PeriodMinObligation,  msh.SpentTotal, msh.UnderSpent, msh.OverSpent, msh.BilledDate, msh.PeriodStartDate, msh.PeriodEndDate, 
mshp.SpentTotal, mshp.UnderSpent
FROM MinimumSpendHistory msh 
     INNER JOIN BillPlanMinimums bpm ON msh.BillingPeriodKey = @BPlanKey 
                                    and bpm.BillPlanMinimumKey = msh.BillPlanMinimumKey 
     INNER JOIN BillPlans bp ON bp.BillPlanKey = bpm.BillPlanKey 
     INNER JOIN ClubMembers cm ON cm.parentmemberkey is null 
                              and cm.ClubMemberKey = msh.ClubMemberKey
     LEFT OUTER JOIN ClubMembers cmp on cm.ClubMemberKey = msh.ClubMemberKey
     LEFT OUTER JOIN MinimumSpendHistory mshp on cmp.ClubMemberKey = mshp.ClubMemberKey
                              and mshp mshp.BillingPeriodKey = @BPlanKeyLastMo
order by cm.clubaccountnumber asc, msh.BilledDate asc
于 2012-11-16T22:28:26.410 に答える
0

ほぼ間違いなくもっと簡単な方法がありますが、これはおそらくうまくいくでしょう:

ClubAccountNumber が ClubMembers で一意でない場合は、このテーブルの主キーも選択し、代わりにその 2 つの副選択を結合する必要があります。

Select
  a.ClubAccountNumber, 
  a.Description,
  a.PeriodMinObligation,
  a.SpentTotal,
  a.UnderSpent, 
  a.OverSpent, 
  a.BilledDate, 
  a.PeriodStartDate, 
  a.PeriodEndDate, 
  a.OverSpent, -- this second instance of overspent is from the question...
  b.SpentTotal As LastMonthSpentTotal, 
  b.UnderSpent As LastMonthUnderspent
From (
  Select
    cm.ClubAccountNumber, 
    bp.Description,
    msh.PeriodMinObligation,
    msh.SpentTotal,
    msh.UnderSpent, 
    msh.OverSpent, 
    msh.BilledDate, 
    msh.PeriodStartDate, 
    msh.PeriodEndDate
  From
    MinimumSpendHistory msh 
      Inner Join
    BillPlanMinimums bpm 
      On msh.BillingPeriodKey = @BPlanKey And bpm.BillPlanMinimumKey = msh.BillPlanMinimumKey 
      Inner Join
    BillPlans bp 
      On bp.BillPlanKey = bpm.BillPlanKey
      Inner Join
    ClubMembers cm 
      On cm.ParentMemberKey Is Null And cm.ClubMemberKey = msh.ClubMemberKey 
  ) a Left Outer Join (
  Select 
    cm.ClubAccountNumber,
    msh.SpentTotal,
    msh.UnderSpent
  From
    MinimumSpendHistory msh 
      Inner Join
    BillPlanMinimums bpm 
      On msh.BillingPeriodKey = @BPlanKeyLastMo and bpm.BillPlanMinimumKey = msh.BillPlanMinimumKey
      Inner Join
    BillPlans bp 
      On bp.BillPlanKey = bpm.BillPlanKey 
      Inner Join
    ClubMembers cm 
      On cm.ParentMemberKey Is Null And cm.ClubMemberKey = msh.ClubMemberKey 
  ) b On a.ClubAccountNumber = b.ClubAccountNumber -- this should probably join on cm.clubmemberkey, but we are guessing
Order By
  a.ClubAccountNumber, 
  a.BilledDate
于 2012-11-16T22:15:01.860 に答える