Solved

divide year across five segments

Posted on 2013-12-12
12
264 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 73

Expert Comment

by:sdstuber
Comment Utility
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
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
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
Comment Utility
i want to maintain 5 sequence date range can be disturbed even or odd
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
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
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 73

Expert Comment

by:sdstuber
Comment Utility
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
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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 34

Expert Comment

by:Mark Geerlings
Comment Utility
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

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.

Join & Write a Comment

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now