我已經瀏覽了幾個以前的問題,但我正在努力將解決方案應用於我的具體示例。在同一張桌面上組合兩個查詢
我有合併查詢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,
表的定義將是有益的。 – Laurence
我在代碼中看不到任何子查詢。這段代碼本身就是子查詢嗎? –
表定義包括主鍵和外鍵等。 – Laurence