1

I have a query which returns a column of values:

?????? ?? ?????? ??????? ?.?..xlsx
1028-13055 Single Patient Focus Wave 3.sav
2.xlsx
2011 BBQ (13Dec2013).sav
2014 Health IT Purchasing Intentions Survey Results.xlsx
2014 Safety Training and Safety Professionals Survey.sav

How do I count the number of occurrences based on the file extensions? In the above example, we have three rows for the xlsx extension and two for the sav extension?

Clockwork-Muse
  • 12,806
  • 6
  • 31
  • 45
ABCD
  • 7,914
  • 9
  • 54
  • 90

3 Answers3

2

Try this:

SELECT RIGHT(yourcolumnname,CHARINDEX('.', Reverse(yourcolumnname)) -1)
FROM yourtable

to isolate just the extension part and then count the occurrences

Milen
  • 8,697
  • 7
  • 43
  • 57
2

Please try:

SELECT colcnt, 
       Count(*) TotCnt 
FROM  (SELECT RIGHT(col, Charindex('.', Reverse(col)) - 1) ColCnt 
       FROM   yourtable)x 
GROUP  BY colcnt 
Hawk
  • 5,060
  • 12
  • 49
  • 74
TechDo
  • 18,398
  • 3
  • 51
  • 64
2

Try this

SELECT extension, 
       Count(*) AS ExtensionCount 
FROM   (SELECT RIGHT(name, Charindex('.', Reverse(name)) - 1) AS Extension 
        FROM   files) t 
GROUP  BY extension 

SQL fiddle

http://sqlfiddle.com/#!6/27269/5

Hawk
  • 5,060
  • 12
  • 49
  • 74
Urban Björkman
  • 2,095
  • 1
  • 13
  • 27