Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Multiple Counts

Author  Topic 

ljw121
Starting Member

3 Posts

Posted - 2009-06-27 : 10:41:31
Hi,

As an overview of what I'm trying to: I've got a table which has got the following columns:

JobNumber | JobTitle | JobBy | JobDate

And this could filled out as follows:

JobNumber | JobTitle | JobBy | JobDate
------------------------------------------
1 | Example 1| Greg | 2009-07-01
2 | Example 2| Fred | 2009-07-02
3 | Example 3| Fred | 2009-07-01
4 | Example 4| Greg | 2009-07-03
5 | Example 5| Greg | 2009-07-01
6 | Example 6| Fred | 2009-07-01
7 | Example 7| Greg | 2009-07-02

The table is self explanatory and shows when jobs are scheduled for completion and who by. Now what I want to know is how much work each person has scheduled on a certain day. My ideal output would be something like this:

JobDate | Fred | Greg
-------------------------
2009-07-01 | 2 | 2
2009-07-02 | 1 | 1
2009-07-02 | 0 | 1


So far by experimenting with Count and Group By I've been able to get this information by running two individual selects which is okay if there are only two people but if there are a lot of people then this becomes impractical. Is there a way to get this information as I've displayed it?

Any help would be much appreciated!!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-27 : 10:53:02
[code]
select JobDate,
Fred = count(case when JobBy = 'Fred' then 1 end),
Greg = count(case when JobBy = 'Greg' then 1 end)
from Jobs
group by JobDate
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

TheSQLGuru
SQL Server MVP

10 Posts

Posted - 2009-07-05 : 10:53:18
the given example works if the values you want to count by are known and fixed. Otherwise, search the web for sql server dynamic crosstab to find solutions for that problem.

Kevin G Boles
TheSQLGuru
Indicium Resources, Inc.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-07-05 : 12:58:02
here is one

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx
Go to Top of Page
   

- Advertisement -