SQL Server 2008 - Returning a result set with multiple rows based on max date and MIN Date

Hi Experts,

I have this query:
Select c.transactionDate
From ams_trans c inner join
(select MAX([transactionDate]) as maxdate From ams_trans group by transactionDate) c2
On c2.maxdate = c.transactionDate
Order by c.transactionDate

Open in new window


But my results are not what I am looking for:
transactionDate
2005-07-15 16:12:46.000
2005-07-15 16:12:46.000
2005-07-15 16:19:22.000
2005-07-18 09:31:29.000
2005-07-18 09:31:29.000
2005-07-18 11:00:36.000
2005-07-18 11:00:36.000
2005-07-20 04:04:59.000
2005-07-20 10:58:54.000
2005-07-20 14:21:49.000
2005-07-20 14:21:49.000
2005-07-20 16:13:38.000
2005-07-20 16:13:38.000
2005-07-21 08:32:49.000
2005-07-21 08:32:49.000
2005-07-21 08:36:49.000
2005-07-21 08:45:13.000
2005-07-21 15:14:59.000
2005-07-22 14:03:08.000
2005-07-22 14:03:08.000
2005-07-25 09:07:57.000
2005-07-25 09:07:57.000
2005-07-25 11:43:37.000
2005-07-25 11:45:24.000
2005-07-25 11:46:52.000
2005-07-25 11:47:34.000
2005-07-25 11:48:25.000
2005-07-25 11:50:22.000
2005-07-25 12:39:06.000
2005-07-25 12:48:04.000
2005-07-25 12:48:59.000
2005-07-26 10:38:08.000
2005-07-26 10:38:08.000
2005-07-26 10:44:08.000
2005-07-26 10:44:08.000
2005-07-26 18:16:36.000
2005-07-26 18:16:36.000
2005-07-27 09:30:16.000
2005-07-29 10:51:09.000
2005-08-01 17:31:21.000
2005-08-01 17:31:21.000

Open in new window


Record Set = Min(beginning of month) and MAX(Ending of each month)
I would like to be able to get a result set like this - one record for each record set  with the latest date and with the beginning date:
Column: MINDate                 MAXDate                                  MAXDate
             2005-07-15               2005-08-01                           2005-08-01

I know my query is not complete and it needs modifying to include the MIN date as well.

Maybe Experts are confused because I am doing this all wrong and need to create a program to loop with a StartDate/Enddate??? In which case please let me know to forget the query and do over???




Like Experts answered this question:
TSQL While Loop for a date range
Question by:  JFranks22 On 2012-01-25 03:33 PM  Views 3,143
Hi Experts,

We have a query that works well for one day, but... we need the data produced from the query for the past 4 years. This is a once off query, so performance is not an issue.

I am looking for a way to use a loop to run a query:
- Between a date range (Jan 1, 2008 - Jan 1, 2012), OR
- Use a count for the number of days - 4 years = 1460

Sample pseudo code:
DECLARE @StartDate = 1/1/2008

WHILE @StartDate =< 1/1/2012
{
     Print 'SELECT @StartDate, COUNT(*) FROM Reservations where DateCreated = @StartDate'
     @StartDate++;
}
END

Not sure if we need to write to a temp table and then select the results from there.

Utlimately, the result should be a result table with the result per day:
Date | Result
1/1/2008 | 15
2/1/2008 | 22
3/1/2008 | 9
4/1/2008 | 11
5/1/2008 | 13
6/1/2008 | 17

Thanks for your thoughts!

Please help and thanks....


The only difference is that I am not hardcoding my dates.  Like:
Between a date range (Jan 1, 2008 - Jan 1, 2012),

I am using the fields from the table like:
Between a date range (MIN([transactionDate]) - MAX([transactionDate])), And each is by month.


Please help and thanks.....
Amour22015Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
You have provided the desired result:
Column: MINDate                 MAXDate                                  MAXDate
             2005-07-15               2005-08-01                           2005-08-01

Open in new window

Please post sampe start data.
Vikas GargAssociate Principal EngineerCommented:
HI

You can try this

select year(transactionDate) yr, month(transactionDate) mnth, MIN(transactionDate) controldate, MAX(transactionDate) lastDate FROM [ncausa].[dbo].[ams_trans]
                               group by year(transactionDate) , month(transactionDate)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
This is not a similar question of this one?
Amour22015Author Commented:
OK great thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.