selecting the single largest/smallest/first/latest row from a table where dates/amounts are not unique

Liam

Afternoon all,
I'm trying to pull together a query that will return financial overview information about our customers.
I have a financial_history table of about 10m rows that contains details of every transaction and I want to see each customers first, last and largest transactions.
The tricky bit (and, yes, this is in part a data quality issue and, yes it's very annoying) is that practically every single customer has more than one transaction on the date of their first/last and has several at the same maximum value. It's not entirely hopeless because each transaction has an ID number that resets daily so the date and ID forms a unique identifier and the transaction IDs are also sequential so, in the case of the earliest transaction, where there are multiple rows for a customer on the same date I want the one with the lowest transaction number
The other problem is that this needs to be run as a sproc job to populate a table with updated info every day so efficiency/runtime is an issue.
Reading through previous questions and answers here I've got as far as a query that runs reasonably quickly but I can't figure out how to incorporate the transaction ID element to select only the single relevant row.
For the largest transaction, for example, the query looks like this

select
fh.contact_number
,fh.maxamt
,fh2.transaction_date
from
(select
#fh.contact_number
,MAX(#fh.amount) maxamt
from #fh
group by
#fh.contact_number) fh
INNER JOIN #fh as fh2
ON fh2.amount = fh.maxamt and fh2.contact_number = fh.contact_number;

and returns information like this

| contact_number | maxamt |     transaction_date    |
|:--------------:|:------:|:-----------------------:|
|        1       |   100  | 2010-06-01 00:00:00.000 |
|        1       |   100  | 2012-06-01 00:00:00.000 |
|        1       |   100  | 2013-06-03 00:00:00.000 |
|        2       |   500  | 2011-04-09 00:00:00.000 |
|        2       |   500  | 2013-11-21 00:00:00.000 |

the financial_history table looks like this

contact_number int
,transID int
,amount money
,transaction_date datetime

Any ideas how I can change this query to select only the row with the largest transID? I'm also open to completely different approaches but I've tried a lot of different things (most of them copied from other questions here) and this is by far and away the fastest running.
I will be immensely grateful for any help or suggestions Thanks!

Jim V.

This will return all of the transaction of the maximum amount. You can add additional criteria to amount_rank SORT BY clause if you want to reduce the number of transactions returned.

SELECT  *
FROM    ( SELECT    contact_number ,
                    transID ,
                    amount ,
                    transaction_date ,
                    RANK() OVER ( PARTITION BY contact_number ORDER BY amount DESC ) AS amount_rank ,
                    ROW_NUMBER() OVER ( PARTITION BY customer_number ORDER BY transaction_date ASC, transID ASC ) AS first_transaction ,
                    ROW_NUMBER() OVER ( PARTITION BY customer_number ORDER BY transaction_date DESC, transID DESC ) AS last_transaction
          FROM      #fh
        ) fh
WHERE   ( fh.amount_rank = 1
          OR fh.first_transaction = 1
          OR fh.last_transaction = 1
        )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Selecting single row with no unique key

From Dev

Selecting row from table in laravel

From Dev

How to select a single row where multiple rows exist from a table

From Dev

How to select a single row where multiple rows exist from a table

From Dev

Selecting row from Table - Java Selenium

From Dev

mySQL - Selecting the latest row from a table

From Dev

MySQL - selecting random row from large table

From Dev

selecting from table where timestamp is latest

From Dev

selecting unique rows from one table according to another table and then sorting it

From Dev

delete single row from group of unique - oracle

From Dev

delete single row from group of unique - oracle

From Dev

Efficiently selecting rows where a row in a related table exists

From Dev

Selecting Latest Row From MAX Row of 3rd Table

From Dev

What is the best way for selecting 2 row from table in one row?

From Dev

selecting records from main table and count of each row in another table

From Dev

Selecting a row in a html table

From Dev

Get unique row by single column where duplicates exist

From Dev

Is it possible to delete a single row using JPA repository where the rows are not unique?

From Dev

Selecting unique records from a table and giving weight to repetitions

From Dev

Selecting unique values from self-referencing table

From Dev

How to combine data from one table where there are duplicate values into a single row?

From Dev

How to combine data from one table where there are duplicate values into a single row?

From Dev

Selecting records where change has occurred from previous row

From Dev

Selecting two rows from another table using one row

From Dev

Selecting Data from Joomla SQL Table where any value is the variable

From Dev

SQL Selecting From One Table With Multiple Where Clauses

From Dev

Simple SQL query selecting from table where email = email

From Dev

MySQL - Selecting data from another table for a WHERE clause

From Dev

extracting row values from a single table with a condition

Related Related

  1. 1

    Selecting single row with no unique key

  2. 2

    Selecting row from table in laravel

  3. 3

    How to select a single row where multiple rows exist from a table

  4. 4

    How to select a single row where multiple rows exist from a table

  5. 5

    Selecting row from Table - Java Selenium

  6. 6

    mySQL - Selecting the latest row from a table

  7. 7

    MySQL - selecting random row from large table

  8. 8

    selecting from table where timestamp is latest

  9. 9

    selecting unique rows from one table according to another table and then sorting it

  10. 10

    delete single row from group of unique - oracle

  11. 11

    delete single row from group of unique - oracle

  12. 12

    Efficiently selecting rows where a row in a related table exists

  13. 13

    Selecting Latest Row From MAX Row of 3rd Table

  14. 14

    What is the best way for selecting 2 row from table in one row?

  15. 15

    selecting records from main table and count of each row in another table

  16. 16

    Selecting a row in a html table

  17. 17

    Get unique row by single column where duplicates exist

  18. 18

    Is it possible to delete a single row using JPA repository where the rows are not unique?

  19. 19

    Selecting unique records from a table and giving weight to repetitions

  20. 20

    Selecting unique values from self-referencing table

  21. 21

    How to combine data from one table where there are duplicate values into a single row?

  22. 22

    How to combine data from one table where there are duplicate values into a single row?

  23. 23

    Selecting records where change has occurred from previous row

  24. 24

    Selecting two rows from another table using one row

  25. 25

    Selecting Data from Joomla SQL Table where any value is the variable

  26. 26

    SQL Selecting From One Table With Multiple Where Clauses

  27. 27

    Simple SQL query selecting from table where email = email

  28. 28

    MySQL - Selecting data from another table for a WHERE clause

  29. 29

    extracting row values from a single table with a condition

HotTag

Archive