테이블 T_BudgetCharts가 제공됩니다. 여기에는 1 레벨에서 N 레벨까지의 항목의 계층 적 구조가 포함됩니다. N 번째 레벨의 항목에만 값이 할당됩니다. 마지막 레벨이 아닌 다른 항목에는 0이 부여됩니다.두 테이블 간의 계층 구조로 항목 합계를 가져 오는 SQL 쿼리
다른 테이블 T_Invoice가 제공됩니다. 이 테이블에는 BudgetItemID 열을 사용하여 첫 번째 테이블과 관련된 일부 예약이 들어 있습니다.
내가 필요한 것은 T_BudgetCharts의 각 BudgetItem의 TotalSum을 반환하는 SQL 쿼리를 작성하는 것입니다. 각 레벨의 합계, T_Invoice 테이블의 각 항목에 대한 예약 된 합계 (여기에 문제가 있습니다) - 예약 된 항목 포함 herarchy의 각 레벨에 대한 합계와 첫 번째 두 값의 차이 인 차이 합계입니다.
내가 SQLSERVER 2008R2 작업입니다 ...
이해에 대한 이미지가 첨부되어 있습니다. 또한 SQL 덤프 ...
CREATE TABLE [dbo].[T_BudgetCharts](
[ID] [int] IDENTITY(1,1) NOT NULL,
[BudgetId] [int] NOT NULL,
[BudgetItemLevel] [int] NOT NULL,
[BudgetItemCode] [nvarchar](20) NOT NULL,
[BudgetItemName] [nvarchar](300) NOT NULL,
[BudgetItemMasterID] [int] NULL,
[BudgetItemBudegtValue] [decimal](18, 2) NULL,
[BugedtIdOriginal] [int] NULL,
[MasterIdOriginal] [int] NULL,
[BugedtItemStatus] [int] NOT NULL,
[sysDateTimeCreated] [datetime] NOT NULL,
[sysDateTimeModified] [datetime] NOT NULL,
CONSTRAINT [PK_T_BudgetCharts] PRIMARY KEY CLUSTERED
(
[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]
SET IDENTITY_INSERT [dbo].[T_BudgetCharts] ON
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (1, 1, 1, N'N/A', N'Not assigned', NULL, CAST(0.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CA5499 AS DateTime), CAST(0x0000A68000CA5499 AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (2, 1, 2, N'N/A', N'Not assigned', 1, CAST(0.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CA5499 AS DateTime), CAST(0x0000A68000CA5499 AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (3, 1, 3, N'N/A', N'Not assigned', 2, CAST(1000.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CA5499 AS DateTime), CAST(0x0000A68000CA7067 AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (4, 2, 1, N'N/A', N'Not assigned', NULL, CAST(0.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CA6213 AS DateTime), CAST(0x0000A68000CA6213 AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (5, 2, 2, N'N/A', N'Not assigned', 4, CAST(0.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CA6213 AS DateTime), CAST(0x0000A68000CA6213 AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (6, 2, 3, N'N/A', N'Not assigned', 5, CAST(0.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CA6213 AS DateTime), CAST(0x0000A68000CA6213 AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (7, 1, 1, N'A', N'Category A', NULL, CAST(0.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CA95BA AS DateTime), CAST(0x0000A68000CA95BA AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (8, 1, 2, N'100', N'Income', 7, CAST(0.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CAAEAF AS DateTime), CAST(0x0000A68000CAAEAF AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (9, 1, 2, N'200', N'Outcome', 7, CAST(0.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CAB852 AS DateTime), CAST(0x0000A68000CAB852 AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (10, 1, 3, N'A.100.1', N'Income 1', 8, CAST(2500.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CADF5F AS DateTime), CAST(0x0000A68000CB0545 AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (11, 1, 3, N'A.100.2', N'Income 2', 8, CAST(10000.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CAF466 AS DateTime), CAST(0x0000A68000CAF466 AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (13, 1, 3, N'A.200.1', N'Outcome 1', 9, CAST(6000.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CBB48C AS DateTime), CAST(0x0000A68000CBB48C AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (14, 1, 3, N'A.200.2', N'Other stuff', 9, CAST(3000.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CBC7EE AS DateTime), CAST(0x0000A68000CBC7EE AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (15, 2, 1, N'T1', N'Money', NULL, CAST(0.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CC2709 AS DateTime), CAST(0x0000A68000CC2709 AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (16, 2, 2, N'99', N'Type A', 15, CAST(0.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CC3E2C AS DateTime), CAST(0x0000A68000CC3E2C AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (17, 2, 2, N'98', N'Type B', 15, CAST(0.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CC4E48 AS DateTime), CAST(0x0000A68000CC4E48 AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (18, 2, 3, N'T1.98.1', N'B 1', 17, CAST(300.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CC698D AS DateTime), CAST(0x0000A68000CC698D AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (19, 2, 3, N'T1.98.2', N'B 2', 17, CAST(800.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CC73DB AS DateTime), CAST(0x0000A68000CC73DB AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (20, 2, 3, N'T1.99.1', N'A 1', 16, CAST(600.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CC958F AS DateTime), CAST(0x0000A68000CC958F AS DateTime))
INSERT [dbo].[T_BudgetCharts] ([ID], [BudgetId], [BudgetItemLevel], [BudgetItemCode], [BudgetItemName], [BudgetItemMasterID], [BudgetItemBudegtValue], [BugedtIdOriginal], [MasterIdOriginal], [BugedtItemStatus], [sysDateTimeCreated], [sysDateTimeModified]) VALUES (21, 2, 3, N'T1.99.2', N'A 2', 16, CAST(400.00 AS Decimal(18, 2)), NULL, NULL, 1, CAST(0x0000A68000CCA173 AS DateTime), CAST(0x0000A68000CCA173 AS DateTime))
SET IDENTITY_INSERT [dbo].[T_BudgetCharts] OFF
CREATE TABLE [dbo].[T_Invoice](
[ID] [int] IDENTITY(1,1) NOT NULL,
[InvoiceBudgetingID] [int] NOT NULL,
[OrderNo] [int] NOT NULL,
[BudgetID] [int] NOT NULL,
[BudgetItemID] [int] NOT NULL,
[AmountProjectCurrency] [decimal](28, 16) NOT NULL,
[AmountBudgetCurrency] [decimal](28, 16) NOT NULL,
[BudgetCurrencyRate] [decimal](28, 16) NOT NULL,
[Percentage] [decimal](28, 16) NOT NULL,
CONSTRAINT [PK_T_Invoice] PRIMARY KEY CLUSTERED
(
[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]
SET IDENTITY_INSERT [dbo].[T_Invoice] ON
INSERT [dbo].[T_Invoice] ([ID], [InvoiceBudgetingID], [OrderNo], [BudgetID], [BudgetItemID], [AmountProjectCurrency], [AmountBudgetCurrency], [BudgetCurrencyRate], [Percentage]) VALUES (1, 1, 1, 1, 10, CAST(300.0000000000000000 AS Decimal(28, 16)), CAST(300.0000000000000000 AS Decimal(28, 16)), CAST(1.0000000000000000 AS Decimal(28, 16)), CAST(100.0000000000000000 AS Decimal(28, 16)))
INSERT [dbo].[T_Invoice] ([ID], [InvoiceBudgetingID], [OrderNo], [BudgetID], [BudgetItemID], [AmountProjectCurrency], [AmountBudgetCurrency], [BudgetCurrencyRate], [Percentage]) VALUES (2, 2, 1, 1, 14, CAST(200.0000000000000000 AS Decimal(28, 16)), CAST(200.0000000000000000 AS Decimal(28, 16)), CAST(1.0000000000000000 AS Decimal(28, 16)), CAST(100.0000000000000000 AS Decimal(28, 16)))
INSERT [dbo].[T_Invoice] ([ID], [InvoiceBudgetingID], [OrderNo], [BudgetID], [BudgetItemID], [AmountProjectCurrency], [AmountBudgetCurrency], [BudgetCurrencyRate], [Percentage]) VALUES (3, 2, 1, 1, 14, CAST(500.0000000000000000 AS Decimal(28, 16)), CAST(100.0000000000000000 AS Decimal(28, 16)), CAST(1.0000000000000000 AS Decimal(28, 16)), CAST(100.0000000000000000 AS Decimal(28, 16)))
SET IDENTITY_INSERT [dbo].[T_Invoice] OFF
**
CREATE TABLE [dbo].[T_Bookings](
[ID] [int] IDENTITY(1,1) NOT NULL,
[BookingTypeID] [int] NOT NULL,
[ProjectID] [int] NOT NULL,
[InvoiceDate] [date] NOT NULL
CONSTRAINT [PK_T_Bookings] PRIMARY KEY CLUSTERED
(
[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]
/****** Object: Table [dbo].[T_Bookings] Script Date: 09/21/2016 15:06:23 ******/
SET IDENTITY_INSERT [dbo].[T_Bookings] ON
INSERT [dbo].[T_Bookings] ([ID], [BookingTypeID], [ProjectID], [InvoiceDate]) VALUES (1, 1, 1, CAST(0xCF3B0B00 AS Date))
INSERT [dbo].[T_Bookings] ([ID], [BookingTypeID], [ProjectID], [InvoiceDate]) VALUES (2, 2, 1, CAST(0xCF3B0B00 AS Date))
INSERT [dbo].[T_Bookings] ([ID], [BookingTypeID], [ProjectID], [InvoiceDate]) VALUES (3, 1, 1, CAST(0xCF3B0B00 AS Date))
SET IDENTITY_INSERT [dbo].[T_Bookings] OFF
**
지금까지 내가 첫 번째 부분이 있지만 예약 합과의 차이를 얻을 질수 ... 내 현재 검색어입니다 ... 누군가 도움을 줄 수 있기를 바랍니다 ...
WITH C AS
(
SELECT b.Id,
b.BudgetItemMasterID,
b.BudgetItemCode,
b.BudgetItemName,
b.BudgetItemBudegtValue,
b.BugedtItemStatus,
b.Id AS RootID
FROM T_BudgetCharts b
WHERE b.BugedtItemStatus = 1
UNION ALL
SELECT b.Id,
b.BudgetItemMasterID,
b.BudgetItemCode,
b.BudgetItemName,
b.BudgetItemBudegtValue,
b.BugedtItemStatus,
C.RootID
FROM T_BudgetCharts b
INNER JOIN C ON b.BudgetItemMasterID = C.Id
WHERE b.BugedtItemStatus = 1
)
SELECT b.ID as ID,
b.BudgetItemMasterID AS MasterID,
b.BudgetItemCode AS BudgetItemCode,
b.BudgetItemName AS BudgetItemName,
s.TotalValue AS TotalValue,
ISNULL(g.Spent, 0) as BookedSum,
0 as DifferenceSum --Not implemnted yet..
FROM T_BudgetCharts b
INNER JOIN (SELECT RootID, SUM(BudgetItemBudegtValue) AS TotalValue FROM C GROUP BY RootID) AS s ON b.Id = s.RootID
LEFT JOIN (SELECT BudgetItemId, SUM(AmountProjectCurrency) AS Spent FROM dbo.T_Invoice GRoup By BudgetItemId) AS g ON b.ID = g.BudgetItemID
ORDER BY b.BudgetItemCode
@ JohnCappelletti 답변에 어떤 문제가 발생 했습니까? – digestBen
당신은 단지 그룹화와 합계를 추가해야합니다 - 나는이 답변과 매우 유사한 것을 사용할 수 있다고 생각합니다 : http://stackoverflow.com/questions/24394601/recursive-sum-in-tree-structure – SlimsGhost
매우 복잡합니다 . 사용자 @JohnCappelletti는 오늘 여기에 좋은 대답을주었습니다. 그러나 모든 의견에 어떤 일이 있었는지 모르겠습니다 ... – digestBen