Author |
Topic |
Gudea
Starting Member
18 Posts |
Posted - 2012-05-29 : 12:11:41
|
Hi.I have a table that stores Folders information from file system.That hierarchy stores files lenght inside, and two bit fields to know if there is any physical inconsistency (logic folder not existing physically) or logical inconsistency (physic folder not stored in database yet).I need to build a tree in which parents shows propagated information, I mean, total sum of filesizes, and if there is/are any Folder with some inconsistency in between childs, or sub-childs.The table is:CREATE TABLE [dbo].[Folder]( [Folder_Id] [int] NOT NULL, [Folder_Name] [varchar](200) COLLATE Modern_Spanish_CI_AS NOT NULL, [Folder_Folder_Id] [int] NULL, [Folder_FilesSize] [int] NOT NULL, [Folder_PhysicError] [bit] NULL, [Folder_LogicError] [bit] NULL, CONSTRAINT [PK_Folder] PRIMARY KEY CLUSTERED ( [Folder_Id] ASC)) Some data:INSERT [dbo].[Folder] ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (1, N'RootFolder', 0, 0, 0, 0)INSERT [dbo].[Folder] ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (2, N'Documents', 1, 210342, 0, 0)INSERT [dbo].[Folder] ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (3, N'Programs', 1, 339091, 0, 0)INSERT [dbo].[Folder] ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (4, N'Letters', 2, 0, 0, 0)INSERT [dbo].[Folder] ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (5, N'Received', 4, 32827, 0, 0)INSERT [dbo].[Folder] ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (6, N'Sent', 4, 736222, 0, 0)INSERT [dbo].[Folder] ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (7, N'Productivity', 3, 9978373, 0, 0)INSERT [dbo].[Folder] ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (8, N'Games', 3, 1200287, 0, 0)INSERT [dbo].[Folder] ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (9, N'OfficeAddOns', 7, 1337890, 0, 0)INSERT [dbo].[Folder] ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (10, N'FreeOnes', 9, 0, 1, 0)INSERT [dbo].[Folder] ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (11, N'NewReleases2012', 9, 289309, 0, 1) The basic query I have is like:WITH GlobalTable(Folder_id, Folder_Name , Folder_Folder_id, FullPath,DeepLevel,Folder_PhysicError,Folder_LogicError) AS ( SELECT Folder_id, Folder_Name , Folder_Folder_id, CAST(Folder_Name AS VARCHAR(1000)) AS "FullPath", 0 As DeepLevel, ISNULL(Folder_PhysicError,0) As Folder_PhysicError, ISNULL(Folder_LogicError,0) As Folder_LogicError FROM Folder WHERE Folder_Folder_Id = 0 UNION ALL SELECT t.Folder_id, t.Folder_Name, t.Folder_Folder_Id, CAST((a.FullPath + ''/'' + t.Folder_Name) AS VARCHAR(1000)) AS "FullPath", a.DeepLevel + 1 As DeepLevel, ISNULL(t.Folder_PhysicError,0) As Folder_PhysicError, ISNULL(t.Folder_LogicError,0) As Folder_LogicError FROM Folder AS t JOIN GlobalTable AS a ON t.Folder_Folder_Id = a.Folder_Id )SELECTFolder_id,DeepLevel,Folder_Name,Folder_Folder_Id,Folder_LogicError,Folder_PhysicErrorFROM GlobalTableORDER BY Folder_Folder_Id Which returns plain recursive results:1 0 RootFolder 0 0 02 1 Documents 1 0 03 1 Programs 1 0 04 2 Letters 2 0 07 2 Productivity 3 0 08 2 Games 3 0 05 3 Received 4 0 06 3 Sent 4 0 09 3 OfficeAddOns 7 0 010 4 FreeOnes 9 0 111 4 NewReleases2012 9 1 0 But I'd like to add the mentiones information.As you can see the second level Folder Documents doesn't have any logical of physical error, but I 'd like yo know that some child folders have them, so user can expand Folders to see and solve problems.Also I would like to SUM up files sizes.Any help?ThanksRegards |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-29 : 12:21:02
|
so what should be the output you expect?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Gudea
Starting Member
18 Posts |
Posted - 2012-05-29 : 13:02:54
|
Hi visakhI get:Id Level Name Parent LogError PhyError1 0 RootFolder 0 0 02 1 Documents 1 0 03 1 Programs 1 0 04 2 Letters 2 0 07 2 Productivity 3 0 08 2 Games 3 0 05 3 Received 4 0 06 3 Sent 4 0 09 3 OfficeAddOns 7 0 010 4 FreeOnes 9 0 111 4 NewReleases2012 9 1 0 And I would like to get three additional columnsChildLogicErrors, ChildPhysicErrors, TotalSize:(deteted Level column for space saving)Id Name Parent LogError PhyError ChLE ChPE TZ1 RootFolder 0 0 0 1 1 [SUM size]2 Documents 1 0 0 0 0 [SUM size]3 Programs 1 0 0 1 1 [SUM size]4 Letters 2 0 0 0 0 [SUM size]7 Productivity 3 0 0 1 1 [SUM size]8 Games 3 0 0 0 0 [SUM size]5 Received 4 0 0 0 0 [SUM size]6 Sent 4 0 0 0 0 [SUM size]9 OfficeAddOns 7 0 0 1 1 [SUM size]10 FreeOnes 9 0 1 0 0 [SUM size]11 NewReleases2012 9 1 0 0 0 [SUM size] |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-05-29 : 13:54:42
|
haven't tested it but should be something like thisWITH GlobalTable(Folder_id, Folder_Name , Folder_Folder_id, FullPath,DeepLevel,Folder_PhysicError,Folder_LogicError) AS ( SELECT Folder_id, Folder_Name , Folder_Folder_id, CAST(Folder_Name AS VARCHAR(1000)) AS "FullPath", 0 As DeepLevel, ISNULL(Folder_PhysicError,0) As Folder_PhysicError, ISNULL(Folder_LogicError,0) As Folder_LogicError , root=Folder_id, seq=1 FROM Folder WHERE Folder_Folder_Id = 0 UNION ALL SELECT t.Folder_id, t.Folder_Name, t.Folder_Folder_Id, CAST((a.FullPath + ''/'' + t.Folder_Name) AS VARCHAR(1000)) AS "FullPath", a.DeepLevel + 1 As DeepLevel, ISNULL(t.Folder_PhysicError,0) As Folder_PhysicError, ISNULL(t.Folder_LogicError,0) As Folder_LogicError root=root, seq=seq+1 FROM Folder AS t JOIN GlobalTable AS a ON t.Folder_Folder_Id = a.Folder_Id )SELECTFolder_id,DeepLevel,Folder_Name,Folder_Folder_Id,Folder_LogicError,Folder_PhysicError ,le = (select SUM(convert(int,Folder_LogicError)) from GlobalTable t2 where t.root = t2.root and t2.seq >=t.seq),pe = (select SUM(convert(int,Folder_PhysicError)) from GlobalTable t2 where t.root = t2.root and t2.seq >=t.seq)FROM GlobalTable tORDER BY Folder_Folder_Id You can use a similar thing for the sizes==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-05-29 : 14:16:06
|
noticed you gave data and I made a bit of a blunderdeclare @folder TABLE( [Folder_Id] [int] NOT NULL, [Folder_Name] [varchar](200) COLLATE Modern_Spanish_CI_AS NOT NULL, [Folder_Folder_Id] [int] NULL, [Folder_FilesSize] [int] NOT NULL, [Folder_PhysicError] [bit] NULL, [Folder_LogicError] [bit] NULL)INSERT @folder([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (1, N'RootFolder', 0, 0, 0, 0)INSERT @folder ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (2, N'Documents', 1, 210342, 0, 0)INSERT @folder ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (3, N'Programs', 1, 339091, 0, 0)INSERT @folder ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (4, N'Letters', 2, 0, 0, 0)INSERT @folder ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (5, N'Received', 4, 32827, 0, 0)INSERT @folder ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (6, N'Sent', 4, 736222, 0, 0)INSERT @folder ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (7, N'Productivity', 3, 9978373, 0, 0)INSERT @folder ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (8, N'Games', 3, 1200287, 0, 0)INSERT @folder ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (9, N'OfficeAddOns', 7, 1337890, 0, 0)INSERT @folder ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (10, N'FreeOnes', 9, 0, 1, 0)INSERT @folder ([Folder_Id], [Folder_Name], [Folder_Folder_Id], [Folder_FilesSize], [Folder_PhysicError], [Folder_LogicError]) VALUES (11, N'NewReleases2012', 9, 289309, 0, 1);WITH GlobalTable--Folder_id, Folder_Name , Folder_Folder_id, FullPath,DeepLevel,Folder_PhysicError,Folder_LogicError) AS ( SELECT Folder_id, Folder_Name , Folder_Folder_id, CAST(Folder_Name AS VARCHAR(1000)) AS "FullPath", 0 As DeepLevel, ISNULL(Folder_PhysicError,0) As Folder_PhysicError, ISNULL(Folder_LogicError,0) As Folder_LogicError , root=convert(varchar(max),Folder_id)+',', seq=1 FROM @Folder WHERE Folder_Folder_Id = 0 UNION ALL SELECT t.Folder_id, t.Folder_Name, t.Folder_Folder_Id, CAST((a.FullPath + '/' + t.Folder_Name) AS VARCHAR(1000)) AS "FullPath", a.DeepLevel + 1 As DeepLevel, ISNULL(t.Folder_PhysicError,0) As Folder_PhysicError, ISNULL(t.Folder_LogicError,0) As Folder_LogicError, root=root+convert(varchar(max),t.Folder_id)+',', seq=seq+1 FROM @Folder AS t JOIN GlobalTable AS a ON t.Folder_Folder_Id = a.Folder_Id ) SELECT root,Folder_id,DeepLevel,Folder_Name,Folder_Folder_Id,Folder_LogicError,Folder_PhysicError ,le = (select sum(convert(int,Folder_LogicError)) from GlobalTable t2 where ','+t2.root like '%,' + convert(varchar(20),t.folder_id) + ',%' and t2.seq >=t.seq),pe = (select sum(convert(int,Folder_PhysicError)) from GlobalTable t2 where ','+t2.root like '%,' + convert(varchar(20),t.folder_id) + ',%' and t2.seq >=t.seq)FROM GlobalTable tORDER BY Folder_Folder_Id ==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-29 : 14:30:36
|
quote: Originally posted by Gudea Hi visakhI get:Id Level Name Parent LogError PhyError1 0 RootFolder 0 0 02 1 Documents 1 0 03 1 Programs 1 0 04 2 Letters 2 0 07 2 Productivity 3 0 08 2 Games 3 0 05 3 Received 4 0 06 3 Sent 4 0 09 3 OfficeAddOns 7 0 010 4 FreeOnes 9 0 111 4 NewReleases2012 9 1 0 And I would like to get three additional columnsChildLogicErrors, ChildPhysicErrors, TotalSize:(deteted Level column for space saving)Id Name Parent LogError PhyError ChLE ChPE TZ1 RootFolder 0 0 0 1 1 [SUM size]2 Documents 1 0 0 0 0 [SUM size]3 Programs 1 0 0 1 1 [SUM size]4 Letters 2 0 0 0 0 [SUM size]7 Productivity 3 0 0 1 1 [SUM size]8 Games 3 0 0 0 0 [SUM size]5 Received 4 0 0 0 0 [SUM size]6 Sent 4 0 0 0 0 [SUM size]9 OfficeAddOns 7 0 0 1 1 [SUM size]10 FreeOnes 9 0 1 0 0 [SUM size]11 NewReleases2012 9 1 0 0 0 [SUM size]
this you can do it inside CTE itself. Add two columns inside CTE to get counts of LE and PEsstart from reverse order ie child nodes and traverse till root and you will get counts against root nodes------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-29 : 21:53:19
|
see how you can accomodate it inline WITH GlobalTable AS ( SELECT f.Folder_id, f.Folder_Name , f.Folder_Folder_id, CAST(f.Folder_Name AS VARCHAR(1000)) AS "FullPath", 0 As DeepLevel, ISNULL(f.Folder_PhysicError,0) As Folder_PhysicError, ISNULL(f.Folder_LogicError,0) As Folder_LogicError, CAST(ISNULL(f.Folder_PhysicError,0) AS int) As Full_Folder_PhysicError, CAST(ISNULL(f.Folder_LogicError,0) AS int) As Full_Folder_LogicError, f.Folder_FilesSize FROM Folder f LEFT JOIN Folder AS f1 ON f1.Folder_Folder_Id = f.Folder_Id WHERE f1.Folder_Id IS NULL UNION ALL SELECT t.Folder_id, t.Folder_Name, t.Folder_Folder_Id, CAST((a.FullPath + '/' + t.Folder_Name) AS VARCHAR(1000)) AS "FullPath", a.DeepLevel + 1 As DeepLevel, ISNULL(t.Folder_PhysicError,0) As Folder_PhysicError, ISNULL(t.Folder_LogicError,0) As Folder_LogicError, a.Full_Folder_PhysicError + CAST(ISNULL(t.Folder_PhysicError,0) AS int), a.Full_Folder_LogicError + CAST(ISNULL(t.Folder_LogicError,0)AS int), a.Folder_FilesSize + t.Folder_FilesSize FROM Folder AS t JOIN GlobalTable AS a ON t.Folder_Id = a.Folder_Folder_Id )SELECTFolder_id,--DeepLevel,Folder_Name,Folder_Folder_Id,Folder_LogicError,Folder_PhysicError,SUM(Folder_FilesSize) AS [Size],SUM(Full_Folder_PhysicError) - Folder_PhysicError AS Ch_PE,SUM(Full_Folder_LogicError) - Folder_LogicError AS Ch_LEFROM GlobalTableGROUP BY Folder_id,Folder_Name,Folder_Folder_Id,Folder_LogicError,Folder_PhysicErrorORDER BY Folder_Folder_Idoutput-------------------------------------------------------------Folder_id Folder_Name Folder_Folder_Id Folder_LogicError Folder_PhysicError Size Ch_PE Ch_LE1 RootFolder 0 0 0 26329128 1 12 Documents 1 0 0 1189733 0 03 Programs 1 0 0 25139395 1 14 Letters 2 0 0 0 769049 0 07 Productivity 3 0 0 22921835 1 18 Games 3 0 0 1200287 0 05 Received 4 0 0 32827 0 06 Sent 4 0 0 736222 0 09 OfficeAddOns 7 0 0 2965089 1 110 FreeOnes 9 0 1 0 0 011 NewReleases2012 9 1 0 289309 0 0 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Gudea
Starting Member
18 Posts |
Posted - 2012-05-30 : 09:51:31
|
ThanksI tested visakh solution. It is returning only some folders with don't know which criteria, all with DeepLevel 0.8 Games 3 0 05 Received 4 0 06 Sent 4 0 010 FreeOnes 9 0 111 NewReleases2012 9 1 0 If I take out the JOIN in anchor query, then all folders are shown.The size is acumulated downwards, so lower subfolder levels acumulate parent's folders sized, since upward accumulation should happen to totalize subfolders sizes.Id Lvl Name Parent PhE LoE Size1 0 RootFolder 0 0 0 02 1 Documents 1 0 0 2103423 1 Programs 1 0 0 3390914 2 Letters 2 0 0 2103427 2 Productivity 3 0 0 103174648 2 Games 3 0 0 15393785 3 Received 4 0 0 2431696 3 Sent 4 0 0 9465649 3 OfficeAddOns 7 0 0 1165535410 4 FreeOnes 9 0 1 1165535411 4 NewReleases2012 9 1 0 11944663 Complete script brings this:Id Lvl Name Parent PhE LoE Size Ch_PhE Ch_LoE8 0 Games 3 0 0 1200287 0 05 0 Received 4 0 0 32827 0 06 0 Sent 4 0 0 736222 0 010 0 FreeOnes 9 0 1 0 0 011 0 NewReleases2012 9 1 0 289309 0 0 I'll test Nigel solution. |
 |
|
Gudea
Starting Member
18 Posts |
Posted - 2012-05-30 : 10:17:11
|
Great.Nigel solution does propagate errors upwards. Now I have to deal with sizes.Lets try...Thanks. |
 |
|
Gudea
Starting Member
18 Posts |
Posted - 2012-05-30 : 10:36:54
|
Sorry.I tested again visakh solution. It was my mistake. It does bubble up errors also.Still having problems with sizes.Manually calculated sizes (in Excel) should be like: Size TotalSizeROOT 14124341 DOCUMENTS 210342 979391 LETTERS 0 769049 SENT 736222 736222 RECEIVED 32827 32827 PROGRAMS 339091 13144950 PRODUCTIVITY 9978373 11605572 OFFICEADDONS 1337890 1627199 FREEONES 0 0 NEWRELEASES2012 289309 289309 GAMES 1200287 1200287 And I am getting from your query:Id Name Parent PhE LoE Size Ch_PhE Ch_LoE1 RootFolder 0 0 0 26329128 1 12 Documents 1 0 0 1189733 0 03 Programs 1 0 0 25139395 1 14 Letters 2 0 0 769049 0 07 Productivity 3 0 0 22921835 1 18 Games 3 0 0 1200287 0 05 Received 4 0 0 32827 0 06 Sent 4 0 0 736222 0 09 OfficeAddOns 7 0 0 2965089 1 110 FreeOnes 9 0 1 0 0 011 NewReleases2012 9 1 0 289309 0 0 Also, I don't know why I cant include again DeepLevel column you commented in execution query.Regards |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-30 : 16:42:36
|
quote: Originally posted by Gudea Sorry.I tested again visakh solution. It was my mistake. It does bubble up errors also.Still having problems with sizes.Manually calculated sizes (in Excel) should be like: Size TotalSizeROOT 14124341 DOCUMENTS 210342 979391 LETTERS 0 769049 SENT 736222 736222 RECEIVED 32827 32827 PROGRAMS 339091 13144950 PRODUCTIVITY 9978373 11605572 OFFICEADDONS 1337890 1627199 FREEONES 0 0 NEWRELEASES2012 289309 289309 GAMES 1200287 1200287 And I am getting from your query:Id Name Parent PhE LoE Size Ch_PhE Ch_LoE1 RootFolder 0 0 0 26329128 1 12 Documents 1 0 0 1189733 0 03 Programs 1 0 0 25139395 1 14 Letters 2 0 0 769049 0 07 Productivity 3 0 0 22921835 1 18 Games 3 0 0 1200287 0 05 Received 4 0 0 32827 0 06 Sent 4 0 0 736222 0 09 OfficeAddOns 7 0 0 2965089 1 110 FreeOnes 9 0 1 0 0 011 NewReleases2012 9 1 0 289309 0 0 Also, I don't know why I cant include again DeepLevel column you commented in execution query.Regards
can you tell me how you calculated those manual values? if its a simple sum then it should give value i posted.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Gudea
Starting Member
18 Posts |
Posted - 2012-05-31 : 11:20:26
|
Adding values in excel.Letters has Sent folder (736222) and Received (32827). As 736222 + 32827 = 769049 and Letters has no files inside itself Letters folders total size is 769049. (this size is correct in your results), but...Documents has files for another 210342 bytes.Added to Letters subfolders size: 210342 + 769049 = 979391.Documents Folder should be 979391 bytes heavy, and in your results is about 1189733.For the Programs branch, we have:Freeones (0 buyes) + NewReleases (289309).OfficeAddons has files for 1337890, plus Freeones and Nuereleases Subfoldes (289309) = 1627199. This is OfficeAddOnss size, but in your results it weights 2965089.Productivity has another 9978373 bytes. Plus OfficeAddons totals 11605572 bytes. In your results Productivity has 22921835 bytes.Programs has files for 339091, and Games for 1200287 bytes.Added Programss files, plus Games, plus Productivity (11605572) totals 13144950 bytes for Programs.Total Root folders size is 13144950 + 979391 = 14124341.In your results total size for Root is 26329128Just as if it were regular filesystem content size calculation.Folders can have files and subfolders inside.It looks like your filesystem were infected by a kind of file growing worm virus ;-) LOL.I cant make it to work. This could be calculated at business classes level, but I would have to build a huge collection hierarchy of folders objects just to show a flat level of it with sizes calculated and totalized.ThanksRegards |
 |
|
|
|
|