在同一个表上组合两个查询

时间:2012-11-16 21:50:26

标签: .net sql vb.net sql-server-2008 tsql

我已经查看了之前的几个问题,但我很难将解决方案应用到我的具体示例中。

我在查询1和查询2时遇到问题。

我的查询最初返回(在其他详细信息中)当前的所有成员/用户的值“ SpentTotal ”和“ UnderSpent ”一个月。

我的问题是在这个原始的quert中添加了两个额外的列,它们将返回JUST这两列(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,查询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

非常感谢任何愿意提供帮助和时间的人。

干杯!

  • 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,

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

几乎可以肯定有更简单的方法,但这可能有用:

如果ClubAccountNumber在ClubMembers上不是唯一的,您还需要选择此表的主键并加入两个子选项。

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
相关问题