MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

count of rows by month using a Timestamp field

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
Celeste Locklin
Beginner


Joined: 04 May 2006
Posts: 26
Topics: 10
Location: USA

PostPosted: Thu Jun 05, 2008 6:48 pm    Post subject: count of rows by month using a Timestamp field Reply with quote

Is there an "easy" way to count the number of rows per month in a timestamp field on a DB2 table?
I want to count how many rows posted in jan, how many in feb, mar, etc.
for a given year.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Thu Jun 05, 2008 7:59 pm    Post subject: Reply with quote

Celeste Locklin,

Try this untested sql which will you the desired results
Code:

SELECT A.MNTH_NAME
      ,COUNT(*)
  FROM (SELECT MONTH(your timestamp col) AS MNTH_NAME
          FROM TABLE
         WHERE YEAR(Your timestamp col) = 2008) A
 GROUP BY A.MNTH_NAME
       ;   


Hope this helps...

Cheers

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Jun 06, 2008 1:22 pm    Post subject: Reply with quote

How about this.

Code:


Select Month(Timestamp), Count(*)
from  table
where Year(timestamp)  = 2008
group by Month(Timestamp)

_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Fri Jun 06, 2008 1:25 pm    Post subject: Reply with quote

NASCAR9,

I believe you canNOT do it that as the Group by clause needs Existing column name in the table.

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
CraigG
Intermediate


Joined: 02 May 2007
Posts: 202
Topics: 0
Location: Viginia, USA

PostPosted: Fri Jun 06, 2008 2:31 pm    Post subject: Reply with quote

Code:

SELECT MONTH_TIMESTAMP, COUNT(*)
FROM (SELECT MONTH(TIMESTAMP) AS MONTH_TIMESTAMP
FROM TABLE WHERE YEAR(TIMESTAMP) = 2008) B
GROUP BY MONTH_TIMESTAMP;
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Jun 06, 2008 2:47 pm    Post subject: Reply with quote

kolusu wrote:
NASCAR9,

I believe you canNOT do it that as the Group by clause needs Existing column name in the table.

Kolusu


I just ran this
Code:
Month(lastupdt)  as MM, Count(*)  as total
FROM "HOURS".PREMCHG
where Year(lastupdt)  = 2004
group by Month(lastupdt))


and here are the results
MM total

11 37
10 83
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Fri Jun 06, 2008 2:59 pm    Post subject: Reply with quote

NASCAR9,

Is this on z/OS? Question
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Jun 06, 2008 3:01 pm    Post subject: Reply with quote

kolusu wrote:
NASCAR9,

Is this on z/OS? Question


Yes, V8 DB2, not sure ov the zOS version..
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Jun 06, 2008 3:10 pm    Post subject: Reply with quote

The zOS is 1.8
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
CraigG
Intermediate


Joined: 02 May 2007
Posts: 202
Topics: 0
Location: Viginia, USA

PostPosted: Fri Jun 06, 2008 3:14 pm    Post subject: Reply with quote

Code:

SELECT MONTH_TIMESTAMP, COUNT(*)                   
FROM (SELECT MONTH(TIMESTAMP) AS MONTH_TIMESTAMP   
FROM TABLE                                       
WHERE YEAR(TIMESTAMP) = 2008) B                     
GROUP  BY MONTH_TIMESTAMP;                         


Works fine for me with ZOS DB v7. The group by has an existing column name it is created in the inner select. Instead of saying it won't work prove it.
Back to top
View user's profile Send private message
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Fri Jun 06, 2008 3:24 pm    Post subject: Reply with quote

CraigG wrote:
Code:

SELECT MONTH_TIMESTAMP, COUNT(*)                   
FROM (SELECT MONTH(TIMESTAMP) AS MONTH_TIMESTAMP   
FROM TABLE                                       
WHERE YEAR(TIMESTAMP) = 2008) B                     
GROUP  BY MONTH_TIMESTAMP;                         


Works fine for me with ZOS DB v7. The group by has an existing column name it is created in the inner select. Instead of saying it won't work prove it.


CraigG, Was this directed at me? I don't think any of the posts above were negative in anyway. I pointed out another way to get the results. In SQL usually it can be accomplished more than one way.
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
Celeste Locklin
Beginner


Joined: 04 May 2006
Posts: 26
Topics: 10
Location: USA

PostPosted: Thu Jul 24, 2008 7:01 pm    Post subject: Reply with quote

excellent ! thanks to all !!
Back to top
View user's profile Send private message
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Fri Jul 25, 2008 11:58 am    Post subject: Reply with quote

If you want to select multiple years, try this:

Code:
SELECT  CCYY_MM, COUNT(*)                   
  FROM (  SELECT  SUBSTR(CHAR(<timestamp_colnm>),1,7) AS CCYY_MM
            FROM  <table_nm>
           WHERE  <predicates>  ) as temp_tbl
GROUP  BY CCYY_MM
ORDER BY 1
for fetch only with ur

CCYY_MM 2         
------- -----------
1980-01           1
2003-08           1
2004-11           2
2006-10           1
2007-04           1
2008-06           1
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group