DB2 Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDB2 Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old February 8th, 2007, 04:11 PM
ETL_DEV ETL_DEV is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 2 ETL_DEV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 41 sec
Reputation Power: 0
Counter in a Sql statement

Hi,

Given a column payment_type, where payment type varies, I need to count each time when the value of the payment_type changes and display the total count in the end.

For example,
payment_typ is 1 1 2 2 2 2 2 3 3 3 3 3 4. Total count should be 3.

Summary:I need to traverse the values, capture the change and increment the counter.

So far, I have this:

with data_set_1(payment_type) as
(
values 1,2,2,3,3,4,4
)
select sum(case)
from
(select payment_type, case payment_type
when LAG(payment_type then 0
else 1
end case
from data_set_1)as counter

I have a problem with LAG function (which gets the previous value of payment_type). It works with Oracle but not with DB2. Is thier any function equivalent to LAG in DB2? Or may there is another way to get the previous value. What am I missing here?

Any help is appreciated.

Thanks!

Reply With Quote
  #2  
Old February 20th, 2007, 09:05 AM
LKBrwn_DBA LKBrwn_DBA is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 294 LKBrwn_DBA User rank is Sergeant (500 - 2000 Reputation Level)LKBrwn_DBA User rank is Sergeant (500 - 2000 Reputation Level)LKBrwn_DBA User rank is Sergeant (500 - 2000 Reputation Level)LKBrwn_DBA User rank is Sergeant (500 - 2000 Reputation Level)LKBrwn_DBA User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 16 h 48 m 15 sec
Reputation Power: 9
Cool


Try this:
Code:
With Data_Set_1(Payment_Type) As 
(
Values 1,2,2,3,3,4,4
)
Select Count(Distinct(Payment_Type)) - 1 As Count_Chg
  From Data_Set_1;



Last edited by LKBrwn_DBA : February 21st, 2007 at 08:08 AM.

Reply With Quote
  #3  
Old February 21st, 2007, 07:43 PM
ETL_DEV ETL_DEV is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 2 ETL_DEV User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 41 sec
Reputation Power: 0
Hi, Thanks for the response but I forgot to mention that payment_type can randomly re-occur multiple times.

example: 1,2,2,3,3,4,4,4,5,2,3

You solution will report a count of 4 where as the payment_type has changed 6 times.

Correct answer should be 6

Reply With Quote
  #4  
Old February 23rd, 2007, 10:27 AM
LKBrwn_DBA LKBrwn_DBA is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 294 LKBrwn_DBA User rank is Sergeant (500 - 2000 Reputation Level)LKBrwn_DBA User rank is Sergeant (500 - 2000 Reputation Level)LKBrwn_DBA User rank is Sergeant (500 - 2000 Reputation Level)LKBrwn_DBA User rank is Sergeant (500 - 2000 Reputation Level)LKBrwn_DBA User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 16 h 48 m 15 sec
Reputation Power: 9
Thumbs down There is no "correct" answer

Quote:
Originally Posted by ETL_DEV
Hi, Thanks for the response but I forgot to mention that payment_type can randomly re-occur multiple times.

example: 1,2,2,3,3,4,4,4,5,2,3

You solution will report a count of 4 where as the payment_type has changed 6 times.

Correct answer should be 6

Your requirement seems a bit flaky, some RDBMS may NOT return the rows in the same order they were inserted, therefore the “count” of changes may vary each time you execute the query and/or between different RDBMS.



Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDB2 Development > Counter in a Sql statement


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway