Hi all,
I was recently (and very kindly) helped by Jesus under the following thread Common Table Expression Help.
Being a complete newbie to CTE's, I was at a loss on how to achieve my goal and whilst I managed to use Jesus' help to create other needed queries, I have found myself at a loss with this one and am hoping that someone far better at SQL than me can help.
I have the following database:
USE [master] GO /****** Object: Database [TestDatabase] Script Date: 28/05/2013 13:00:35 ******/ CREATE DATABASE [TestDatabase] CONTAINMENT = NONE ON PRIMARY ( NAME = N'TestDatabase', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.ATLASSQL2012\MSSQL\DATA\TestDatabase.mdf' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) LOG ON ( NAME = N'TestDatabase_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.ATLASSQL2012\MSSQL\DATA\TestDatabase_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) GO ALTER DATABASE [TestDatabase] SET COMPATIBILITY_LEVEL = 110 GO IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')) begin EXEC [TestDatabase].[dbo].[sp_fulltext_database] @action = 'enable' end GO ALTER DATABASE [TestDatabase] SET ANSI_NULL_DEFAULT OFF GO ALTER DATABASE [TestDatabase] SET ANSI_NULLS OFF GO ALTER DATABASE [TestDatabase] SET ANSI_PADDING OFF GO ALTER DATABASE [TestDatabase] SET ANSI_WARNINGS OFF GO ALTER DATABASE [TestDatabase] SET ARITHABORT OFF GO ALTER DATABASE [TestDatabase] SET AUTO_CLOSE OFF GO ALTER DATABASE [TestDatabase] SET AUTO_CREATE_STATISTICS ON GO ALTER DATABASE [TestDatabase] SET AUTO_SHRINK OFF GO ALTER DATABASE [TestDatabase] SET AUTO_UPDATE_STATISTICS ON GO ALTER DATABASE [TestDatabase] SET CURSOR_CLOSE_ON_COMMIT OFF GO ALTER DATABASE [TestDatabase] SET CURSOR_DEFAULT GLOBAL GO ALTER DATABASE [TestDatabase] SET CONCAT_NULL_YIELDS_NULL OFF GO ALTER DATABASE [TestDatabase] SET NUMERIC_ROUNDABORT OFF GO ALTER DATABASE [TestDatabase] SET QUOTED_IDENTIFIER OFF GO ALTER DATABASE [TestDatabase] SET RECURSIVE_TRIGGERS OFF GO ALTER DATABASE [TestDatabase] SET DISABLE_BROKER GO ALTER DATABASE [TestDatabase] SET AUTO_UPDATE_STATISTICS_ASYNC OFF GO ALTER DATABASE [TestDatabase] SET DATE_CORRELATION_OPTIMIZATION OFF GO ALTER DATABASE [TestDatabase] SET TRUSTWORTHY OFF GO ALTER DATABASE [TestDatabase] SET ALLOW_SNAPSHOT_ISOLATION OFF GO ALTER DATABASE [TestDatabase] SET PARAMETERIZATION SIMPLE GO ALTER DATABASE [TestDatabase] SET READ_COMMITTED_SNAPSHOT OFF GO ALTER DATABASE [TestDatabase] SET HONOR_BROKER_PRIORITY OFF GO ALTER DATABASE [TestDatabase] SET RECOVERY FULL GO ALTER DATABASE [TestDatabase] SET MULTI_USER GO ALTER DATABASE [TestDatabase] SET PAGE_VERIFY CHECKSUM GO ALTER DATABASE [TestDatabase] SET DB_CHAINING OFF GO ALTER DATABASE [TestDatabase] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) GO ALTER DATABASE [TestDatabase] SET TARGET_RECOVERY_TIME = 0 SECONDS GO EXEC sys.sp_db_vardecimal_storage_format N'TestDatabase', N'ON' GO USE [TestDatabase] GO /****** Object: Table [dbo].[AttributeGroups] Script Date: 28/05/2013 13:00:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[AttributeGroups]( [AttributeGroupId] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NOT NULL, CONSTRAINT [PK_AttributeGroups] PRIMARY KEY CLUSTERED ( [AttributeGroupId] 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].[Attributes] Script Date: 28/05/2013 13:00:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Attributes]( [AttributeId] [int] IDENTITY(1,1) NOT NULL, [AttributeGroupId] [int] NOT NULL, [AttributeValue] [decimal](22, 6) NOT NULL, CONSTRAINT [PK_Attributes] PRIMARY KEY CLUSTERED ( [AttributeId] 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].[AttributesToItems] Script Date: 28/05/2013 13:00:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[AttributesToItems]( [AttributeId] [int] NOT NULL, [ItemId] [int] NOT NULL, CONSTRAINT [PK_AttributesToItems] PRIMARY KEY CLUSTERED ( [AttributeId] ASC, [ItemId] 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].[Containers] Script Date: 28/05/2013 13:00:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Containers]( [ContainerId] [int] IDENTITY(1,1) NOT NULL, [BaseContainerId] [int] NULL, [Name] [nvarchar](50) NOT NULL, CONSTRAINT [PK_Containers] PRIMARY KEY CLUSTERED ( [ContainerId] 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].[ItemRelationships] Script Date: 28/05/2013 13:00:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[ItemRelationships]( [ChildItemId] [int] NOT NULL, [ParentItemId] [int] NOT NULL, CONSTRAINT [PK_ItemRelationships] PRIMARY KEY CLUSTERED ( [ChildItemId] ASC, [ParentItemId] 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].[Items] Script Date: 28/05/2013 13:00:35 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Items]( [ItemId] [int] IDENTITY(1,1) NOT NULL, [ContainerId] [int] NOT NULL, [Name] [nvarchar](50) NOT NULL, CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED ( [ItemId] 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 IDENTITY_INSERT [dbo].[AttributeGroups] ON GO INSERT [dbo].[AttributeGroups] ([AttributeGroupId], [Name]) VALUES (1, N'Group 1') GO SET IDENTITY_INSERT [dbo].[AttributeGroups] OFF GO SET IDENTITY_INSERT [dbo].[Attributes] ON GO INSERT [dbo].[Attributes] ([AttributeId], [AttributeGroupId], [AttributeValue]) VALUES (1, 1, CAST(10.000000 AS Decimal(22, 6))) GO INSERT [dbo].[Attributes] ([AttributeId], [AttributeGroupId], [AttributeValue]) VALUES (3, 1, CAST(20.000000 AS Decimal(22, 6))) GO INSERT [dbo].[Attributes] ([AttributeId], [AttributeGroupId], [AttributeValue]) VALUES (4, 1, CAST(30.000000 AS Decimal(22, 6))) GO INSERT [dbo].[Attributes] ([AttributeId], [AttributeGroupId], [AttributeValue]) VALUES (5, 1, CAST(40.000000 AS Decimal(22, 6))) GO INSERT [dbo].[Attributes] ([AttributeId], [AttributeGroupId], [AttributeValue]) VALUES (6, 1, CAST(50.000000 AS Decimal(22, 6))) GO INSERT [dbo].[Attributes] ([AttributeId], [AttributeGroupId], [AttributeValue]) VALUES (7, 1, CAST(60.000000 AS Decimal(22, 6))) GO INSERT [dbo].[Attributes] ([AttributeId], [AttributeGroupId], [AttributeValue]) VALUES (8, 1, CAST(70.000000 AS Decimal(22, 6))) GO INSERT [dbo].[Attributes] ([AttributeId], [AttributeGroupId], [AttributeValue]) VALUES (9, 1, CAST(80.000000 AS Decimal(22, 6))) GO INSERT [dbo].[Attributes] ([AttributeId], [AttributeGroupId], [AttributeValue]) VALUES (10, 1, CAST(90.000000 AS Decimal(22, 6))) GO INSERT [dbo].[Attributes] ([AttributeId], [AttributeGroupId], [AttributeValue]) VALUES (11, 1, CAST(100.000000 AS Decimal(22, 6))) GO INSERT [dbo].[Attributes] ([AttributeId], [AttributeGroupId], [AttributeValue]) VALUES (12, 1, CAST(110.000000 AS Decimal(22, 6))) GO INSERT [dbo].[Attributes] ([AttributeId], [AttributeGroupId], [AttributeValue]) VALUES (13, 1, CAST(120.000000 AS Decimal(22, 6))) GO SET IDENTITY_INSERT [dbo].[Attributes] OFF GO INSERT [dbo].[AttributesToItems] ([AttributeId], [ItemId]) VALUES (1, 1) GO INSERT [dbo].[AttributesToItems] ([AttributeId], [ItemId]) VALUES (3, 2) GO INSERT [dbo].[AttributesToItems] ([AttributeId], [ItemId]) VALUES (4, 3) GO INSERT [dbo].[AttributesToItems] ([AttributeId], [ItemId]) VALUES (5, 4) GO INSERT [dbo].[AttributesToItems] ([AttributeId], [ItemId]) VALUES (6, 5) GO INSERT [dbo].[AttributesToItems] ([AttributeId], [ItemId]) VALUES (7, 6) GO INSERT [dbo].[AttributesToItems] ([AttributeId], [ItemId]) VALUES (8, 7) GO INSERT [dbo].[AttributesToItems] ([AttributeId], [ItemId]) VALUES (9, 8) GO INSERT [dbo].[AttributesToItems] ([AttributeId], [ItemId]) VALUES (10, 9) GO INSERT [dbo].[AttributesToItems] ([AttributeId], [ItemId]) VALUES (11, 10) GO INSERT [dbo].[AttributesToItems] ([AttributeId], [ItemId]) VALUES (12, 11) GO INSERT [dbo].[AttributesToItems] ([AttributeId], [ItemId]) VALUES (13, 12) GO SET IDENTITY_INSERT [dbo].[Containers] ON GO INSERT [dbo].[Containers] ([ContainerId], [BaseContainerId], [Name]) VALUES (1, NULL, N'Level 1') GO INSERT [dbo].[Containers] ([ContainerId], [BaseContainerId], [Name]) VALUES (2, 1, N'Level 2') GO INSERT [dbo].[Containers] ([ContainerId], [BaseContainerId], [Name]) VALUES (3, 1, N'Level 2b') GO INSERT [dbo].[Containers] ([ContainerId], [BaseContainerId], [Name]) VALUES (4, 2, N'Level 3') GO INSERT [dbo].[Containers] ([ContainerId], [BaseContainerId], [Name]) VALUES (5, NULL, N'TypeB') GO SET IDENTITY_INSERT [dbo].[Containers] OFF GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (1, 13) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (2, 13) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (3, 13) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (4, 14) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (5, 14) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (6, 14) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (7, 15) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (8, 15) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (9, 15) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (10, 16) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (11, 16) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (12, 16) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (13, 17) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (14, 17) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (15, 17) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (1007, 13) GO INSERT [dbo].[ItemRelationships] ([ChildItemId], [ParentItemId]) VALUES (1008, 17) GO SET IDENTITY_INSERT [dbo].[Items] ON GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (1, 1, N'A') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (2, 1, N'B') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (3, 1, N'C') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (4, 1, N'D') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (5, 1, N'E') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (6, 1, N'F') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (7, 1, N'G') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (8, 1, N'H') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (9, 1, N'I') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (10, 1, N'J') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (11, 1, N'K') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (12, 1, N'L') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (13, 2, N'A2') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (14, 2, N'A2') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (15, 2, N'C2') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (16, 3, N'D2B') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (17, 4, N'A3') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (1007, 5, N'TypeB1') GO INSERT [dbo].[Items] ([ItemId], [ContainerId], [Name]) VALUES (1008, 5, N'TypeB2') GO SET IDENTITY_INSERT [dbo].[Items] OFF GO ALTER TABLE [dbo].[Attributes] WITH CHECK ADD CONSTRAINT [FK_Attributes_AttributeGroups] FOREIGN KEY([AttributeGroupId]) REFERENCES [dbo].[AttributeGroups] ([AttributeGroupId]) ON UPDATE CASCADE ON DELETE CASCADE GO ALTER TABLE [dbo].[Attributes] CHECK CONSTRAINT [FK_Attributes_AttributeGroups] GO ALTER TABLE [dbo].[AttributesToItems] WITH CHECK ADD CONSTRAINT [FK_AttributesToItems_Attributes] FOREIGN KEY([AttributeId]) REFERENCES [dbo].[Attributes] ([AttributeId]) ON UPDATE CASCADE ON DELETE CASCADE GO ALTER TABLE [dbo].[AttributesToItems] CHECK CONSTRAINT [FK_AttributesToItems_Attributes] GO ALTER TABLE [dbo].[AttributesToItems] WITH CHECK ADD CONSTRAINT [FK_AttributesToItems_Items] FOREIGN KEY([ItemId]) REFERENCES [dbo].[Items] ([ItemId]) ON UPDATE CASCADE ON DELETE CASCADE GO ALTER TABLE [dbo].[AttributesToItems] CHECK CONSTRAINT [FK_AttributesToItems_Items] GO ALTER TABLE [dbo].[Containers] WITH CHECK ADD CONSTRAINT [FK_Containers_Containers] FOREIGN KEY([BaseContainerId]) REFERENCES [dbo].[Containers] ([ContainerId]) GO ALTER TABLE [dbo].[Containers] CHECK CONSTRAINT [FK_Containers_Containers] GO ALTER TABLE [dbo].[ItemRelationships] WITH CHECK ADD CONSTRAINT [FK_ItemRelationships_ChildItems] FOREIGN KEY([ParentItemId]) REFERENCES [dbo].[Items] ([ItemId]) GO ALTER TABLE [dbo].[ItemRelationships] CHECK CONSTRAINT [FK_ItemRelationships_ChildItems] GO ALTER TABLE [dbo].[ItemRelationships] WITH CHECK ADD CONSTRAINT [FK_ItemRelationships_ParentItems] FOREIGN KEY([ChildItemId]) REFERENCES [dbo].[Items] ([ItemId]) GO ALTER TABLE [dbo].[ItemRelationships] CHECK CONSTRAINT [FK_ItemRelationships_ParentItems] GO ALTER TABLE [dbo].[Items] WITH CHECK ADD CONSTRAINT [FK_Items_Containers] FOREIGN KEY([ContainerId]) REFERENCES [dbo].[Containers] ([ContainerId]) ON UPDATE CASCADE ON DELETE CASCADE GO ALTER TABLE [dbo].[Items] CHECK CONSTRAINT [FK_Items_Containers] GO USE [master] GO ALTER DATABASE [TestDatabase] SET READ_WRITE GO
Logically, what I need to do is:
- Provide a ContainerId as a parameter AND a AttributeGroupId as a parameter
- Select all Items within the Container
- Move down the Container hierarchy to the absolute base Container of that provided in step 1, selecting all of the Child Items of those selected in Step 2 at each step (limited to those within the Child-to-Parent ItemRelationships). Note - it is important that only child objects within the immediate Container's base Container are selected. It is also important to note that the absolute base container could be the next level down, or even the ninth, or... etc.
- Using the selected absolute "base" items, obtain the linked Attributes (where they fall within the specified AttributeGroupId)
- Sum the attributes back up the hierarchy to the items within the specified Container.
Success so far:
I've successfully achieved items 1 to 4 (with partial 5) using the following:
DECLARE @containerId INT = 4; DECLARE @attributeGroupId INT = 1; -- Insert statements for procedure here WITH H AS ( SELECT I.*, CAST(NULL AS INT) AS ParentItemId FROM dbo.Items I WHERE I.ContainerId = @containerId UNION ALL SELECT I.*, R.ParentItemId FROM dbo.ItemRelationships R INNER JOIN H ON H.ItemId = R.ParentItemId INNER JOIN dbo.Items I ON R.ChildItemId = I.ItemId ), BC -- recursive base container AS ( SELECT C.ContainerId, C.BaseContainerId FROM dbo.Items I INNER JOIN dbo.Containers C ON I.ContainerId = C.ContainerId WHERE I.ContainerId = @containerId UNION ALL SELECT C.ContainerId, C.BaseContainerId FROM dbo.Containers C INNER JOIN BC ON BC.BaseContainerId = C.ContainerId ) , C -- Containers AS ( -- The anchor is the root container SELECT DISTINCT -- use of DISTINCT massively improves performance BC.ContainerId FROM BC WHERE BC.BaseContainerId IS NULL -- UNION ALL -- Child containers SELECT CNT.ContainerId FROM dbo.Containers CNT INNER JOIN C ON C.ContainerId = CNT.BaseContainerId ) SELECT --H.ItemId, H.ParentItemId, --H.ContainerId, --H.Name, --CAST(NULL AS DECIMAL) AS AttributeValue SUM(A.AttributeValue) as AttributeValue FROM H INNER JOIN dbo.AttributesToItems ATI ON H.ItemId = ATI.ItemId INNER JOIN dbo.Attributes A on ATI.AttributeId = A.AttributeId WHERE H.ContainerId IN ( SELECT C.ContainerId FROM C ) AND A.AttributeGroupId = @attributeGroupId GROUP BY H.ParentItemId
If I enter a ContainerId of 1, I get "NULL", "780" as the results. Not really an issue as other logic will prevent this from being called on a base Container level. For a ContainerId of 2 I get the following:
Which is correct. However, if I enter a ContainerId of 4 I still get the above, but what I actually want it to do is return Item with ID of 17 (which has child items of 13,14,15 in ContainerId 2 - the base of ContainerId 4 in which lies Item ID 17) and a AttributeValue of "450.000000". What is not happening is the automatic "recursion" back up the hierarchy. I understand why this is from the query supplied above, but I don't know how to do what is intended.
Please can someone help?
Final notes:
- This is a simplified version of the actual scenario which contains many, many containers with thousands of Items. I hope however I have supplied enough to obtain your kind help.
- There are also many AttributeGroups so it is key to select only those Attributes within the specified group.
Thank you in advance.