rockit
rockit

Reputation: 3796

SQL server 2008: How to get the start date and end date of my data?

As a newb, I already know that I will be berated for asking this question, but I did not find the answer on the site here and could use some help...

I have a table that lists data by the day, and by type. For example

Transaction  |  Date  | Type
-----------------------------
Updat    | 11/7/2008  | Cash-out
Update   | 11/10/2008 | Wrote-check 
Deposit  | 11/11/2009 | Cashed Check 
Update   | 11/18/2008 | Wrote check 
Deposit  | 11/19/2009 | Cashed Check 

What I'm trying to do, is find the very first occurrence of each transaction type, and the very last occurrence of each transaction type. so I'm trying to figure out an sql statement that I can write that will return something like this:

Transaction  |  First Date  | Last Date  |
----------------------------------------------
Update       | 11/7/2008    | 11/18/2008 |
Deposit      | 11/11/2009   | 1/19/2009  |

any ideas?

Upvotes: 1

Views: 504

Answers (2)

Russell Steen
Russell Steen

Reputation: 6612

SELECT Transaction, Min([date])  AS [First Date] , Max([Date]) AS [Last Date]
FROM myTable GROUP BY Transaction

Upvotes: 10

Tom H
Tom H

Reputation: 47402

SELECT
     transaction,
     MIN([date]) AS [First Date],
     MAX([date]) AS [Last Date]
FROM
     My_Table
GROUP BY
     transaction

Upvotes: 3

Related Questions