I have a table that looks like this
CREATE TABLE [dbo].[CNF_StatisticalGroup]
(
[StatisticalGroupId] [int] IDENTITY(1,1) NOT NULL,
[StatisticalGroupCode] [varchar](10) NOT NULL,
[StatisticalGroupDescription] [nvarchar](100) NULL,
[ParentId] [int] NULL
)
With ParentID
being self referencing.
My data for instance my look like this:
18 Consumer Consumer Direct NULL
19 Win Walkin 18
20 OL Online 18
21 100 100/F 19
22 ME Ticket Centre 19
As you can see that Ticket Centre belongs under Walkin and Walkin belongs under Consumer Direct.
I need to be able to count the number of items under a level and add it to the above level and so on.
For instance.
- Ticket Centre has a count of 2
- Walkin has an individual count of 3
- Consumer Direct has an individual count of 1
So it looks like this
Consumer Direct 1
Walkin 3
Ticket Center 2
What I need is to add the ticket center 2 to the walkin of 3 so that Walking has a count of 5 so something like this
Description Individual Count Cummulative Count
-----------------------------------------------------------
Consumer Direct 1 6
Walkin 3 5
Ticket Center 2 2
Can someone please help me create this in a SQL statement or a view or a stored procedure?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…