views:

69

answers:

2

Given a table that has a column of string "timestamps" (yyyyMMddHHmmssSSS format), I want to substring the first 8 characters, and get a count of how many rows have that substring, grouping the results.

Sample data...

TIMESTAMP
20100802123456123
20100803123456123
20100803123456123
20100803123456123
20100804123456123
20100805123456123
20100805123456123
20100805123456123
20100805123456123
20100806123456123
20100807123456123
20100807123456123

...and expected results...

SUBSTRING, COUNT
20100802, 1
20100803, 3
20100804, 1
20100805, 4
20100806, 1
20100807, 2

I know this should be easy, but I'm not having any luck at the moment.

+6  A: 

I don't have a database to test with, but it seems like you are looking for

select
  substr(timestamp, 1, 8),
  count(*)
from
  my_table
group by
  substr(timestamp, 1, 8);
Brandon Horsley
I think that might do the trick. Thank you very much. I was trying to do substring() as YMD and then group by YMD.
krick
A: 

Thank you very much.

Kashan Ahmad