SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


COUNT and NULL


COUNT and NULL

Author
Message
wayne.cursor
wayne.cursor
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 18
Hi SQL Gurus,

I have a question about using the COUNT function. How can I write a query that will return the count value of records created within a certain time frame... say the last 7 days, grouped by a CATEGORY column?


so my database table would have the fields...

name, category, createdDate


And i want my query to generate a recordset that looks like


Category, Count
--------,------
category1, 10
category2, 20
category3, 15


which is displaying the Count of number of records created in the last 7 days, for each Category.

Any ideas would be greatly appreciated.

Thanks

Wayne
Mahesh Bote
Mahesh Bote
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2676 Visits: 1286

Select category, count(*) From {Table}
Where createdDate Between {start date} And {end date}
Group By category



confirm on this.

Mahesh


MH-09-AM-8694
wayne.cursor
wayne.cursor
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 18
Hi Mahesh

thanks for your quick reply.

What you have posted is exactly what i was after. Except, it doesn't return all categories. i think it ignores Categories which have a 0 (zero) count value.

any ideas on how to get around this?

Thanks

Wayne
wayne.cursor
wayne.cursor
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 18
Actually, thinking a bit further down the track... what i am actually after is...


I have a table like this:

name, category, createdDate, status, statusDate


and i want to create a recordset like this:

category, created_Count, status_Count
--------, -------------, -------------
category1, 10, 6
category2, 0, 6
category3, 10, 0
category4, 0, 0


which is displaying the Count of number of records created in the last 7 days for each Category, and the Count of number of records at a particular status for each category which have achieved that status in the last 30 days. Including showing categories that have a 0(zero) count.


am i asking the impossible?

Again, thanks for all assistance :-)


Wayne
Mahesh Bote
Mahesh Bote
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2676 Visits: 1286
...am i asking the impossible?


Certainly not. I have suggested the solution as per the 1st post. You havn't mentioned these possiblities, otherwise I would have been considered it as well.

anyways, please give some more details on this with real life data.

Mahesh


MH-09-AM-8694
wayne.cursor
wayne.cursor
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 18
Hi Mahesh,

here is the report that im trying to generate:

notice how there are only Skill_Groups listed that have COUNT values for them. I also need to show Skill_Groups that do not have any Jobs_Created_Last_7_Days, etc.


Here is the source table with data that i am querying.

the table is called "ftjoborder"
Mahesh Bote
Mahesh Bote
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2676 Visits: 1286


...I also need to show Skill_Groups that do not have any Jobs_Created_Last_7_Days...



Select category, count(dbjobskillgrp), count(dbjobstatus)
From {Table}
Where (dbjobcreatedDate Between {start date} And {end date} Or dbjobcreatedDate Is Null)
Group By category



confirm on this.

Mahesh


MH-09-AM-8694
ChiragNS
ChiragNS
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6715 Visits: 1865
Hi

What about using a Left join instead of "OR". This will have a better execution plan.

"Keep Trying"
Mahesh Bote
Mahesh Bote
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2676 Visits: 1286
Chirag (4/29/2008)
Hi

What about using a Left join instead of "OR". This will have a better execution plan.


Do you mean, joining TableA to TableA with left outer join will give better performance than using or?

Mahesh



MH-09-AM-8694
ChiragNS
ChiragNS
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6715 Visits: 1865
Experts correct me if iam wromg

when OR is used sql server finds it difficult to use the right index , mostly it does a table scan. This can bring down the performance .

"Keep Trying"
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search