0

I have a table with a list of folders that looks like this:

Path                    Size
C:\ParentFolder\A       123
C:\ParentFolder\A\B     442434
C:\ParentFolder\A\B\C   13413412
C:\ParentFolder\D       2422341234
C:\ParentFolder\D\E     3342
C:\ParentFolder\D\E\F   2
C:\ParentFolder\D\E\G   2
...

I'm looking for some combination of SUM, GROUP BY, and PATINDEX/LTRIM/SUBSTRING/etc. which would give me back this:

Path                    SumSize
C:\ParentFolder\A       13855969
C:\ParentFolder\D       2422344580
...

C:\ParentFolder is a known prefix, but A,D,etc. are variable folder names. Do I need to write a function to accomplish that or can I use some combination of string functions?

2
  • What sql-version are you using? Commented Jun 7, 2012 at 14:18
  • One level down? Yes, I want to summarize A and everything under A. D and everything under D and nothing else. The trick is I don't know what A and D are until runtime. Version? SQL 2008R2 Commented Jun 7, 2012 at 14:26

5 Answers 5

1
select r.Path, sum(Size) as SumSize
from MyTable m
inner join (
    select Path
    from MyTable 
    where charindex('\', Path, len('C:\ParentFolder\') + 1) = 0 
) r on charindex(r.Path, m.Path, 0) = 1
group by r.Path

SQL Fiddle example here

Sign up to request clarification or add additional context in comments.

Comments

0

Starting with your testing set,

CREATE TABLE #MyTable (Folder varchar(100) not null, Size bigint not null)

INSERT #MyTable values
  ('C:\ParentFolder\A'     ,  123)
 ,('C:\ParentFolder\A\B'   ,  442434)
 ,('C:\ParentFolder\A\B\C' ,  13413412)
 ,('C:\ParentFolder\D'     ,  2422341234)
 ,('C:\ParentFolder\D\E'   ,  3342)
 ,('C:\ParentFolder\D\E\F' ,  2)
 ,('C:\ParentFolder\D\E\G' ,  2)

you'd first determine what folders you want to summarize. I do so here by loading them into a temp table:

DECLARE @Targets table (Folder varchar(100) not null)
INSERT @Targets values
  ('C:\ParentFolder\A')
 ,('C:\ParentFolder\D')

From here it's easy, using the like clause:

SELECT ta.Folder, sum(Size) TotalSize
 from @Targets ta
  left outer join #MyTable mt
   on mt.Folder like ta.Folder + '%'
 group by ta.Folder

Complications may ensue if your folders contain reserved characters used by the like clause: % _ ] [ and a few others.

2 Comments

I think it will give worg result due to the duplication when using like operator
This solution was the cleanest. The only adjustment I had to make was change INSERT @Targets values ('C:\ParentFolder\A') ,('C:\ParentFolder\D') Into a dynamic query that would discover the folders at runtime.
0

Assuming that there is always an entry for the highest level dir (i.e if there is a c:\xxx\yyy\zzz there will always be a c:\xxx\yyy how about

;with roots (root) as (
     select distinct
        path + '\' 
     from 
        thetable
     where 
        --only include paths with 2 x \
        len(path) - 2 = len(replace(path, '\', '')) 
)
select
    roots.root,
    sum(thetable.size)
from 
    roots
inner join 
    thetable on left(thetable.path + '\', len(roots.root)) = roots.root 
group by
    roots.root

Comments

0

--If the folder name is always one character

select 
LEFT(folder,CHARINDEX('r\',folder)+2) as folder_group
,SUM(size) as sumsize
from #mytable
GROUP BY
LEFT(folder,CHARINDEX('r\',folder)+2)

--If the folder name has a variable length

select 
CASE WHEN CHARINDEX('\',folder,CHARINDEX('\',folder,CHARINDEX('\',folder)+1)+1) = 0 THEN folder
    ELSE LEFT(folder,CHARINDEX('\',folder,CHARINDEX('\',folder,CHARINDEX('\',folder)+1)+1) -1) END AS folder_group
,SUM(size) as sumsize
from #mytable
GROUP BY
CASE WHEN CHARINDEX('\',folder,CHARINDEX('\',folder,CHARINDEX('\',folder)+1)+1) = 0 THEN folder
    ELSE LEFT(folder,CHARINDEX('\',folder,CHARINDEX('\',folder,CHARINDEX('\',folder)+1)+1) -1) END 

Comments

0
 select path, (select sum(Size) 
               from Paths p2 where p2.Path like p1.Path+'%') as total
 from Paths p1
 where charIndex('\',Path, len('C:\ParentFolder\')+1) = 0

1 Comment

You are making the assumption that immediate children names will only be one character long, "but A,D,etc. are variable folder names."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.