aspose file tools
The moose likes JDBC and the fly likes SQL: Select the first of each set Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "SQL: Select the first of each set" Watch "SQL: Select the first of each set" New topic
Author

SQL: Select the first of each set

Timothy Chen Allen
Ranch Hand

Joined: Mar 16, 2003
Posts: 161
I have a table like this. It's a ledger. Each entry is associated with an invoice number, and there is a transaction type and a description. The table is sorted by invoice and transaction type:

I need to extract a listing of invoices and their descriptions-- however, the client is only interested in the very first description of each invoice, so this isn't about doing a concatonation.
If I do this right, the list above will produce:

I'm currently doing this with a cursor (I'm in SQL Server 2000), but there are 700,000 records, so this is taking far too long. How can I do this without a cursor (I run through the list, recording the inveoice number, and write out the record to another table when the invoice number changes), or at least more quickly? Thanks in advance.
[ November 20, 2003: Message edited by: Tim Allen ]

Timothy Chen Allen
Learn Spanish in Washington, DC
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26201
    
  66

Tim,
That's a good idea to store a table with the results. Take a look at triggers. You can set one up so that when the base table changes, your auxillary one is updated too (if neccessary.)


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: SQL: Select the first of each set
 
Similar Threads
A Tricky Stored Procedure
Commercial Mock Exams
Hibernate 3.3.2 not flushing inserts before selects
Homework help please!!
i need to get my work verified , can u please check & comment ??