473,394 Members | 1,724 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,394 software developers and data experts.

Joining Same DB Table but want to locate back one record only

Right folks.

First time poster here.

I have a table called Trans that looks like



Batch, Seq, Client, Date, Transaction
---------------------------------------------------------
B100, 1, C1, 10/09/2007, T1
B100, 2, C2, 10/09/2007, T2
...
B78, 5, C1, 05/05/2007, T5
..
B69, 3, C2, 04/04/2007, T8
..
B53, 3, C1, 03/03/2007, T10


The script I need will have to go through all recs in B100 and find the last transaction (by date) for each client within the same table. I basically want to end up with

Client, Date
----------------------
C1, 05/05/2007
C2, 04/04/2007

Hopefully somebody can give me a script to do it.

Thanks

RebelDevil
Oct 9 '07 #1
11 1422
amitpatel66
2,367 Expert 2GB
Right folks.

First time poster here.

I have a table called Trans that looks like



Batch, Seq, Client, Date, Transaction
---------------------------------------------------------
B100, 1, C1, 10/09/2007, T1
B100, 2, C2, 10/09/2007, T2
...
B78, 5, C1, 05/05/2007, T5
..
B69, 3, C2, 04/04/2007, T8
..
B53, 3, C1, 03/03/2007, T10


The script I need will have to go through all recs in B100 and find the last transaction (by date) for each client within the same table. I basically want to end up with

Client, Date
----------------------
C1, 05/05/2007
C2, 04/04/2007

Hopefully somebody can give me a script to do it.

Thanks

RebelDevil
Your sample data is not that clear and is not with in sync of your requirement:

You want the latest transaction for each client under each batch.

Sample data

Batch client date trans
B100 C1 10/09/2007 T1
B100 C1 05/05/2007 T2
B100 C2 10/09/2007 T3
B100 C2 05/04/2007 T4

Then your sample output should look like:

Client Date
C1 10/09/2007
C2 10/09/2007
...........

I assume my understanding is correct,
try for below query:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT client,date FROM table_name 
  3. WHERE date IN(select max(date) from table_name GROUP BY batch,client)
  4.  
  5.  
Oct 9 '07 #2
Sorry,

Let me try to explain it again. I might have got it wrong above.

The records in the Trans Table are

Batch, Seq, Client, Date, Transaction
---------------------------------------------
B100, 1, C1, 10/09/2007, T1
B100, 2, C2, 10/09/2007, T2
B78, 5, C1, 05/05/2007, T5
B69, 3, C2, 04/04/2007, T8
B69, 4, C1, 04/04/2007, T12
B53, 3, C1, 03/03/2007, T10


The unique index for this table is Batch+Seq

I have a screen then that display all transactions for Batch B100 so it displays

B100, 1, C1, 10/09/2007, T1
B100, 2, C2, 10/09/2007, T2

But I also need to display the previous transaction for each client.

When I say previous I mean by the last transaction by Sample date. So the previous one in the file for C1 is

B78, 5, C1, 05/05/2007, T5

and the previous one in the file for C2 is

B69, 3, C2, 04/04/2007, T8


My final SQL Statement should return
Client, Prev Transaction.
C1, T5
C2, T8

Once I have a SQL statement to display the above then I can work out the rest.

Hope this explains it better.

TIA
RebelDevil
Oct 9 '07 #3
amitpatel66
2,367 Expert 2GB
Sorry,

Let me try to explain it again. I might have got it wrong above.

The records in the Trans Table are

Batch, Seq, Client, Date, Transaction
---------------------------------------------
B100, 1, C1, 10/09/2007, T1
B100, 2, C2, 10/09/2007, T2
B78, 5, C1, 05/05/2007, T5
B69, 3, C2, 04/04/2007, T8
B69, 4, C1, 04/04/2007, T12
B53, 3, C1, 03/03/2007, T10


The unique index for this table is Batch+Seq

I have a screen then that display all transactions for Batch B100 so it displays

B100, 1, C1, 10/09/2007, T1
B100, 2, C2, 10/09/2007, T2

But I also need to display the previous transaction for each client.

When I say previous I mean by the last transaction by Sample date. So the previous one in the file for C1 is

B78, 5, C1, 05/05/2007, T5

and the previous one in the file for C2 is

B69, 3, C2, 04/04/2007, T8


My final SQL Statement should return
Client, Prev Transaction.
C1, T5
C2, T8

Once I have a SQL statement to display the above then I can work out the rest.

Hope this explains it better.

TIA
RebelDevil
Try below query:

Expand|Select|Wrap|Line Numbers
  1. SELECT e.client,e.transaction FROM
  2. (SELECT client,date,transaction,dense_rank() OVER(PARTITION BY client ORDER BY date DESC) rn FROM table_name) e
  3. WHERE e.rn = 2
  4.  
Oct 9 '07 #4
Try below query:

Expand|Select|Wrap|Line Numbers
  1. SELECT e.client,e.transaction FROM
  2. (SELECT client,date,transaction,dense_rank() OVER(PARTITION BY client ORDER BY date DESC) rn FROM table_name) e
  3. WHERE e.rn = 2
  4.  
That wont work because if I open Batch 69 I should be able to get the previous transactions for those client.

B69, 3, C2, 04/04/2007, T8
B69, 4, C1, 04/04/2007, T12

so it would locate T10 for C1 from the below record and nothering for C2.

B53, 3, C1, 03/03/2007, T10
Oct 9 '07 #5
amitpatel66
2,367 Expert 2GB
That wont work because if I open Batch 69 I should be able to get the previous transactions for those client.

B69, 3, C2, 04/04/2007, T8
B69, 4, C1, 04/04/2007, T12

so it would locate T10 for C1 from the below record and nothering for C2.

B53, 3, C1, 03/03/2007, T10
I have a small clarification:

Consider following data:

B100 C1 10/09/07 T1
B100 C1 04/04/07 T2
B100 C2 10/09/07 T3
B100 C2 04/04/07 T4

So you want previous transactions for all the clients for batch B100 ie

C1 T2
C2 T4

This you can achieve by the query that I gave you in the previous post.

But now consider following data:

B100 C1 10/09/07 T1
B100 C1 06/04/07 T2
B100 C2 10/09/07 T3
B100 C2 06/04/07 T4
B69, C2, 04/04/2007, T8
B69, C1, 04/04/2007, T12
B53, C1, 03/03/07, T10

So you want the previous transaction of all the clients for a particular batch right?

If you pass B100 then you want as:

C1 T2
C2 T4

and when you pass B69 then you want as:

C1 T10

Is my explanation correct?
Oct 10 '07 #6
I have a small clarification:

Consider following data:

B100 C1 10/09/07 T1
B100 C1 04/04/07 T2
B100 C2 10/09/07 T3
B100 C2 04/04/07 T4

So you want previous transactions for all the clients for batch B100 ie

C1 T2
C2 T4

This you can achieve by the query that I gave you in the previous post.

But now consider following data:

B100 C1 10/09/07 T1
B100 C1 06/04/07 T2
B100 C2 10/09/07 T3
B100 C2 06/04/07 T4
B69, C2, 04/04/2007, T8
B69, C1, 04/04/2007, T12
B53, C1, 03/03/07, T10

So you want the previous transaction of all the clients for a particular batch right?

If you pass B100 then you want as:

C1 T2
C2 T4

and when you pass B69 then you want as:

C1 T10

Is my explanation correct?
Yes, your explanation with your data is correct.
Oct 10 '07 #7
amitpatel66
2,367 Expert 2GB
Yes, your explanation with your data is correct.
Check out below Query. It takes one input parameter ie BATCH. Pass any batch value and check if it is returning you the previous transaction for each client of that batch

Expand|Select|Wrap|Line Numbers
  1. SELECT g.client,g.hdate FROM
  2. (SELECT DISTINCT tran.batch,e.client,tran.hdate,ROW_NUMBER() OVER(PARTITION BY e.client ORDER BY tran.hdate DESC) rn FROM
  3. (SELECT client,hdate from tran where batch = &1) e, tran
  4. WHERE e.client=tran.client
  5. AND  tran.hdate < e.hdate) g
  6. WHERE g.rn = 1
  7.  
Oct 10 '07 #8
sorry about this but this is exactly what I have. Above I was trying to give an example so I could work with the replies I got.

Current table is

TType BType Ins BNo ISO Id Res SDate
2 1 F 057 010 S1 14 10/10/2007
2 1 F 057 020 S2 1 10/10/2007
2 1 F 057 030 S3 11 10/10/2007
2 1 F 054 010 S1 12 09/10/2007
2 1 F 053 040 S3 4 05/10/2007
2 1 F 050 020 S1 20 01/10/2007
2 1 F 050 070 S2 30 01/10/2007

The unique index in this table is the first 5 columns

The Script I want will go through a batch and return the previous Res for all ids within a Batch (BNo)..

The script I have at the moment is

Expand|Select|Wrap|Line Numbers
  1. SELECT cur.ISO, MAX(prv.SDate) 
  2. FROM BatchDets cur
  3. INNER JOIN BatchDets prv 
  4.     ON cur.TType = prv.TType AND cur.BType = prv.BType 
  5.     AND cur.Id = prv.Id AND cur.SDate > prv.SDate 
  6. WHERE cur.TType= '2' and  cur.BType = '1' and  cur.Ins = 'F'
  7.     and  cur.BNo = '057' 
  8. GROUP BY cur.ISO
The above statement returns

cur.ISO MAX(prv.SDate)
010 09/10/2007
020 01/10/2007
030 05/10/2007

what I would like to be returned is

cur.ISO prv.Res
010 12
020 30
030 4

If I try to add the prv.Res to the select statement I get errors.

I hope this explains in full my requirements.

Thanks again
RebelDevil
Oct 10 '07 #9
amitpatel66
2,367 Expert 2GB
sorry about this but this is exactly what I have. Above I was trying to give an example so I could work with the replies I got.

Current table is

TType BType Ins BNo ISO Id Res SDate
2 1 F 057 010 S1 14 10/10/2007
2 1 F 057 020 S2 1 10/10/2007
2 1 F 057 030 S3 11 10/10/2007
2 1 F 054 010 S1 12 09/10/2007
2 1 F 053 040 S3 4 05/10/2007
2 1 F 050 020 S1 20 01/10/2007
2 1 F 050 070 S2 30 01/10/2007

The unique index in this table is the first 5 columns

The Script I want will go through a batch and return the previous Res for all ids within a Batch (BNo)..

The script I have at the moment is

Expand|Select|Wrap|Line Numbers
  1. SELECT cur.ISO, MAX(prv.SDate) 
  2. FROM BatchDets cur
  3. INNER JOIN BatchDets prv 
  4.     ON cur.TType = prv.TType AND cur.BType = prv.BType 
  5.     AND cur.Id = prv.Id AND cur.SDate > prv.SDate 
  6. WHERE cur.TType= '2' and  cur.BType = '1' and  cur.Ins = 'F'
  7.     and  cur.BNo = '057' 
  8. GROUP BY cur.ISO
The above statement returns

cur.ISO MAX(prv.SDate)
010 09/10/2007
020 01/10/2007
030 05/10/2007

what I would like to be returned is

cur.ISO prv.Res
010 12
020 30
030 4

If I try to add the prv.Res to the select statement I get errors.

I hope this explains in full my requirements.

Thanks again
RebelDevil

Could you please try the query that i provided you:
Expand|Select|Wrap|Line Numbers
  1. SELECT g.client,g.hdate FROM
  2. (SELECT DISTINCT tran.batch,e.client,tran.hdate,ROW_NUMBER() OVER(PARTITION BY e.client ORDER BY tran.hdate DESC) rn FROM
  3. (SELECT client,hdate FROM tran WHERE batch = &1) e, tran
  4. WHERE e.client=tran.client
  5. AND  tran.hdate < e.hdate) g
  6. WHERE g.rn = 1
  7.  
I think it would be easier for you to re write the above query in the way you want and check if it is working!!
Oct 11 '07 #10
Could you please try the query that i provided you:
Expand|Select|Wrap|Line Numbers
  1. SELECT g.client,g.hdate FROM
  2. (SELECT DISTINCT tran.batch,e.client,tran.hdate,ROW_NUMBER() OVER(PARTITION BY e.client ORDER BY tran.hdate DESC) rn FROM
  3. (SELECT client,hdate FROM tran WHERE batch = &1) e, tran
  4. WHERE e.client=tran.client
  5. AND  tran.hdate < e.hdate) g
  6. WHERE g.rn = 1
  7.  
I think it would be easier for you to re write the above query in the way you want and check if it is working!!
I've tried to change the query you gave me to extract the information I want but couldn't as I need to be able to select from 2 different views of the same table at the end.

In you example it would mean starting with
Expand|Select|Wrap|Line Numbers
  1. SELECT e.client,g.hdate FROM
Oct 12 '07 #11
amitpatel66
2,367 Expert 2GB
I've tried to change the query you gave me to extract the information I want but couldn't as I need to be able to select from 2 different views of the same table at the end.

In you example it would mean starting with
Expand|Select|Wrap|Line Numbers
  1. SELECT e.client,g.hdate FROM
Let me explain my query:

First the batch number is passed to the query and it selects all the clients,tran date for that batch. Then i compare tran date with another object of same table, and i get records for tran date < date of another table alias....

i partition the data and assign the row number and get the record whose row = 1
Oct 12 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Sami | last post by:
Please bear with me, and if you answer this question, please do it step by step. I am new at Access, not at all sophisticated. I am using Office XP. This will need to be read in Access for...
8
by: kaosyeti | last post by:
i have a (hopefully) small problem. i have created a system where a user enters customer information into a table through a form. this table has no primary key. there are 9 fields on the form to...
5
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
2
by: Thomas R. Hummel | last post by:
Hello, I am currently working on a monthly load process with a datamart. I originally designed the tables in a normalized fashion with the idea that I would denormalize as needed once I got an...
12
by: veaux | last post by:
Question about joins in queries. I have 2 tables with a field called "ID". Table 1 Rec1 = Jan12FredFlintstone Rec2 = Feb01WilmaRubble Table 2 Rec1 = Jan12BarneyRubble Rec2 = Mar03SamSlate
4
by: J | last post by:
I am editing a pre-existing view. This view is already bringing data from 40+ tables so I am to modify it without screwing with anything else that is already in there. I need to (left) join it...
4
by: Rnt6872 | last post by:
Table A Table B BOL# B_BOL# Chargeback# Hi All, I have been struggling with this for...
22
by: mforema | last post by:
Hey Everybody, I have an URGENT REQUEST (I need a reply within 24 hours, or I lose my job) I need help writing vba code for a command button. I want this button to locate a hyperlink in a table (the...
4
by: Neil | last post by:
Having trouble with inserting a record into a table. It's a list of names. But, for some reason, it won't take a particular name. When a user tries to enter a name into the table, the system hangs...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.