Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting a count using datename... 1

Status
Not open for further replies.

gtjr92

Programmer
May 26, 2004
96
I am trying to get a count of entries per month. The date created field is the field that holds the date of an entry.
When i run the below query it only gives me data from one day for each month, it does not iterate through the whole month. How can i have it so it will give me the total count from each month.
Code:
Select  
 distinct datename(Month,DateCreated)As Month,
 datepart(Month,DateCreated)As Monthid,
 datepart(year,DateCreated) As Year, 
 count(*) As EntryCount 
   from blog_entry  
    WHERE blog_Entry.IsDeleted = 0 and 
  datename(Month,DateCreated)=datename(Month,DateCreated)
group by datecreated
order by monthid,year
 
the first part that's weird is

datename(Month,DateCreated)=datename(Month,DateCreated)

that's always gonna be true, don't you think? :)

perhaps you meant this --

datename(Month,DateCreated)=datename(Month,getdate())

the next part that's not quite right is this --

group by datecreated

this will give you one row in the result for each value of datecreated


r937.com | rudy.ca
 
i changed the datename(Month,DateCreated)=datename(Month,getdate())
like you suggested. if i run the query with that and keep the datecreated group clause i do not get any rows returned. IF i remove the group by datecreated i get
Column DateCreated is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
 
hint: in the GROUP BY, put the columns that you want a count of entries for -- probably year and month



r937.com | rudy.ca
 
tried it still get no rows.
tried this
Code:
group by datename(Month,DateCreated),datepart(Month,DateCreated),datepart(year,DateCreated),datecreated
also tried adding just the datepart month and date created fields in the group by same thing no rows returned.
 
No rows = WHERE clause is f**ed up.

There are some other weirdos in this query... later.

------
[small]<this is sig>
select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')
</this is sig>[/small]
[banghead]
 
There are some other weirdos in this query... later. "
well then how about helping me out instead of making snide remarks!
 
if datecreated is in your GROUP BY, then you are going to get one row in the result for every different day

please show your entire query

r937.com | rudy.ca
 
my entire query was in my first post, but here it is with the changes i made after reading the previous posts
Code:
	Select  distinct datename(Month,DateCreated)As Month,datepart(Month,DateCreated)As Monthid,
datepart(year,DateCreated) As Year, count(*) As EntryCount 
 from blog_entry  
WHERE blog_Entry.IsDeleted = 0 and datename(Month,DateCreated)=datename(Month,getdate())
group by datename(Month,DateCreated),datepart(Month,DateCreated),datepart(year,DateCreated),datecreated
order by monthid,year

if i just group by date created, or any of the datepart/datename i get no rows returned. when i do my original query i only get results back for one day of each month instead of a total of all days for each months

original query
Code:
Select  
 distinct datename(Month,DateCreated)As Month,
 datepart(Month,DateCreated)As Monthid,
 datepart(year,DateCreated) As Year, 
 count(*) As EntryCount 
   from blog_entry  
    WHERE blog_Entry.IsDeleted = 0 and 
  datename(Month,DateCreated)=datename(Month,DateCreated)
group by datecreated
order by monthid,year
 
try this --
Code:
select datename(Month,DateCreated) as Month
     , datepart(Month,DateCreated) as Monthid
     , datepart(Year,DateCreated)  as Year
     , count(*)       as EntryCount 
  from blog_entry  
 where blog_entry.IsDeleted = 0 
   and datename(Month,DateCreated)
     = datename(Month,getdate())
group 
    by datename(Month,DateCreated) 
     , datepart(Month,DateCreated) 
     , datepart(Year,DateCreated) 
order 
    by Monthid
     , Year

r937.com | rudy.ca
 
what about this?

Code:
Select  
 distinct datename(Month,DateCreated)As Month,
 datepart(Month,DateCreated)As Monthid,
 datepart(year,DateCreated) As Year, 
 count(*) As EntryCount 
   from blog_entry  
    WHERE blog_Entry.IsDeleted = 0 
group by datename(Month,DateCreated),datepart(year,DateCreated)
order by monthid,year



Denis The SQL Menace
SQL blog:
Personal Blog:
 
Yup, that and... better swap monthid and [year] in ORDER BY clause - in case data spans across many years.

------
[small]<this is sig>
select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')
</this is sig>[/small]
[banghead]
 
Denis, that's hardly fair -- you could at least include a link to a web site somewhere where they do a credible job of defining sargable :)

(i have one or two such links, but i can't find them at the moment -- maybe you have one?)

also, DISTINCT is not required (in this example) because of the GROUP BY

r937.com | rudy.ca
 
Blah... forgetit [dazed], query already selects only data from May only (all years?).

About sarg thing... here.

------
[small]<this is sig>
select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')
</this is sig>[/small]
[banghead]
 
thanks, vongrunt, that's one of the links i had

key point: "expressions that include a function on a column... are not sargable"


r937.com | rudy.ca
 
so change
and datename(Month,DateCreated) = datename(Month,getdate())

To

and DateCreated >= DATEADD(mm, DATEDIFF(mm, 0, GETDATE())+0, 0)
and DateCreated < DATEADD(mm, DATEDIFF(mm, 0, GETDATE())+1, 0)

Here are some ways to make your queries sargable ( yes indeed selfish selfpromotion ;-)


Denis The SQL Menace
SQL blog:
Personal Blog:
 
I already tried rudy's query as i stated in my 2nd post
"if i run the query with that and keep the datecreated group clause i do not get any rows returned"

other ideas?
i appreciate all the help..
 
tried sqldenis too still i get no rows
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top