473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Need help on a query with analytics

Hi,

i need help on a query.

I've this table :

create table t (
customer number(5),
time date,
status varchar2(1)
)
/

insert into t values (1,to_date('20030101','yyyymmdd'),1);
insert into t values (1,to_date('20030121','yyyymmdd'),0);
insert into t values (1,to_date('20030803','yyyymmdd'),1);
insert into t values (1,to_date('20030909','yyyymmdd'),1);
insert into t values (1,to_date('20031201','yyyymmdd'),0);
What i want is this output, representing periods (with a begin and an
end date) of successive status.

customer status begin_ end_
1 1 01/01/03 21/01/03
1 0 21/01/03 03/08/03
1 1 03/08/03 01/12/03
1 0 01/12/03
On a windows 2000 platform EE 8.1.7, i write this query :

select customer,
status,
time begin_,
lead(time) over (partition by customer order by time) end_
from t
order by customer, time
/

CUSTOMER S BEGIN_ END_
--------- - -------- --------
1 1 01/01/03 21/01/03
1 0 21/01/03 03/08/03
1 1 03/08/03 09/09/03
1 1 09/09/03 01/12/03
1 0 01/12/03

but this is not exactly what i search (lines 3 and 4 need to be merge)

Analytics functions can be useful here ?
Can someone help me ?

Thanks in advance
Jul 19 '05 #1
3 2353
fr**********@yahoo.fr (FRITSCH Vincent) wrote in message news:<b2**************************@posting.google. com>...
What i want is this output, representing periods (with a begin and an
end date) of successive status.

customer status begin_ end_
1 1 01/01/03 21/01/03
1 0 21/01/03 03/08/03
1 1 03/08/03 01/12/03
1 0 01/12/03
On a windows 2000 platform EE 8.1.7, i write this query :

select customer,
status,
time begin_,
lead(time) over (partition by customer order by time) end_
from t
order by customer, time
/

CUSTOMER S BEGIN_ END_
--------- - -------- --------
1 1 01/01/03 21/01/03
1 0 21/01/03 03/08/03
1 1 03/08/03 09/09/03
1 1 09/09/03 01/12/03
1 0 01/12/03

but this is not exactly what i search (lines 3 and 4 need to be merge)

Hi

What you're saying is, that the record of 09/09/03 can be ignored, as that
record set the status of the customer to the same as it already was, right ?
Then this will give you what you want :
SQL> select
2 customer,
3 status,
4 time begin_,
5 lead(time) over (partition by customer order by time) end_
6 from (
7 select
8 t.*,
9 case
10 when status = lag(status) over (partition by customer order by
time) then 1
11 else 0
12 end samestatus
13 from t
14 )
15 where samestatus = 0
16 order by customer, time;

CUSTOMER S BEGIN_ END_
---------- - -------- --------
1 1 03-01-01 03-01-21
1 0 03-01-21 03-08-03
1 1 03-08-03 03-12-01
1 0 03-12-01
Hope that helps.

/KiBeHa
Jul 19 '05 #2
fr**********@yahoo.fr (FRITSCH Vincent) wrote in message news:<b2**************************@posting.google. com>...
Hi,

i need help on a query.

I've this table :

create table t (
customer number(5),
time date,
status varchar2(1)
)
/

insert into t values (1,to_date('20030101','yyyymmdd'),1);
insert into t values (1,to_date('20030121','yyyymmdd'),0);
insert into t values (1,to_date('20030803','yyyymmdd'),1);
insert into t values (1,to_date('20030909','yyyymmdd'),1);
insert into t values (1,to_date('20031201','yyyymmdd'),0);
What i want is this output, representing periods (with a begin and an
end date) of successive status.

customer status begin_ end_
1 1 01/01/03 21/01/03
1 0 21/01/03 03/08/03
1 1 03/08/03 01/12/03
1 0 01/12/03
On a windows 2000 platform EE 8.1.7, i write this query :

select customer,
status,
time begin_,
lead(time) over (partition by customer order by time) end_
from t
order by customer, time
/

CUSTOMER S BEGIN_ END_
--------- - -------- --------
1 1 01/01/03 21/01/03
1 0 21/01/03 03/08/03
1 1 03/08/03 09/09/03
1 1 09/09/03 01/12/03
1 0 01/12/03

but this is not exactly what i search (lines 3 and 4 need to be merge)

Analytics functions can be useful here ?
Can someone help me ?

Thanks in advance


I think this does what you need - basically, remove "non-events" like
row 4 from the data first:

1 select customer,
2 status,
3 time begin_,
4 lead(time) over (partition by customer order by time) end_
5 from ( select customer,
6 status,
7 time,
8 lag(status) over (partition by customer order by
time) prev_status
9 from t
10 )
11 where status != nvl(prev_status,-1)
12* order by customer, time
SQL> /

CUSTOMER S BEGIN_ END_
---------- - ----------- -----------
1 1 01-JAN-2003 21-JAN-2003
1 0 21-JAN-2003 03-AUG-2003
1 1 03-AUG-2003 01-DEC-2003
1 0 01-DEC-2003
Jul 19 '05 #3
Thank you for your help

Vincent

an******@onetel.net.uk (Tony) wrote in message news:<c0**************************@posting.google. com>...
fr**********@yahoo.fr (FRITSCH Vincent) wrote in message news:<b2**************************@posting.google. com>...
Hi,

i need help on a query.

I've this table :

create table t (
customer number(5),
time date,
status varchar2(1)
)
/

insert into t values (1,to_date('20030101','yyyymmdd'),1);
insert into t values (1,to_date('20030121','yyyymmdd'),0);
insert into t values (1,to_date('20030803','yyyymmdd'),1);
insert into t values (1,to_date('20030909','yyyymmdd'),1);
insert into t values (1,to_date('20031201','yyyymmdd'),0);
What i want is this output, representing periods (with a begin and an
end date) of successive status.

customer status begin_ end_
1 1 01/01/03 21/01/03
1 0 21/01/03 03/08/03
1 1 03/08/03 01/12/03
1 0 01/12/03
On a windows 2000 platform EE 8.1.7, i write this query :

select customer,
status,
time begin_,
lead(time) over (partition by customer order by time) end_
from t
order by customer, time
/

CUSTOMER S BEGIN_ END_
--------- - -------- --------
1 1 01/01/03 21/01/03
1 0 21/01/03 03/08/03
1 1 03/08/03 09/09/03
1 1 09/09/03 01/12/03
1 0 01/12/03

but this is not exactly what i search (lines 3 and 4 need to be merge)

Analytics functions can be useful here ?
Can someone help me ?

Thanks in advance


I think this does what you need - basically, remove "non-events" like
row 4 from the data first:

1 select customer,
2 status,
3 time begin_,
4 lead(time) over (partition by customer order by time) end_
5 from ( select customer,
6 status,
7 time,
8 lag(status) over (partition by customer order by
time) prev_status
9 from t
10 )
11 where status != nvl(prev_status,-1)
12* order by customer, time
SQL> /

CUSTOMER S BEGIN_ END_
---------- - ----------- -----------
1 1 01-JAN-2003 21-JAN-2003
1 0 21-JAN-2003 03-AUG-2003
1 1 03-AUG-2003 01-DEC-2003
1 0 01-DEC-2003

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
2
by: Tom Cole | last post by:
Howdy, When a client has cookies turn off, our application uses the ASP.NET 2.0 cookieless session which puts the session id into the URI. Is there anyway to make it use a query string instead?...
10
by: L. R. Du Broff | last post by:
I own a small business. Need to track a few hundred pieces of rental equipment that can be in any of a few dozen locations. I'm an old-time C language programmer (UNIX environment). If the only...
1
by: David Chang | last post by:
Hi, does any one know how google analytics (urchin.js) works? I always thought javascript cannot talk to multiple servers due to security constraints. But google analytics seems to be able to...
0
by: Ansuiya | last post by:
hii Can any 1 tell me which is fast ? AWstat or google analytics.Google Analytics takes too much time for processing but i dnt know about AWstat.Help please.. Thanx
14
victorduwon
by: victorduwon | last post by:
Hi guys and gals, I am using javascript to autologin to my google analytics account via a hyperlink. This is what the text looks like this <?xml version="1.0" encoding="utf-8"?>...
1
by: tatipellinaresh | last post by:
Hi, I want to use visitor tracking in my web application which is similar to Google Analytics. But in case of google we have to use their script file and they will store out site's data into their...
3
by: knkk | last post by:
I am trying to include this code in the footer.template files of all blog templates: <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.