2015-11-09 52 views
-5

我嘗試了很多。但卻無法預期result.I有一個查詢這是給我以下類似的結果如何在sql查詢中將列更改爲行

sno name   budget  NameTrim  TravelCost  Expense 
    3 Local Expense 12  Expense  NULL   24 
    4 Curent Expense 12  Expense  NULL   24 
    1 Local Travel 123  Travel  246    NULL 
    2 Global Travel 123  Travel  246    NULL 

,但數據應該是以下格式

Travel 
    1 Local Travel  123 
    2 Global Travel 123 
      subtotal = 246 

Expense 

    3 Local Expenses 12 
    4 Current Expe  12 
     Subtotal =  24 

我使用下面的查詢

SELECT concat([Segment_0], '-' , [Segment_1], '-' ,[Segment_2]) , T2.[AcctName],T5.[Name] , 
    sum(T0.[Debit]) as Actual , sum(T3.[DebLTotal]) as Budget , 
    (case 
when T2.[AcctName] like '%Travel%' then right(T2.[AcctName], 30) 
when T2.[AcctName] like '%Expense%' then right(T2.[AcctName],30) 
end) as NameTrim 
from JDT1 T0 INNER JOIN OJDT T1 ON T0.[TransId] = T1.[TransId] INNER JOIN OACT T2 ON T0.[Account] = T2.[AcctCode], 
OBGT T3 INNER JOIN BGT1 T4 ON T3.[AbsId] = T4.[BudgId] INNER JOIN OBGS T5 ON T3.[Instance] = T5.[AbsId],OASC T6 
Where T5.[Name] = 'Main Budget 2015' 
Group By concat([Segment_0], '-' , [Segment_1], '-' ,[Segment_2]), T2.[AcctName],T5.[Name] 

我在這裏發佈給定查詢的表結構請幫助我。

USE [RCTP_NGO] 
GO 
/****** Object: Table [dbo].[BGT1] Script Date: 09/Nov/2015 12:35:30 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE TABLE [dbo].[BGT1](
    [BudgId] [int] NOT NULL, 
    [Line_ID] [int] NOT NULL, 
    [DebLTotal] [numeric](19, 6) NULL, 
    [CredLTotal] [numeric](19, 6) NULL, 
    [DebSTotal] [numeric](19, 6) NULL, 
    [CredSTotal] [numeric](19, 6) NULL, 
    [DebRLTotal] [numeric](19, 6) NULL, 
    [CrdRLTotal] [numeric](19, 6) NULL, 
    [DebRSTotal] [numeric](19, 6) NULL, 
    [CrdRSTotal] [numeric](19, 6) NULL, 
    [FtrIDRLSum] [numeric](19, 6) NULL, 
    [FtrIDRSSum] [numeric](19, 6) NULL, 
    [FtrICRLSum] [numeric](19, 6) NULL, 
    [FtrICRSSum] [numeric](19, 6) NULL, 
    [FtrODRLSum] [numeric](19, 6) NULL, 
    [FtrODRSSum] [numeric](19, 6) NULL, 
    [FtrOCRLSum] [numeric](19, 6) NULL, 
    [FtrOCRSSum] [numeric](19, 6) NULL, 
    [MonthPrcnt] [numeric](19, 6) NULL, 
    [LineMemo] [nvarchar](50) NULL, 
    [Instance] [int] NULL, 
    [AcctCode] [nvarchar](15) NULL, 
    [UserSign] [smallint] NULL, 
CONSTRAINT [BGT1_PRIMARY] PRIMARY KEY CLUSTERED 
(
    [BudgId] ASC, 
    [Line_ID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 
/****** Object: Table [dbo].[JDT1] Script Date: 09/Nov/2015 12:35:30 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
SET ANSI_PADDING ON 
GO 
CREATE TABLE [dbo].[JDT1](
    [TransId] [int] NOT NULL, 
    [Line_ID] [int] NOT NULL, 
    [Account] [nvarchar](15) NULL, 
    [Debit] [numeric](19, 6) NULL, 
    [Credit] [numeric](19, 6) NULL, 
    [SYSCred] [numeric](19, 6) NULL, 
    [SYSDeb] [numeric](19, 6) NULL, 
    [FCDebit] [numeric](19, 6) NULL, 
    [FCCredit] [numeric](19, 6) NULL, 
    [FCCurrency] [nvarchar](3) NULL, 
    [DueDate] [datetime] NULL, 
    [SourceID] [int] NULL, 
    [SourceLine] [smallint] NULL, 
    [ShortName] [nvarchar](15) NULL, 
    [IntrnMatch] [int] NULL, 
    [ExtrMatch] [int] NULL, 
    [ContraAct] [nvarchar](15) NULL, 
    [LineMemo] [nvarchar](50) NULL, 
    [Ref3Line] [nvarchar](27) NULL, 
    [TransType] [nvarchar](20) NULL, 
    [RefDate] [datetime] NULL, 
    [Ref2Date] [datetime] NULL, 
    [Ref1] [nvarchar](100) NULL, 
    [Ref2] [nvarchar](100) NULL, 
    [CreatedBy] [int] NULL, 
    [BaseRef] [nvarchar](11) NULL, 
    [Project] [nvarchar](20) NULL, 
    [TransCode] [nvarchar](4) NULL, 
    [ProfitCode] [nvarchar](8) NULL, 
    [TaxDate] [datetime] NULL, 
    [SystemRate] [numeric](19, 6) NULL, 
    [MthDate] [datetime] NULL, 
    [ToMthSum] [numeric](19, 6) NULL, 
    [UserSign] [smallint] NULL, 
    [BatchNum] [int] NULL, 
    [FinncPriod] [int] NULL, 
    [RelTransId] [int] NULL, 
    [RelLineID] [int] NULL, 
    [RelType] [char](1) NULL, 
    [LogInstanc] [int] NULL, 
    [VatGroup] [nvarchar](8) NULL, 
    [BaseSum] [numeric](19, 6) NULL, 
    [VatRate] [numeric](19, 6) NULL, 
    [Indicator] [nvarchar](2) NULL, 
    [AdjTran] [char](1) NULL, 
    [RevSource] [char](1) NULL, 
    [ObjType] [nvarchar](20) NULL, 
    [VatDate] [datetime] NULL, 
    [PaymentRef] [nvarchar](27) NULL, 
    [SYSBaseSum] [numeric](19, 6) NULL, 
    [MultMatch] [int] NULL, 
    [VatLine] [char](1) NULL, 
    [VatAmount] [numeric](19, 6) NULL, 
    [SYSVatSum] [numeric](19, 6) NULL, 
    [Closed] [char](1) NULL, 
    [GrossValue] [numeric](19, 6) NULL, 
    [CheckAbs] [int] NULL, 
    [LineType] [int] NULL, 
    [DebCred] [char](1) NULL, 
    [SequenceNr] [int] NULL, 
    [StornoAcc] [nvarchar](15) NULL, 
    [BalDueDeb] [numeric](19, 6) NULL, 
    [BalDueCred] [numeric](19, 6) NULL, 
    [BalFcDeb] [numeric](19, 6) NULL, 
    [BalFcCred] [numeric](19, 6) NULL, 
    [BalScDeb] [numeric](19, 6) NULL, 
    [BalScCred] [numeric](19, 6) NULL, 
    [IsNet] [char](1) NULL, 
    [DunWizBlck] [char](1) NULL, 
    [DunnLevel] [int] NULL, 
    [DunDate] [datetime] NULL, 
    [TaxType] [smallint] NULL, 
    [TaxPostAcc] [char](1) NULL, 
    [StaCode] [nvarchar](8) NULL, 
    [StaType] [int] NULL, 
    [TaxCode] [nvarchar](8) NULL, 
    [ValidFrom] [datetime] NULL, 
    [GrossValFc] [numeric](19, 6) NULL, 
    [LvlUpdDate] [datetime] NULL, 
    [OcrCode2] [nvarchar](8) NULL, 
    [OcrCode3] [nvarchar](8) NULL, 
    [OcrCode4] [nvarchar](8) NULL, 
    [OcrCode5] [nvarchar](8) NULL, 
    [MIEntry] [int] NULL, 
    [MIVEntry] [int] NULL, 
    [ClsInTP] [int] NULL, 
    [CenVatCom] [int] NULL, 
    [MatType] [int] NULL, 
    [PstngType] [int] NULL, 
    [ValidFrom2] [datetime] NULL, 
    [ValidFrom3] [datetime] NULL, 
    [ValidFrom4] [datetime] NULL, 
    [ValidFrom5] [datetime] NULL, 
    [Location] [int] NULL, 
    [WTaxCode] [nvarchar](4) NULL, 
    [EquVatRate] [numeric](19, 6) NULL, 
    [EquVatSum] [numeric](19, 6) NULL, 
    [SYSEquSum] [numeric](19, 6) NULL, 
    [TotalVat] [numeric](19, 6) NULL, 
    [SYSTVat] [numeric](19, 6) NULL, 
    [WTLiable] [char](1) NULL, 
    [WTLine] [char](1) NULL, 
    [WTApplied] [numeric](19, 6) NULL, 
    [WTAppliedS] [numeric](19, 6) NULL, 
    [WTAppliedF] [numeric](19, 6) NULL, 
    [WTSum] [numeric](19, 6) NULL, 
    [WTSumFC] [numeric](19, 6) NULL, 
    [WTSumSC] [numeric](19, 6) NULL, 
    [PayBlock] [char](1) NULL, 
    [PayBlckRef] [int] NULL, 
    [LicTradNum] [nvarchar](32) NULL, 
    [InterimTyp] [int] NULL, 
    [DprId] [int] NULL, 
    [MatchRef] [nvarchar](20) NULL, 
    [Ordered] [char](1) NULL, 
    [CUP] [int] NULL, 
    [CIG] [int] NULL, 
    [BPLId] [int] NULL, 
    [BPLName] [nvarchar](100) NULL, 
    [VatRegNum] [nvarchar](32) NULL, 
    [SLEDGERF] [char](1) NULL, 
CONSTRAINT [JDT1_PRIMARY] PRIMARY KEY CLUSTERED 
(
    [TransId] ASC, 
    [Line_ID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 
SET ANSI_PADDING OFF 
GO 
/****** Object: Table [dbo].[OACT] Script Date: 09/Nov/2015 12:35:30 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
SET ANSI_PADDING ON 
GO 
CREATE TABLE [dbo].[OACT](
    [AcctCode] [nvarchar](15) NOT NULL, 
    [AcctName] [nvarchar](100) NULL, 
    [CurrTotal] [numeric](19, 6) NULL, 
    [EndTotal] [numeric](19, 6) NULL, 
    [Finanse] [char](1) NULL, 
    [Groups] [nvarchar](8) NULL, 
    [Budget] [char](1) NULL, 
    [Frozen] [char](1) NULL, 
    [Free_2] [char](1) NULL, 
    [Postable] [char](1) NULL, 
    [Fixed] [char](1) NULL, 
    [Levels] [smallint] NULL, 
    [ExportCode] [nvarchar](10) NULL, 
    [GrpLine] [int] NULL, 
    [FatherNum] [nvarchar](15) NULL, 
    [AccntntCod] [nvarchar](15) NULL, 
    [CashBox] [char](1) NULL, 
    [GroupMask] [smallint] NULL, 
    [RateTrans] [char](1) NULL, 
    [TaxIncome] [char](1) NULL, 
    [ExmIncome] [char](1) NULL, 
    [ExtrMatch] [int] NULL, 
    [IntrMatch] [int] NULL, 
    [ActType] [char](1) NULL, 
    [Transfered] [char](1) NULL, 
    [BlncTrnsfr] [char](1) NULL, 
    [OverType] [char](1) NULL, 
    [OverCode] [nvarchar](8) NULL, 
    [SysMatch] [int] NULL, 
    [PrevYear] [char](1) NULL, 
    [ActCurr] [nvarchar](3) NULL, 
    [RateDifAct] [nvarchar](15) NULL, 
    [SysTotal] [numeric](19, 6) NULL, 
    [FcTotal] [numeric](19, 6) NULL, 
    [Protected] [char](1) NULL, 
    [RealAcct] [char](1) NULL, 
    [Advance] [char](1) NULL, 
    [CreateDate] [datetime] NULL, 
    [UpdateDate] [datetime] NULL, 
    [FrgnName] [nvarchar](100) NULL, 
    [Details] [nvarchar](254) NULL, 
    [ExtraSum] [numeric](19, 6) NULL, 
    [Project] [nvarchar](20) NULL, 
    [RevalMatch] [char](1) NULL, 
    [DataSource] [char](1) NULL, 
    [LocMth] [char](1) NULL, 
    [MTHCounter] [int] NULL, 
    [BNKCounter] [int] NULL, 
    [UserSign] [smallint] NULL, 
    [LocManTran] [char](1) NULL, 
    [LogInstanc] [int] NULL, 
    [ObjType] [nvarchar](20) NULL, 
    [ValidFor] [char](1) NULL, 
    [ValidFrom] [datetime] NULL, 
    [ValidTo] [datetime] NULL, 
    [ValidComm] [nvarchar](30) NULL, 
    [FrozenFor] [char](1) NULL, 
    [FrozenFrom] [datetime] NULL, 
    [FrozenTo] [datetime] NULL, 
    [FrozenComm] [nvarchar](30) NULL, 
    [Counter] [int] NULL, 
    [Segment_0] [nvarchar](20) NULL, 
    [Segment_1] [nvarchar](20) NULL, 
    [Segment_2] [nvarchar](20) NULL, 
    [Segment_3] [nvarchar](20) NULL, 
    [Segment_4] [nvarchar](20) NULL, 
    [Segment_5] [nvarchar](20) NULL, 
    [Segment_6] [nvarchar](20) NULL, 
    [Segment_7] [nvarchar](20) NULL, 
    [Segment_8] [nvarchar](20) NULL, 
    [Segment_9] [nvarchar](20) NULL, 
    [FormatCode] [nvarchar](210) NULL, 
    [CfwRlvnt] [char](1) NULL, 
    [ExchRate] [char](1) NULL, 
    [RevalAcct] [nvarchar](15) NULL, 
    [LastRevBal] [numeric](19, 6) NULL, 
    [LastRevDat] [datetime] NULL, 
    [DfltVat] [nvarchar](8) NULL, 
    [VatChange] [char](1) NULL, 
    [Category] [int] NULL, 
    [TransCode] [nvarchar](4) NULL, 
    [OverCode5] [nvarchar](8) NULL, 
    [OverCode2] [nvarchar](8) NULL, 
    [OverCode3] [nvarchar](8) NULL, 
    [OverCode4] [nvarchar](8) NULL, 
    [DfltTax] [nvarchar](8) NULL, 
    [TaxPostAcc] [char](1) NULL, 
    [AcctStrLe] [nvarchar](2) NULL, 
    [MeaUnit] [nvarchar](10) NULL, 
    [BalDirect] [nvarchar](4) NULL, 
    [UserSign2] [smallint] NULL, 
    [PlngLevel] [nvarchar](2) NULL, 
    [MultiLink] [char](1) NULL, 
    [PrjRelvnt] [char](1) NULL, 
    [Dim1Relvnt] [char](1) NULL, 
    [Dim2Relvnt] [char](1) NULL, 
    [Dim3Relvnt] [char](1) NULL, 
    [Dim4Relvnt] [char](1) NULL, 
    [Dim5Relvnt] [char](1) NULL, 
    [AccrualTyp] [char](1) NULL, 
    [DatevAcct] [int] NULL, 
    [DatevAutoA] [char](1) NULL, 
    [DatevFirst] [char](1) NULL, 
    [SnapShotId] [int] NULL, 
    [PCN874Rpt] [char](1) NULL, 
    [SCAdjust] [char](1) NULL, 
    [BPLId] [int] NULL, 
    [BPLName] [nvarchar](100) NULL, 
    [SubLedgerN] [nvarchar](60) NULL, 
    [VATRegNum] [nvarchar](32) NULL, 
    [ActId] [nvarchar](210) NOT NULL, 
    [ClosingAcc] [nvarchar](15) NULL, 
    [PurpCode] [nvarchar](2) NULL, 
    [RefCode] [nvarchar](30) NULL, 
    [U_COR_FIN_Cls] [nvarchar](32) NULL, 
CONSTRAINT [OACT_PRIMARY] PRIMARY KEY CLUSTERED 
(
    [AcctCode] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 
SET ANSI_PADDING OFF 
GO 
/****** Object: Table [dbo].[OBGT] Script Date: 09/Nov/2015 12:35:30 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
CREATE TABLE [dbo].[OBGT](
    [AbsId] [int] NOT NULL, 
    [AcctCode] [nvarchar](15) NOT NULL, 
    [BgdCode] [int] NULL, 
    [FatherCode] [nvarchar](15) NULL, 
    [FthrPrcnt] [numeric](19, 6) NULL, 
    [DebLTotal] [numeric](19, 6) NULL, 
    [CredLTotal] [numeric](19, 6) NULL, 
    [DebSTotal] [numeric](19, 6) NULL, 
    [CredSTotal] [numeric](19, 6) NULL, 
    [DebRLTotal] [numeric](19, 6) NULL, 
    [CrdRLTotal] [numeric](19, 6) NULL, 
    [DebRSTotal] [numeric](19, 6) NULL, 
    [CrdRSTotal] [numeric](19, 6) NULL, 
    [FtrIDRLSum] [numeric](19, 6) NULL, 
    [FtrIDRSSum] [numeric](19, 6) NULL, 
    [FtrICRLSum] [numeric](19, 6) NULL, 
    [FtrICRSSum] [numeric](19, 6) NULL, 
    [FtrODRLSum] [numeric](19, 6) NULL, 
    [FtrOCRLSum] [numeric](19, 6) NULL, 
    [FtrODRSSum] [numeric](19, 6) NULL, 
    [FtrOCRSSum] [numeric](19, 6) NULL, 
    [FinancYear] [datetime] NOT NULL, 
    [Instance] [int] NOT NULL, 
    [UserSign] [smallint] NULL, 
    [SCNCounter] [smallint] NULL, 
CONSTRAINT [OBGT_PRIMARY] PRIMARY KEY CLUSTERED 
(
    [AbsId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 
/****** Object: Table [dbo].[OJDT] Script Date: 09/Nov/2015 12:35:30 PM ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
SET ANSI_PADDING ON 
GO 
CREATE TABLE [dbo].[OJDT](
    [BatchNum] [int] NULL, 
    [TransId] [int] NOT NULL, 
    [BtfStatus] [char](1) NULL, 
    [TransType] [nvarchar](20) NULL, 
    [BaseRef] [nvarchar](11) NULL, 
    [RefDate] [datetime] NULL, 
    [Memo] [nvarchar](50) NULL, 
    [Ref1] [nvarchar](100) NULL, 
    [Ref2] [nvarchar](100) NULL, 
    [CreatedBy] [int] NULL, 
    [LocTotal] [numeric](19, 6) NULL, 
    [FcTotal] [numeric](19, 6) NULL, 
    [SysTotal] [numeric](19, 6) NULL, 
    [TransCode] [nvarchar](4) NULL, 
    [OrignCurr] [nvarchar](3) NULL, 
    [TransRate] [numeric](19, 6) NULL, 
    [BtfLine] [int] NULL, 
    [TransCurr] [nvarchar](3) NULL, 
    [Project] [nvarchar](20) NULL, 
    [DueDate] [datetime] NULL, 
    [TaxDate] [datetime] NULL, 
    [PCAddition] [char](1) NULL, 
    [FinncPriod] [int] NULL, 
    [DataSource] [char](1) NULL, 
    [UpdateDate] [datetime] NULL, 
    [CreateDate] [datetime] NULL, 
    [UserSign] [smallint] NULL, 
    [UserSign2] [smallint] NULL, 
    [RefndRprt] [char](1) NULL, 
    [LogInstanc] [int] NULL, 
    [ObjType] [nvarchar](20) NULL, 
    [Indicator] [nvarchar](2) NULL, 
    [AdjTran] [char](1) NULL, 
    [RevSource] [char](1) NULL, 
    [StornoDate] [datetime] NULL, 
    [StornoToTr] [int] NULL, 
    [AutoStorno] [char](1) NULL, 
    [Corisptivi] [char](1) NULL, 
    [VatDate] [datetime] NULL, 
    [StampTax] [char](1) NULL, 
    [Series] [smallint] NOT NULL, 
    [Number] [int] NOT NULL, 
    [AutoVAT] [char](1) NULL, 
    [DocSeries] [smallint] NULL, 
    [FolioPref] [nvarchar](4) NULL, 
    [FolioNum] [int] NULL, 
    [CreateTime] [smallint] NULL, 
    [BlockDunn] [char](1) NULL, 
    [ReportEU] [char](1) NULL, 
    [Report347] [char](1) NULL, 
    [Printed] [char](1) NULL, 
    [DocType] [nvarchar](60) NULL, 
    [AttNum] [int] NULL, 
    [GenRegNo] [char](1) NULL, 
    [RG23APart2] [int] NULL, 
    [RG23CPart2] [int] NULL, 
    [MatType] [int] NULL, 
    [Creator] [nvarchar](155) NULL, 
    [Approver] [nvarchar](155) NULL, 
    [Location] [int] NULL, 
    [SeqCode] [smallint] NULL, 
    [Serial] [int] NULL, 
    [SeriesStr] [nvarchar](3) NULL, 
    [SubStr] [nvarchar](3) NULL, 
    [AutoWT] [char](1) NULL, 
    [WTSum] [numeric](19, 6) NULL, 
    [WTSumSC] [numeric](19, 6) NULL, 
    [WTSumFC] [numeric](19, 6) NULL, 
    [WTApplied] [numeric](19, 6) NULL, 
    [WTAppliedS] [numeric](19, 6) NULL, 
    [WTAppliedF] [numeric](19, 6) NULL, 
    [BaseAmnt] [numeric](19, 6) NULL, 
    [BaseAmntSC] [numeric](19, 6) NULL, 
    [BaseAmntFC] [numeric](19, 6) NULL, 
    [BaseVtAt] [numeric](19, 6) NULL, 
    [BaseVtAtSC] [numeric](19, 6) NULL, 
    [BaseVtAtFC] [numeric](19, 6) NULL, 
    [VersionNum] [nvarchar](11) NULL, 
    [BaseTrans] [int] NULL, 
    [ResidenNum] [char](1) NULL, 
    [OperatCode] [char](1) NULL, 
    [Ref3] [nvarchar](27) NULL, 
    [SSIExmpt] [char](1) NULL, 
    [SignMsg] [ntext] NULL, 
    [SignDigest] [ntext] NULL, 
    [CertifNum] [nvarchar](50) NULL, 
    [KeyVersion] [int] NULL, 
    [CUP] [int] NULL, 
    [CIG] [int] NULL, 
    [SupplCode] [nvarchar](254) NULL, 
    [SPSrcType] [int] NULL, 
    [SPSrcID] [int] NULL, 
    [SPSrcDLN] [int] NULL, 
    [DeferedTax] [char](1) NULL, 
    [AgrNo] [int] NULL, 
    [SeqNum] [int] NULL, 
    [ECDPosTyp] [char](1) NULL, 
    [RptPeriod] [nvarchar](5) NULL, 
    [RptMonth] [datetime] NULL, 
    [U_HOF_APPROVAL] [nvarchar](10) NULL, 
    [U_Project] [nvarchar](15) NULL, 
    [U_Site] [nvarchar](15) NULL, 
    [U_Prepared] [nvarchar](20) NULL, 
    [U_Serial] [nvarchar](10) NULL, 
CONSTRAINT [OJDT_PRIMARY] PRIMARY KEY CLUSTERED 
(
    [TransId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 
GO 
SET ANSI_PADDING OFF 
GO 
ALTER TABLE [dbo].[BGT1] ADD CONSTRAINT [DF_BGT1_Line_ID] DEFAULT ((0)) FOR [Line_ID] 
GO 
ALTER TABLE [dbo].[BGT1] ADD CONSTRAINT [DF_BGT1_Instance] DEFAULT ((1)) FOR [Instance] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_Line_ID] DEFAULT ((0)) FOR [Line_ID] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_IntrnMatch] DEFAULT ((0)) FOR [IntrnMatch] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_ExtrMatch] DEFAULT ((0)) FOR [ExtrMatch] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_TransType] DEFAULT ('-1') FOR [TransType] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_RelTransId] DEFAULT ((-1)) FOR [RelTransId] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_RelLineID] DEFAULT ((-1)) FOR [RelLineID] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_RelType] DEFAULT ('N') FOR [RelType] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_LogInstanc] DEFAULT ((0)) FOR [LogInstanc] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_AdjTran] DEFAULT ('N') FOR [AdjTran] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_RevSource] DEFAULT ('N') FOR [RevSource] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_ObjType] DEFAULT ('30') FOR [ObjType] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_MultMatch] DEFAULT ((0)) FOR [MultMatch] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_VatLine] DEFAULT ('N') FOR [VatLine] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_Closed] DEFAULT ('N') FOR [Closed] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_LineType] DEFAULT ((0)) FOR [LineType] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_SequenceNr] DEFAULT ((0)) FOR [SequenceNr] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_IsNet] DEFAULT ('Y') FOR [IsNet] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_DunWizBlck] DEFAULT ('N') FOR [DunWizBlck] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_DunnLevel] DEFAULT ((0)) FOR [DunnLevel] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_TaxType] DEFAULT ((0)) FOR [TaxType] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_TaxPostAcc] DEFAULT ('N') FOR [TaxPostAcc] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_ValidFrom] DEFAULT ('19000101') FOR [ValidFrom] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_MIEntry] DEFAULT ((0)) FOR [MIEntry] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_MIVEntry] DEFAULT ((0)) FOR [MIVEntry] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_ClsInTP] DEFAULT ((0)) FOR [ClsInTP] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_CenVatCom] DEFAULT ((-1)) FOR [CenVatCom] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_MatType] DEFAULT ((-1)) FOR [MatType] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_PstngType] DEFAULT ((0)) FOR [PstngType] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_ValidFrom2] DEFAULT ('19000101') FOR [ValidFrom2] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_ValidFrom3] DEFAULT ('19000101') FOR [ValidFrom3] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_ValidFrom4] DEFAULT ('19000101') FOR [ValidFrom4] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_ValidFrom5] DEFAULT ('19000101') FOR [ValidFrom5] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_WTLiable] DEFAULT ('N') FOR [WTLiable] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_WTLine] DEFAULT ('N') FOR [WTLine] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_PayBlock] DEFAULT ('N') FOR [PayBlock] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_InterimTyp] DEFAULT ((0)) FOR [InterimTyp] 
GO 
ALTER TABLE [dbo].[JDT1] ADD CONSTRAINT [DF_JDT1_Ordered] DEFAULT ('N') FOR [Ordered] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Finanse] DEFAULT ('N') FOR [Finanse] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Budget] DEFAULT ('N') FOR [Budget] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Frozen] DEFAULT ('N') FOR [Frozen] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Postable] DEFAULT ('Y') FOR [Postable] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Levels] DEFAULT ((2)) FOR [Levels] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_CashBox] DEFAULT ('N') FOR [CashBox] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_GroupMask] DEFAULT ((1)) FOR [GroupMask] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_RateTrans] DEFAULT ('Y') FOR [RateTrans] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_TaxIncome] DEFAULT ('N') FOR [TaxIncome] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_ExmIncome] DEFAULT ('N') FOR [ExmIncome] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_ActType] DEFAULT ('N') FOR [ActType] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Transfered] DEFAULT ('N') FOR [Transfered] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_BlncTrnsfr] DEFAULT ('N') FOR [BlncTrnsfr] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_OverType] DEFAULT ('N') FOR [OverType] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_SysMatch] DEFAULT ((-1)) FOR [SysMatch] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_PrevYear] DEFAULT ('N') FOR [PrevYear] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Protected] DEFAULT ('N') FOR [Protected] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_RealAcct] DEFAULT ('N') FOR [RealAcct] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Advance] DEFAULT ('Y') FOR [Advance] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_RevalMatch] DEFAULT ('N') FOR [RevalMatch] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_DataSource] DEFAULT ('N') FOR [DataSource] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_LocMth] DEFAULT ('Y') FOR [LocMth] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_LocManTran] DEFAULT ('N') FOR [LocManTran] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_LogInstanc] DEFAULT ((0)) FOR [LogInstanc] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_ObjType] DEFAULT ('1') FOR [ObjType] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_ValidFor] DEFAULT ('N') FOR [ValidFor] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_FrozenFor] DEFAULT ('N') FOR [FrozenFor] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Counter] DEFAULT ((0)) FOR [Counter] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_CfwRlvnt] DEFAULT ('N') FOR [CfwRlvnt] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_ExchRate] DEFAULT ('Y') FOR [ExchRate] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_VatChange] DEFAULT ('Y') FOR [VatChange] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_TaxPostAcc] DEFAULT ('N') FOR [TaxPostAcc] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_BalDirect] DEFAULT ('0') FOR [BalDirect] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_MultiLink] DEFAULT ('N') FOR [MultiLink] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_PrjRelvnt] DEFAULT ('N') FOR [PrjRelvnt] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Dim1Relvnt] DEFAULT ('N') FOR [Dim1Relvnt] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Dim2Relvnt] DEFAULT ('N') FOR [Dim2Relvnt] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Dim3Relvnt] DEFAULT ('N') FOR [Dim3Relvnt] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Dim4Relvnt] DEFAULT ('N') FOR [Dim4Relvnt] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_Dim5Relvnt] DEFAULT ('N') FOR [Dim5Relvnt] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_AccrualTyp] DEFAULT ('N') FOR [AccrualTyp] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_DatevAutoA] DEFAULT ('N') FOR [DatevAutoA] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_DatevFirst] DEFAULT ('Y') FOR [DatevFirst] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_SnapShotId] DEFAULT ((0)) FOR [SnapShotId] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_PCN874Rpt] DEFAULT ('N') FOR [PCN874Rpt] 
GO 
ALTER TABLE [dbo].[OACT] ADD CONSTRAINT [DF_OACT_SCAdjust] DEFAULT ('N') FOR [SCAdjust] 
GO 
ALTER TABLE [dbo].[OBGT] ADD CONSTRAINT [DF_OBGT_Instance] DEFAULT ((1)) FOR [Instance] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_BtfStatus] DEFAULT ('O') FOR [BtfStatus] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_TransType] DEFAULT ('-1') FOR [TransType] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_PCAddition] DEFAULT ('N') FOR [PCAddition] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_DataSource] DEFAULT ('N') FOR [DataSource] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_RefndRprt] DEFAULT ('N') FOR [RefndRprt] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_LogInstanc] DEFAULT ((0)) FOR [LogInstanc] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_ObjType] DEFAULT ('30') FOR [ObjType] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_AdjTran] DEFAULT ('N') FOR [AdjTran] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_RevSource] DEFAULT ('N') FOR [RevSource] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_AutoStorno] DEFAULT ('N') FOR [AutoStorno] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_Corisptivi] DEFAULT ('N') FOR [Corisptivi] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_StampTax] DEFAULT ('N') FOR [StampTax] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_Series] DEFAULT ((0)) FOR [Series] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_AutoVAT] DEFAULT ('N') FOR [AutoVAT] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_BlockDunn] DEFAULT ('N') FOR [BlockDunn] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_ReportEU] DEFAULT ('N') FOR [ReportEU] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_Report347] DEFAULT ('N') FOR [Report347] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_Printed] DEFAULT ('N') FOR [Printed] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_AttNum] DEFAULT ((0)) FOR [AttNum] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_GenRegNo] DEFAULT ('N') FOR [GenRegNo] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_AutoWT] DEFAULT ('N') FOR [AutoWT] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_ResidenNum] DEFAULT ('1') FOR [ResidenNum] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_DeferedTax] DEFAULT ('N') FOR [DeferedTax] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_ECDPosTyp] DEFAULT ('N') FOR [ECDPosTyp] 
GO 
ALTER TABLE [dbo].[OJDT] ADD CONSTRAINT [DF_OJDT_U_HOF_APPROVAL] DEFAULT ('Pending') FOR [U_HOF_APPROVAL] 
GO 
+0

第一件事只有標籤。 mysql或sqlserver?第二件事不包括真正的數據庫名稱。這不是很好的做法。 – pedram

+0

我已經標記了,如果你仔細檢查@rajeshmpanchal – kaurSukh

+0

沒關係,這很好。可能我的網速很慢。 – pedram

回答

0

您的代碼如下所示:DBMS您使用的這些

SELECT *, 
    CASE WHEN NameTrim = 'Travel' THEN SUM(budget) OVER() ELSE NULL END AS TravelCost, 
    CASE WHEN NameTrim = 'Expense' THEN SUM(budget) OVER() ELSE NULL END AS Expense 
FROM (
    SELECT CONVERT(INT, T2.budget) AS budget, -- budget is char type, must convert to use SUM 
     CASE 
     when T2.[AcctName] like '%Travel%' then 'Travel' 
     when T2.[AcctName] like '%Expense%' then 'Expense' 
     end as NameTrim 
    FROM JDT1 T0 
     INNER JOIN OJDT T1 ON T0.[TransId] = T1.[TransId] 
     INNER JOIN OACT T2 ON T0.[Account] = T2.[AcctCode] 
     CROSS JOIN OBGT T3 
     INNER JOIN BGT1 T4 ON T3.[AbsId] = T4.[BudgId] 
     INNER JOIN OBGS T5 ON T3.[Instance] = T5.[AbsId] 
     CROSS JOIN OASC T6 
    WHERE T5.[Name] = 'Main Budget 2015' 
) AS A 
+0

此查詢給我關鍵字'時'附近的語法不正確。請再檢查一次 。 @NguyễnHảiTriều – kaurSukh

+0

請問你能否正確地將它添加到給定的查詢中。因爲當我使用它。它給了我很多錯誤,可能是我錯誤地使用了。請在給定的查詢中添加。所以它會清楚地告訴我。 – kaurSukh

+0

什麼沒有。線不正確? –