Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Is it possible to group by year & month in DB2

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
Cketha
Warnings : 1

New User


Joined: 19 May 2007
Posts: 22
Location: mumbai

PostPosted: Wed Jun 06, 2007 6:22 pm    Post subject: Is it possible to group by year & month in DB2
Reply with quote

Hi,
I have field in DB2 table defined with Date as a data type, it's having daily details like
2007-29-01
2007-20-01
2007-30-01,
2007-31-02
2007-31-02

in CCYY-DD-MM format

Is it possible to group by year & month without considering the DD
We want only one record per month.
Can you help regarding this?
Back to top
View user's profile Send private message

Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Wed Jun 06, 2007 6:46 pm    Post subject: Re: Group by month in DB2
Reply with quote

Cketha wrote:
Hi,
I have field in DB2 table defined with Date as a data type, it's having daily details like
2007-29-01
2007-20-01
2007-30-01,
2007-31-02
2007-31-02

in CCYY-DD-MM format

Is it possible to group by year & month without considering the DD
We want only one record per month.
Can you help regarding this?


You can do it with
Code:
SELECT YEAR_MONTH, SUM(AMOUNT) FROM
(SELECT SUBSTR(CHAR(DATE_COLUMN,ISO),1,7) AS YEAR_MONTH, AMOUNT FROM TABLE)
GROUP BY YEAR_MONTH
Back to top
View user's profile Send private message
Cketha
Warnings : 1

New User


Joined: 19 May 2007
Posts: 22
Location: mumbai

PostPosted: Wed Jun 06, 2007 6:52 pm    Post subject: Re: Group by month in DB2
Reply with quote

Craq Giegerich wrote:
Cketha wrote:
Hi,
I have field in DB2 table defined with Date as a data type, it's having daily details like
2007-29-01
2007-20-01
2007-30-01,
2007-31-02
2007-31-02

in CCYY-DD-MM format

Is it possible to group by year & month without considering the DD
We want only one record per month.
Can you help regarding this?


You can do it with
Code:
SELECT YEAR_MONTH, SUM(AMOUNT) FROM
(SELECT SUBSTR(CHAR(DATE_COLUMN,ISO),1,7) AS YEAR_MONTH, AMOUNT FROM TABLE)
GROUP BY YEAR_MONTH


Thank you.
It will work.
Back to top
View user's profile Send private message
Abirami.YN

New User


Joined: 16 Jul 2005
Posts: 15
Location: India - Chennai

PostPosted: Thu Jun 07, 2007 3:05 pm    Post subject:
Reply with quote

Hi

I am getting error for the above query. Error Message is

DSNT408I SQLCODE = -104, ERROR: ILLEGAL SYMBOL "<EMPTY>". SOME SYMBOLS THAT
MIGHT BE LEGAL ARE: CORRELATION NAME
DSNT418I SQLSTATE = 42601 SQLSTATE RETURN CODE
DSNT415I SQLERRP = DSNHSM10 SQL PROCEDURE DETECTING ERROR
DSNT416I SQLERRD = 0 0 0 -1 471 0 SQL DIAGNOSTIC INFORMATION
DSNT416I SQLERRD = X'00000000' X'00000000' X'00000000' X'FFFFFFFF'
X'000001D7' X'00000000' SQL DIAGNOSTIC INFORMATION


My Query:

SELECT YY1MM, SUM(PURCHASE_AMT)
FROM (SELECT SUBSTR(CHAR(ARCH_DATE,ISO),1,7) AS YY1MM,
PURCHASE_AMT FROM PURTBL)
GROUP BY YY1MM;


Could you plz help me.

Thank You.
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Thu Jun 07, 2007 3:21 pm    Post subject:
Reply with quote

Quote:
SELECT YY1MM, SUM(PURCHASE_AMT)
FROM (SELECT SUBSTR(CHAR(ARCH_DATE,ISO),1,7) AS YY1MM,
PURCHASE_AMT FROM PURTBL)
GROUP BY YY1MM;

try
Code:
SELECT YY1MM, SUM(PURCHASE_AMT)
FROM (SELECT SUBSTR(CHAR(ARCH_DATE,ISO),1,7) AS YY1MM,
PURCHASE_AMT FROM PURTBL) x
GROUP BY YY1MM;
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Sorting group data rajella DFSORT/ICETOOL 4 Sun Jan 22, 2017 11:32 pm
No new posts Syncsort Help to group fields sudhakarraju SYNCSORT 6 Thu Dec 29, 2016 1:38 am
This topic is locked: you cannot edit posts or make replies. How to pass the previous month date i... Suganya87 DFSORT/ICETOOL 5 Mon Oct 31, 2016 4:13 pm
No new posts Wishing everyone a very Happy Diwali ... Rohit Umarjikar General Talk & Fun Stuff 1 Sat Oct 29, 2016 1:54 am
No new posts How to get previous month in SYMNAMES Suganya87 SYNCSORT 9 Fri Oct 28, 2016 1:37 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us