same record with different status

I am looking to get accountnumbers which has status = 5  and that changed to status = 8.  There should not be any intermediate status in between these two status.  Please suggest how do I get this in single query. They have effective dates associated with each status.

Quick response is appreciated!
d27m11yAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
DB2 or Oracle?

How do you log the status change?

Can you provide sample data and expected results?  A table definition wouldn't hurt...
0
Kent OlsenData Warehouse Architect / DBACommented:
Hi D27,

The only practical way to view the changes is if you actually save a new row every time the status changes.  If that's the case, you can use the OLAP extensions to serialize the rows by account number.

WITH t0
AS
(
  SELECT row_number () over (partition by account, logged_timestamp) RN, t.*
  FROM mytable
) 
SELECT * FROM t0
WHERE status = 8
  AND (select 1 FROM t0 t1 WHERE t0.account = t1.account AND t1.RN+1 = t0.RN AND t1.status = 5)

Open in new window


Something like that should work.

Kent
0
sventhanCommented:
Try this ...

select acctno,effectivedate,
      count(*) over (partition by acctno,effectivedate
                     order by effectivedate
                     ) cnt
    from yourtable
    where cnt = 2
    status = 5 and status =8
   order by acctno,effectivedate
   /
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

d27m11yAuthor Commented:
Am looking in DB2, as such.

select * from
(Select acct_no,
STAT_CD ,
STAT_EFF_DT as DECES_EFF_DT ,
STAT_RM_DT as DECES_REMV_DT 
FROM A
WHERE STAT_CD in ('5') ) A
join
(Select acct_no,
STAT_CD ,
STAT_EFF_DT as PRG_EFF_DT,
STAT_RM_DT as PRG_REMV_DT
FROM A
WHERE STAT_CD in ('8')) B
on a.acct_no = b.acct_no
and a.DECES_REMV_DT = b.PRG_EFF_DT
where acct_no in ('1')

Open in new window



This is what I am trying to do. Is there any easier way to do it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenData Warehouse Architect / DBACommented:
Hi D27,

All 3 of the DB2 platforms should run the query I posted above, as long as they are running a fairly recent release.


Kent
0
PortletPaulfreelancerCommented:
The approach you show in your query looks sound, but I'm relying on you know the data
    (e.g. I cannot tell if this is good or not: A.DECES_REMV_DT = B.PRG_EFF_DT )

You can use 2 derived tables as you are doing, or this should also work:
SELECT
      A.*, B.*
FROM TableA A
      INNER JOIN TableA B
                  ON A.acct_no = B.acct_no
                        AND A.DECES_REMV_DT = B.PRG_EFF_DT
                        AND B.STAT_CD = '8'
WHERE A.STAT_CD = '5'
      AND A.acct_no IN ('1') -- use = if possible

Open in new window

0
d27m11yAuthor Commented:
@Kdo, how do I write my above mentioned query using WITH clause
0
Kent OlsenData Warehouse Architect / DBACommented:
I'm not sure what you mean.  I posted a CTE (with clause) style example.
0
d27m11yAuthor Commented:
Served my purpose
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB2

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.