2012-11-16 77 views
2

我已經瀏覽了幾個以前的問題,但我正在努力將解決方案應用於我的具體示例。在同一張桌面上組合兩個查詢

我有合併查詢1和查詢的麻煩2.

我原先的查詢返回(在其他細節)值「SpentTotal」和「UnderSpent」爲所有成員/用戶對於當前個月。

我的問題已經增加了兩個額外的列到這個原quert將只返回這兩列(花和超支),但對於以前個月數據

原始查詢#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,對前一個月的所有列的查詢,但我只需要兩(花和花以上),加入到上述查詢,加入了對客戶編號:

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, 
+0

表的定義將是有益的。 – Laurence

+1

我在代碼中看不到任何子查詢。這段代碼本身就是子查詢嗎? –

+0

表定義包括主鍵和外鍵等。 – Laurence

回答

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 
+0

我在第一個FROM附近得到了一個「不正確的語法」。當我將鼠標懸停在「a」中)左外部聯接(「它讀取 - 'OverSpent'列被多次指定爲 - 我認爲它與頂部的兩個超級腳本的實例有關,但它不是。 – UserAyeJay

+0

@ user1830856沒有注意到你已經選擇了相同的值兩次,修復了這個問題。 – Laurence

+0

仍然在第一次接收到「接近錯誤的語法」,因此非常感謝您的幫助 – UserAyeJay

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 
+0

如果你想沿着那條路線走,那麼每個MinimumSpendHistory行可以有不同的BillPlanMinimums和BillPlan行。另外可能沒有任何前幾個月的數據。 – Laurence

+0

我看着他在字面上加入了客戶號碼。 – Paparazzi

+0

我把它全部寫出來再次加入其他表格,現在我認爲你是對的。 – Laurence

相關問題