Sql server SQL查询以获取两个表之间分层结构中的项和
给出了一个表T_BudgetCharts。它包含从1到n级的项目层次结构。只有第n级的项目才具有指定的值。其他不是最后一级的项目的assignd为0 另一张表中给出了T_发票。此表包含一些使用BudgetItemMid列与第一个表相关的预订 我需要编写一个SQL查询,返回T_BudgetCharts中每个BudgetItem的总和-包括每个级别的总和,T_Invoice表中每个项目的记账总和-(问题来了)-包括herarchy中每个级别的记账总和,以及差分总和,这是前两个值之间的差值 我正在SQLServer2008R2上工作 为了更好地理解,附上了一张图片。还有SQL转储Sql server SQL查询以获取两个表之间分层结构中的项和,sql-server,sum,common-table-expression,hierarchical,Sql Server,Sum,Common Table Expression,Hierarchical,给出了一个表T_BudgetCharts。它包含从1到n级的项目层次结构。只有第n级的项目才具有指定的值。其他不是最后一级的项目的assignd为0 另一张表中给出了T_发票。此表包含一些使用BudgetItemMid列与第一个表相关的预订 我需要编写一个SQL查询,返回T_BudgetCharts中每个BudgetItem的总和-包括每个级别的总和,T_Invoice表中每个项目的记账总和-(问题来了)-包括herarchy中每个级别的记账总和,以及差分总和,这是前两个值之间的差值 我正在SQ
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
**
**
到目前为止,我有第一部分,但无法得到预定的金额和差额…这是我目前的查询…希望有人能帮助
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
编辑:修复了连接条件和一些其他详细信息(抱歉!) 以下CTE将每个Id连接为一棵树的“根”,该树包含自身和所有级别的所有子级)。使用CTE的查询会根据组(Id及其所有子项)汇总您想要的任何内容 我试图让它看起来像你的照片,但你可能仍然需要调整它,以适应你的需要。关键部分是使用CTE定义每个“组”(每个父级及其所有子级)
@约翰卡佩莱蒂:你的答案怎么了?你只需要添加分组和求和-我想你可以使用与这个答案非常相似的东西:它有点复杂。用户@johncapelletti今天在这里给出了一个很好的答案,但我不知道所有的评论都发生了什么……你的解决方案不起作用。分层总和不在预算中计算,也不在发票中计算……如果我在T_发票表中有额外的列,这是交易的类型,比如说收入和结果-将此值添加到第一个T_预算表项的总和会很困难吗?示例:如果是trasactiontype收入,则将此值添加到第一个表中的总和(分层所有级别)。否则就照现在的样子做吧…@slimsghosts我不太清楚,但这听起来像是将
sum(isnull(b.BudgetItemBudegtValue,0))作为TotalValue更改为sum(isnull(b.BudgetItemBudegtValue,0)+(g.TransactionType='INCOME'然后g.AmountColumn else 0 end时的情况))作为总值
。请注意,case
表达式中添加了任何内容,可以对其进行调整,以包括所需的任何条件和添加量。此功能对于任何向上滚动每个“父级”层次结构的计算都是一样的。如果需要澄清,请告诉我
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
WITH C AS
(
SELECT b.Id AS RootId,
b.BudgetItemMasterID as RootBudgetItemMasterID,
b.BudgetItemCode as RootBudgetItemCode,
b.BudgetItemName as RootBudgetItemName,
b.Id as ChildId
FROM T_BudgetCharts b
WHERE b.BugedtItemStatus = 1
UNION ALL
SELECT c.RootId,
c.RootBudgetItemMasterID,
c.RootBudgetItemCode,
c.RootBudgetItemName,
b.Id as ChildId
FROM T_BudgetCharts b
INNER JOIN C ON b.BudgetItemMasterID = C.ChildId
WHERE b.BugedtItemStatus = 1
)
SELECT
C.RootId as Id,
C.RootBudgetItemMasterID as BudgetItemMasterID,
C.RootBudgetItemCode as BudgetItemCode,
C.RootBudgetItemName as BudgetItemName,
sum(isnull(b.BudgetItemBudegtValue, 0)) AS TotalValue,
sum(isnull(g.AmountProjectCurrency, 0)) AS BookedSum,
sum(isnull(b.BudgetItemBudegtValue, 0)) - sum(isnull(g.AmountProjectCurrency, 0)) as DifferenceSum
from C
inner join T_BudgetCharts b ON b.Id = C.ChildId and b.BugedtItemStatus = 1
left join T_Invoice g ON b.ID = g.BudgetItemID
group by C.RootId, C.RootBudgetItemMasterID, C.RootBudgetItemCode, C.RootBudgetItemName
order by C.RootId, C.RootBudgetItemMasterID, C.RootBudgetItemCode, C.RootBudgetItemName