SQL Query

Nearly there with my query now.

All i need to be able to do is show results only from the current month and back the last Twelve Months, also need this to be dynamic so when the month changes the query follows.

The code is below

SELECT     TOP (100) PERCENT DATENAME(month, createdon) + DATENAME(year, createdon) AS Month, projcode, result, COUNT(*) AS Expr2
FROM         dbo.dmsproj
GROUP BY createdon, DATENAME(month, createdon) + DATENAME(year, createdon), projcode, result
ORDER BY YEAR(createdon), MONTH(createdon)

Thanks

John
pepps11976Asked:
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.

Alpesh PatelAssistant ConsultantCommented:
SELECT     TOP (100) PERCENT DATENAME(month, createdon) + DATENAME(year, createdon) AS Month, projcode, result, COUNT(*) AS Expr2
FROM         dbo.dmsproj
where  DATENAME(month, createdon) = DATENAME(month, GETDATE()) and DATENAME(year, createdon) = DATENAME(year, GETDATE())
GROUP BY createdon, DATENAME(month, createdon) + DATENAME(year, createdon), projcode, result
ORDER BY YEAR(createdon), MONTH(createdon)
0
mayank_joshiCommented:
SELECT     TOP (100) PERCENT DATENAME(month, createdon) + DATENAME(year, createdon) AS Month, projcode, result, COUNT(*) AS Expr2
FROM         dbo.dmsproj
where  DATENAME(month, createdon) between
 DATENAME(month, GETDATE()) and DATENAME(month, dateadd(month,-12,GETDATE()))
and  DATENAME(year, createdon) between DATENAME(year, GETDATE())
and DATENAME(year, dateadd(month,-12,GETDATE()))
GROUP BY createdon, DATENAME(month, createdon) + DATENAME(year, createdon), projcode, result
ORDER BY YEAR(createdon), MONTH(createdon)
	 

Open in new window

0
pepps11976Author Commented:
Hi Tried the query but it returns nothing
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

mayank_joshiCommented:
sorry you should use DATEPART:-

SELECT     TOP (100) PERCENT DATENAME(month, createdon) + DATENAME(year, createdon) AS Month, projcode, result, COUNT(*) AS Expr2
FROM         dbo.dmsproj
where  DATEPART(month, createdon) between
 DATEPART(month, GETDATE()) and DATEPART(month, dateadd(month,-12,GETDATE()))
and  DATEPART(year, createdon) between DATEPART(year, GETDATE())
and DATEPART(year, dateadd(month,-12,GETDATE()))
GROUP BY createdon, DATENAME(month, createdon) + DATENAME(year, createdon), projcode, result
ORDER BY YEAR(createdon), MONTH(createdon)
	 

Open in new window

0
pepps11976Author Commented:
Tried again but the query is still returning nothing
0
pepps11976Author Commented:
Anyone???
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this should do:
SELECT     TOP (100) PERCENT DATENAME(month, createdon) + DATENAME(year, createdon) AS Month, projcode, result, COUNT(*) AS Expr2
FROM         dbo.dmsproj
where  createdon >= DATEADD(MONTH, -12, convert(datetime, convert(varchar(8), getdate(), 120) + '01', 120))
GROUP BY createdon, DATENAME(month, createdon) + DATENAME(year, createdon), projcode, result
ORDER BY YEAR(createdon), MONTH(createdon)

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
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.