Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

SQL: Select the first of each set

 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
author & internet detective
Posts: 39986
806
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic