Solved

divide year across five segments

Posted on 2013-12-12
12
267 Views
Last Modified: 2013-12-27
Hi,
How can i do this we can use dummy table so basic idea is divide current_year -1998 and distribute years among  five segments ( seq_1 to seq_5).

Result should be:


Sequence     count for (current-1998/3)
                         

seq_1                    1998-2000
seq_2                    2001-2003
seq_3                    2004-2006
seq_4                    2007-2009
seq_5                    2010-2013
0
Comment
Question by:sam2929
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 39715543
select sequence,count(*) from
(select case when extract(year from YOUR_DATE) in (1998,1999,2000) then 'seq_1'
when extract(year from YOUR_DATE) in (2001,2002,2003) then 'seq_2'
when extract(year from YOUR_DATE) in (2004, 2005,2006) then 'seq_3'
when extract(year from YOUR_DATE) in (2007,2008,2009) then 'seq_4'
when extract(year from YOUR_DATE) in (2010,2011,2012,2013) then 'seq_5'
end sequence
from YOUR_TABLE)
group by sequence
0
 

Author Comment

by:sam2929
ID: 39715586
problem is i want to make dates dynamic as next year it can be  as below.

can we do this without hard coding the years?

Can we use any system table as there is no table for this

seq_1                    1998-2000
seq_2                    2001-2003
seq_3                    2004-2006
seq_4                    2007-2009
seq_5                    2010-2014
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39715587
select sequence,count(*) from
(select case when extract(year from YOUR_DATE) in (1998,1999,2000) then 'seq_1'
when extract(year from YOUR_DATE) in (2001,2002,2003) then 'seq_2'
when extract(year from YOUR_DATE) in (2004, 2005,2006) then 'seq_3'
when extract(year from YOUR_DATE) in (2007,2008,2009) then 'seq_4'
else 'seq_5'
end sequence
from YOUR_TABLE)
group by sequence
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 74

Expert Comment

by:sdstuber
ID: 39715590
if you want to maintain ranges of 3 years each then it's not a problem you can simply

do

floor((year - 1998)/3)+1

but then you'd have 6 sequences, not 5
0
 

Author Comment

by:sam2929
ID: 39715595
i want to maintain 5 sequence date range can be disturbed even or odd
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39715598
the "else" version will do that.


1998-2009 will be spread between the first 4 sequences, everything else goes into sequence 5


I have no idea what you mean by "disturbed even or odd"
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39715603
another option is spread years approximately evenly across sequences, but not in order.


mod(year-1998,5)+1


1998 - 1
1999 - 2
2000 -3
2001 - 4
2002 - 5
2003 - 1
2004 - 2
2005 - 3
2006 - 4
2007- 5
2008 - 1
2009 - 2
2010 - 3
2011 - 4
2012 - 5
2013 - 1
2014 - 2

etc
0
 

Author Comment

by:sam2929
ID: 39715604
can we use sys tables instead of your_table

something like below

select sequence,count(*) from
(select case when extract(year from sysdate) in (1998,1999,2000) then 'seq_1'
when extract(year from sysdate) in (2001,2002,2003) then 'seq_2'
when extract(year from sysdate) in (2004.2005,2006) then 'seq_3'
when extract(year from sysdate) in (2007,2008,2009) then 'seq_4'
else 'seq_5'
end sequence
from sysdate)
group by sequence
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39715610
sysdate is a function not a table


if you want to use something from the data dictionary that will only give you counts if your dictionary has data going back to 1998


if you just want to see some sample data then create some....


WITH your_table
     AS (SELECT DATE '1998-01-01' + LEVEL * 123 your_date
           FROM DUAL
         CONNECT BY LEVEL < 100)
SELECT sequence, COUNT(*)
  FROM (SELECT CASE
                   WHEN EXTRACT(YEAR FROM your_date) IN (1998, 1999, 2000) THEN 'seq_1'
                   WHEN EXTRACT(YEAR FROM your_date) IN (2001, 2002, 2003) THEN 'seq_2'
                   WHEN EXTRACT(YEAR FROM your_date) IN (2004, 2005, 2006) THEN 'seq_3'
                   WHEN EXTRACT(YEAR FROM your_date) IN (2007, 2008, 2009) THEN 'seq_4'
                   ELSE 'seq_5'
               END
                   sequence
          FROM your_table)
GROUP BY sequence
ORDER BY sequence
0
 
LVL 35

Accepted Solution

by:
johnsone earned 500 total points
ID: 39716478
Looks like this should do what you are trying to do and will do it dynamically based on the start year of 1998 and end year of the current year.

WITH level_tbl (seq, cnt) 
     AS (SELECT MOD(LEVEL, 5) + 1, 
                Count(1) 
         FROM   dual 
         CONNECT BY 1997 + LEVEL <= To_number(To_char(SYSDATE, 'yyyy')) 
         GROUP  BY MOD(LEVEL, 5) + 1) 
SELECT seq, 
       1998 + Nvl((SELECT SUM(cnt) FROM level_tbl b WHERE b.seq < a.seq), 0) 
       beg_year, 
       1998 + Nvl((SELECT SUM(cnt) FROM level_tbl c WHERE c.seq < a.seq), 0) + 
       cnt - 1 
       end_year 
FROM   level_tbl a; 

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 39716883
To do this dynamically, you need to define the rules for how segments are to be distributed. Assuming you always want five segments always starting with 1998, how should 2025 (28 years) be distributed? Four segments of five years and one of eight? Two segments of five years and three of six? In the latter case should the six be the first three segments or the last three (or, perhaps even, every other segment)?
0
 
LVL 35

Expert Comment

by:Mark Geerlings
ID: 39722007
You need to give us more information on how you want the years to be divided, especially for future years.  Will the first four segments continue to have exactly three years each from 1998 - 2009, and the fifth segment will continue to grow?  Or, should at least one of the first four segments change a bit as we enter 2014 in a couple of weeks and then again when we get to 2015, etc.?

And no, you certainly cannot use "sysdate" to help you with this.  That is a function, not a table, and it always returns the current date/time value (which always includes the current year).
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question