TSQL - Taking a range of dates per month into three different columns

Hi Experts,

I so far have this Query:
Select min([transactionDate]) AS ControlDate, max([transactionDate]) AS LastDate
  FROM [ncausa].[dbo].[ams_trans]

Open in new window


But this gives me the start and end of all that is in the table.  I want it to be by month.

But it is not working the way I need it to and it was mentioned to use a DataAdd with looping to get what I want?

Here is the input from table.transactionDate :
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 10:58:54.000
2005-07-20 14:21:49.000
2005-07-20 14:21:49.000
2005-07-20 04:04:59.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

Open in new window


But this is what I am looking for:
Like:
Column: ControlDate                 LastDay                                 LastDay
             2005-07-15               2005-08-01                           2005-08-01

Open in new window


Please help and thanks...
Amour22015Asked:
Who is Participating?
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.

Kyle AbrahamsSenior .Net DeveloperCommented:
You can group by the year and month.  
    select DATEPART(year, transactionDate) yr, DATEPART(month, transactionDate) mnth, MIN(transactionDate) controldate, MAX(transactionDate) lastDate FROM [ncausa].[dbo].[ams_trans]
                               group by DATEPART(year, transactionDate), DATEPART(month, transactionDate)

Open in new window

0
Mayank GairolaSr. Application Support Engg.Commented:
So if I am understanding the question correctly then you want the query to return a single row for every month having columns as min date and max date twice. Not sure why you want the same column twice but the below query should work.

select  MIN(transactionDate) controldate, MAX(transactionDate) lastDate, MAX(transactionDate) lastDay
FROM [ncausa].[dbo].[ams_trans]
group by DATEPART(year, transactionDate), DATEPART(month, transactionDate)
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
You can filter by the current year and month but then the 1st of August won't be returned:
SELECT min([transactionDate]) AS ControlDate, max([transactionDate]) AS LastDate
FROM [ncausa].[dbo].[ams_trans]
WHERE YEAR(transactionDate)=YEAR(GETDATE()) AND MONTH(transactionDate)=MONTH(GETDATE())

Open in new window

0

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
Amour22015Author Commented:
Great
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.