What is the most efficient manner to use MIN,MAX transaction dates - and getting amounts of these individual transactions

Mez

Say I want to display the first, last transactions of our users, and display these amounts. This can be done by

 SELECT user, min(Date) firstDate, max(Date) lastDate
   INTO #MinAndMax
   FROM transactionTable
  GROUP BY user

Then I do another inner select statement, to get the amount based on the value obtained above when doing the min(Date), so like the following;

 SELECT (SELECT amount
           FROM transactionTable
          WHERE date = firstDate) AS firstAmount
   FROM #MinAndMax

However is there a more efficient manner to do this, like pivoting?

GarethD

If might be faster to use analytic functions to rank your transactions by user, and select the first for each:

WITH CTE AS
(   SELECT  [User], 
            FirstDate = Date, 
            FirstAmount = Amount, 
            RowNum = ROW_NUMBER() OVER(PARTITION BY [User] ORDER BY Date),
            LastDate = MAX(Date) OVER(PARTITION BY [User])
    FROM    TransactionTable
)
SELECT  [User], FirstDate, FirstAmount, LastDate
FROM    CTE
WHERE   RowNum = 1;

If, as in your query you only need the amount, your query could simply be. If you have a user table, then it might be faster still to use APPLY along with TOP:

SELECT  u.[User], FirstAmount = t.Amount
FROM    Users u
        CROSS APPLY
        (   SELECT  TOP 1 Amount
            FROM    TransactionTable t
            WHERE   t.[User] = u.[User]
            ORDER BY t.Date
        ) t;

EDIT

To get the first and last amounts you would need a second ROW_NUMBER function:

WITH CTE AS
(   SELECT  [User], 
            Date, 
            Amount, 
            RowNumAsc = ROW_NUMBER() OVER(PARTITION BY [User] ORDER BY Date),
            RowNumDesc = ROW_NUMBER() OVER(PARTITION BY [User] ORDER BY Date DESC)
    FROM    TransactionTable
)
SELECT  [User], 
        FirstDate = MIN(CASE WHEN RowNumAsc = 1 THEN Date END), 
        FirstAmount = MIN(CASE WHEN RowNumAsc = 1 THEN Amount END),
        LastDate = MIN(CASE WHEN RowNumDesc = 1 THEN Date END),
        LastAmount = MIN(CASE WHEN RowNumDesc = 1 THEN Amount END) 
FROM    CTE
WHERE   1 IN (RowNumAsc, RowNumDesc)
GROUP BY [User];

Or

SELECT  u.[User], 
        FirstDate = f.Date,
        FirstAmount = f.Amount,
        LastDate = l.Date,
        LastAmount = l.Amount
FROM    Users u
        CROSS APPLY
        (   SELECT  TOP 1 Amount, Date
            FROM    TransactionTable t
            WHERE   t.[User] = u.[User]
            ORDER BY t.Date
        ) AS f
        CROSS APPLY
        (   SELECT  TOP 1 Amount, Date
            FROM    TransactionTable t
            WHERE   t.[User] = u.[User]
            ORDER BY t.Date DESC
        ) AS l;

Example on SQL Fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the most efficient way to use the or operator in C?

From Dev

What is the most efficient selector to use with findElement()?

From Dev

What is the most efficient way to use the or operator in C?

From Java

What's the most efficient way to extract min, max & median from a vector

From Dev

Making massive amounts of individual row updates faster or more efficient

From Dev

Getting min and max Dates from a pandas dataframe

From Dev

What is the easiest and most efficient way to make a min heap in Scala?

From Dev

Most efficient way to add individual digits of a number

From Dev

What is the most efficient/fastest way to use a jQuery selector multiple times?

From Dev

What is the correct/best/most efficient way to use centralised version control?

From Dev

What's the most efficient way to use Angular component/directive bindings?

From Dev

Most efficient way of downloading large amounts of data to android?

From Dev

Most efficient way to collect and save large amounts of data in background in android

From Dev

SQL Server 2008 - Calculate min and max dates of contribution amounts made in chronological order

From Dev

SQL Server 2008 - Calculate min and max dates of contribution amounts made in chronological order

From Dev

What is most Efficient SQL Query?

From Dev

How do I use XPath in a thread-safe and efficient manner?

From Dev

Getting data between two dates and grouping it by individual dates

From Dev

PyAudio : What is the most efficient format and pack/unpack method to use in callback mode?

From Dev

What is the most efficient way to use another character instead of a space on a web page?

From Dev

What is the most efficient way to use Git for a version control beginner, UI Application or Terminal?

From Dev

boost::geometry Most efficient way of measuring max/min distance of a point to a polygon ring

From Dev

What is the most efficient way to implement GroupBy in Javascript?

From Dev

What is the most efficient way to sync two datagridviews?

From Dev

What is the most efficient way to declare functions in Javascript?

From Dev

What is the most efficient way to list a directory?

From Dev

what is the most efficient way of counting occurrences in pandas?

From Dev

What is the most efficient way to subdivide a large list?

From Dev

What is the most efficient way to generate a UUID in PHP?

Related Related

  1. 1

    What is the most efficient way to use the or operator in C?

  2. 2

    What is the most efficient selector to use with findElement()?

  3. 3

    What is the most efficient way to use the or operator in C?

  4. 4

    What's the most efficient way to extract min, max & median from a vector

  5. 5

    Making massive amounts of individual row updates faster or more efficient

  6. 6

    Getting min and max Dates from a pandas dataframe

  7. 7

    What is the easiest and most efficient way to make a min heap in Scala?

  8. 8

    Most efficient way to add individual digits of a number

  9. 9

    What is the most efficient/fastest way to use a jQuery selector multiple times?

  10. 10

    What is the correct/best/most efficient way to use centralised version control?

  11. 11

    What's the most efficient way to use Angular component/directive bindings?

  12. 12

    Most efficient way of downloading large amounts of data to android?

  13. 13

    Most efficient way to collect and save large amounts of data in background in android

  14. 14

    SQL Server 2008 - Calculate min and max dates of contribution amounts made in chronological order

  15. 15

    SQL Server 2008 - Calculate min and max dates of contribution amounts made in chronological order

  16. 16

    What is most Efficient SQL Query?

  17. 17

    How do I use XPath in a thread-safe and efficient manner?

  18. 18

    Getting data between two dates and grouping it by individual dates

  19. 19

    PyAudio : What is the most efficient format and pack/unpack method to use in callback mode?

  20. 20

    What is the most efficient way to use another character instead of a space on a web page?

  21. 21

    What is the most efficient way to use Git for a version control beginner, UI Application or Terminal?

  22. 22

    boost::geometry Most efficient way of measuring max/min distance of a point to a polygon ring

  23. 23

    What is the most efficient way to implement GroupBy in Javascript?

  24. 24

    What is the most efficient way to sync two datagridviews?

  25. 25

    What is the most efficient way to declare functions in Javascript?

  26. 26

    What is the most efficient way to list a directory?

  27. 27

    what is the most efficient way of counting occurrences in pandas?

  28. 28

    What is the most efficient way to subdivide a large list?

  29. 29

    What is the most efficient way to generate a UUID in PHP?

HotTag

Archive