我已经查看了之前的几个问题,但我正在努力将解决方案应用于我的具体示例。
我在组合查询 1 和查询 2 时遇到问题。
我的查询最初返回(以及其他详细信息)当月所有成员/用户的值“ SpentTotal ”和“ UnderSpent ” 。
我的问题是在这个原始查询中添加了两列额外的列,这将只返回这两列(已花费和超支),但对于前几个月的数据
原始查询 #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,查询 PREVIOUS 月份的所有列,但我只需要两个(已花费和超额),从上面添加到查询中,加入客户编号:
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
非常感谢所有愿意提供帮助和时间的人。
干杯!
- 阿杰
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,