If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > SQL Query to get the latest date

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-11-10, 11:57
asburym1 asburym1 is offline
Registered User
 
Join Date: Mar 2010
Posts: 14
SQL Query to get the latest date

I have a table of customers who have multiple credit scores recorded differents days. I am using SQL 2000 queries

to get the customer latest date credit score only. When I use Max(scoredate) with the customer-id and credit-score

in the group by clause, I still get the previous scoredates instead of just the max scoredate. However if, I don't

put the credit-score in the Select clause I get the max scoredate. How can I get the credit-score with the max

scoredate?

For example;

select max(scoredate) as scoredate, customer-id, credit-score
from customer-tbl
group by customer-id, credit-score

returns the following result:

scoredate customer-id credit-score
---------- ----------- ------------
2003-07-05 100131 715
2001-03-02 100131 738
2002-01-02 100157 706

I want it to return the following result:

scoredate customer-id credit-score
---------- ----------- ------------
2003-07-05 100131 715
2002-01-02 100157 706



Table DDL:
CREATE TABLE customer-tbl(Col1 datetime, Col2 int, Col3 int)
Reply With Quote
  #2 (permalink)  
Old 03-11-10, 12:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by asburym1 View Post
I am using SQL 2000 queries
no you're not, you're using DB2

DB2 SQL Query to get the latest date only

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-15-10, 22:32
TerryP TerryP is offline
Registered User
 
Join Date: Feb 2007
Posts: 38
Hi

Whatever your reason may be, in SQL Server, the answer for your question is as below. This is a question about selecting top n rows from a grouped records.

if object_id('tempdb.#customer_tbl') is not null
drop table #customer_tbl

CREATE TABLE #customer_tbl(Col1 datetime, Col2 int, Col3 int)

INSERT INTO #customer_tbl Values('2003-07-05', 100131, 715)
INSERT INTO #customer_tbl Values('2001-03-02', 100131, 700)
INSERT INTO #customer_tbl Values('2004-09-02', 100131, 38)
INSERT INTO #customer_tbl Values('2005-08-02', 100131, 73)
INSERT INTO #customer_tbl Values('2005-08-03', 100131, 78)
INSERT INTO #customer_tbl Values('2002-01-02', 100157, 706)
INSERT INTO #customer_tbl Values('2003-01-02', 100157, 706)
INSERT INTO #customer_tbl Values('2004-01-02', 100157, 706)
SELECT * FROM #customer_tbl t
WHERE
(
Select Count(*)
FROM #customer_tbl
WHERE t.Col2=Col2
and Col1>t.Col1
)<1
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On