Solved

# Max Date for Each Month

Posted on 2011-10-21
326 Views
Here is the code I currently have, but I want to pull just the max day for each of the 12 months from the database. For example for the month 01 we may have
01
02
03
04
07
28

I would want to pull ONLY 01/28/2011
and so on for each month.
Here is the code I have already, but I'm stuck gathering ONLY the MAX date for month.

USE VDBA
SELECT [DateTime]
,RIGHT(Year(DateTime),2) AS YearPart
,RIGHT('0'+cast(Month(DateTime) AS VARCHAR(2)),2) AS MonthPart
,DAY(DateTime) as SampleDay
,RIGHT('0'+cast(Month(DateTime) AS VARCHAR(2)),2)+'/'+right(Year(DateTime),2) AS ChartDate
,[DataSpaceUsage]
,[IndexSpaceUsage]
,[SpaceAvailable]
,[Size]
,[SpaceAvailableInMb]
FROM [VDBA].[dbo].[SQLDatabaseSize]
WHERE DatabaseName = 'VDBA' and DateTime>'2011'
ORDER BY DateTime
0
Question by:concretesailors

LVL 23

Accepted Solution

What is the result of this query?

SELECT a.* FROM
[VDBA].[dbo].[SQLDatabaseSize] a
INNER JOIN
(
SELECT Max([DateTime]) [DateTime]
,RIGHT(Year(DateTime),2) AS YearPart
,RIGHT('0'+cast(Month(DateTime) AS VARCHAR(2)),2) AS MonthPart
FROM [VDBA].[dbo].[SQLDatabaseSize]
WHERE DatabaseName = 'VDBA' and DateTime>'2011'
GROUP BY RIGHT(Year(DateTime),2)
,RIGHT('0'+cast(Month(DateTime) AS VARCHAR(2)),2)
) B on a.[DateTime] = b.[DateTime]
0

LVL 23

Assisted Solution

SELECT a.* FROM
[VDBA].[dbo].[SQLDatabaseSize] a
INNER JOIN
(
SELECT Max([DateTime]) [DateTime]
,RIGHT(Year(DateTime),2) AS YearPart
,RIGHT('0'+cast(Month(DateTime) AS VARCHAR(2)),2) AS MonthPart
FROM [VDBA].[dbo].[SQLDatabaseSize]
WHERE DatabaseName = 'VDBA' and DateTime>'2011'
GROUP BY RIGHT(Year(DateTime),2)
,RIGHT('0'+cast(Month(DateTime) AS VARCHAR(2)),2)
) B on a.[DateTime] = b.[DateTime]
WHERE a.DatabaseName = 'VDBA'
0

LVL 1

Assisted Solution

USE VDBA
SELECT MAX([DateTime])
,RIGHT(Year(DateTime),2) AS YearPart
,RIGHT('0'+cast(Month(DateTime) AS VARCHAR(2)),2) AS MonthPart
,MAX(DAY(DateTime)) as SampleDay
,MAX(RIGHT('0'+cast(Month(DateTime) AS VARCHAR(2)),2)+'/'+right(Year(DateTime),2)) AS ChartDate
,MAX([DataSpaceUsage])
,MAX([IndexSpaceUsage])
,MAX([SpaceAvailable])
,MAX([Size])
,MAX([SpaceAvailableInMb])
FROM [VDBA].[dbo].[SQLDatabaseSize]
WHERE DatabaseName = 'VDBA' and DateTime>'2011'
GROUP BY RIGHT(Year(DateTime),2))
,RIGHT('0'+cast(Month(DateTime) AS VARCHAR(2)),2))
ORDER BY MAX([DateTime])
0

LVL 7

Author Comment

I don't understand why all the MAX syntax?
0

LVL 7

Author Closing Comment

I broke the points apart, they ALL worked, and gave me grounds to build upon.

RajkumarGS actually granted me the ability to view every database, instance name, size, etc that were in the database.all at once. All grand scripts, and I'm glad and thankful you both answered so quickly. Wish they allowed us to grant more points.
0

## Featured Post

### Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…