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 2005 Forums
 Transact-SQL (2005)
 Help with an annoying SELECT

Author  Topic 

Shakazahn
Starting Member

2 Posts

Posted - 2010-03-18 : 16:35:22
Hi,

I have a table with alot of company names and their IDs, and another one linked by that ID with alot of their activities. So the same company usually can have 100+ activities and this list keep growing everyday.

I need to get a report containing all the companies and data and the amount of activities separated by completed/succesful/etc. This is what I did:

SELECT c.CompanyID, c.CompanyData,
SUM(CASE WHEN a.ActivityStatus = 1 THEN 1 ELSE 0 END) AS Ongoing,
SUM(CASE WHEN a.ActivityStatus = 2 THEN 1 ELSE 0 END) AS Sucessful,
SUM(CASE WHEN a.ActivityStatus = 3 THEN 1 ELSE 0 END) AS Unsucessful,
COUNT(a.ActivityStatus) AS TotalActivities
FROM tbl_Company c
LEFT JOIN tbl_Activities a ON a.CompanyID = c.CompanyID
GROUP BY c.CompanyID, c.CompanyData
ORDER BY c.CompanyData



CompanyID CompanyData Ongoing Sucessful Unsuccessful Total
----------------------------------------------------------------
1 CompanyA 102 50 10 162
2 CompanyB 210 80 15 305


However, the viewer (as always unsatisfied) now wants to see the results separated by weeks according to the time period he/she choose. Like this:


---------- Week 1 ---------- ---------- Week 2 ----------
CompanyID CompanyData Ongoing Sucessful Unsuccessful Ongoing Sucessful Unsuccessful Total
-------------------------------------------------------------------------------------------------
1 CompanyA 52 20 5 30 50 5 162
2 CompanyB 110 45 15 100 30 5 305


I've been struggling for quite some time and can't find any solution to this, please help!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-19 : 12:31:04
is this to show in a sql report? then use matrix as container

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -