Welcome to dbForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

SQL question ( retrieving records based on a priority orde..

 
   Database Forums (Home) -> General Discussions RSS
Next:  Use unique key for Sqlserver view for updates  
Author Message
myemail.an

External


Since: Dec 05, 2007
Posts: 2



(Msg. 1) Posted: Fri Feb 22, 2008 5:49 am
Post subject: SQL question ( retrieving records based on a priority order )
Archived from groups: comp>databases>ms-sqlserver (more info?)

Hi all,

I have a table with this structure:

Customer ID | Transaction date | Transaction type
1 | 1/2/2008 | F
1 | 1/4/2007 | M
1 | 1/2/2008 | R
2 | 1//5/2007 | M
2 | 1/6/2007 | R


For each customer ID, I need to retrive only the records with the most
recent transaction date, and I did:

Customer ID | Transaction date | Transaction type
1 | 1/2/2008 | F
1 | 1/2/2008 | R
2 | 1/6/2007 | R

The problem is, each customer can make more than one transaction in
the same day (see above). In these cases, I need to retrieve only one
record per customer, based on the importance of the transaction: F is
more "important" than R which is more "important" than M. So, in the
example above, I would only retrieve transaction F for customer 1 and
disregard transaction R: they were made on the same date, but I am
more interested in F than in R.

Do you have any suggestions on how I could achieve this in SQL?

I use Microsoft SQL server 2005.

Thank you for your help!

 >> Stay informed about: SQL question ( retrieving records based on a priority orde.. 
Back to top
Login to vote
Plamen Ratchev

External


Since: Nov 06, 2007
Posts: 352



(Msg. 2) Posted: Fri Feb 22, 2008 9:40 am
Post subject: Re: SQL question ( retrieving records based on a priority order ) [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Here is one way:

WITH TransCTE
AS
(SELECT customer_id,
transaction_date,
transaction_type,
ROW_NUMBER() OVER(
PARTITION BY customer_id
ORDER BY transaction_date DESC,
CASE transaction_type
WHEN 'F' THEN 1
WHEN 'R' THEN 2
WHEN 'M' THEN 3
ELSE 99 END) AS seq
FROM Transactions)
SELECT customer_id,
transaction_date,
transaction_type
FROM TransCTE
WHERE seq = 1;

HTH,

Plamen Ratchev
http://www.SQLStudio.com

 >> Stay informed about: SQL question ( retrieving records based on a priority orde.. 
Back to top
Login to vote
"Roy Harvey

External


Since: Sep 24, 2007
Posts: 167



(Msg. 3) Posted: Fri Feb 22, 2008 12:01 pm
Post subject: Re: SQL question ( retrieving records based on a priority order ) [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

CREATE TABLE #X
(Cust int, TranDate datetime, TranType char(1))

INSERT #X values (1, '1/2/2008', 'F')
INSERT #X values (1, '1/4/2007', 'M')
INSERT #X values (1, '1/2/2008', 'R')
INSERT #X values (2, '1/5/2007', 'M')
INSERT #X values (2, '1/6/2007', 'R')

WITH Ranked AS
(
SELECT *,
rank() over (PARTITION BY Cust
ORDER BY TranDate desc,
CASE TranType
WHEN 'F' THEN 'C'
WHEN 'R' THEN 'B'
WHEN 'M' THEN 'A'
END) as Priority
FROM #X
)
SELECT *
FROM Ranked as A
WHERE A.Priority = 1

Note that I used A, B, C rather than the more logical 1, 2, 3 because
I wanted to emphasize that the column Priority was the number assigned
by Rank, and remove any chance of confusing the CASE epxression with
that column.

Roy Harvey
Beacon Falls, CT

On Fri, 22 Feb 2008 05:49:22 -0800 (PST), "myemail.an@googlemail.com"
<myemail.an.DeleteThis@googlemail.com> wrote:

>Hi all,
>
>I have a table with this structure:
>
>Customer ID | Transaction date | Transaction type
>1 | 1/2/2008 | F
>1 | 1/4/2007 | M
>1 | 1/2/2008 | R
>2 | 1//5/2007 | M
>2 | 1/6/2007 | R
>
>
>For each customer ID, I need to retrive only the records with the most
>recent transaction date, and I did:
>
>Customer ID | Transaction date | Transaction type
>1 | 1/2/2008 | F
>1 | 1/2/2008 | R
>2 | 1/6/2007 | R
>
>The problem is, each customer can make more than one transaction in
>the same day (see above). In these cases, I need to retrieve only one
>record per customer, based on the importance of the transaction: F is
>more "important" than R which is more "important" than M. So, in the
>example above, I would only retrieve transaction F for customer 1 and
>disregard transaction R: they were made on the same date, but I am
>more interested in F than in R.
>
>Do you have any suggestions on how I could achieve this in SQL?
>
>I use Microsoft SQL server 2005.
>
>Thank you for your help!
 >> Stay informed about: SQL question ( retrieving records based on a priority orde.. 
Back to top
Login to vote
myemail.an

External


Since: Dec 05, 2007
Posts: 2



(Msg. 4) Posted: Sun Feb 24, 2008 1:13 am
Post subject: Re: SQL question ( retrieving records based on a priority order ) [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Back to top
Login to vote
Display posts from previous:   
   Database Forums (Home) -> General Discussions All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]