Solved

divide year across five segments

Posted on 2013-12-12
12
266 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 34

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Via a live example, show how to take different types of Oracle backups using RMAN.

821 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