Author |
Topic |
iradev
Starting Member
45 Posts |
Posted - 2010-11-10 : 06:44:07
|
Hello everyone,Basically I have the following code below which outputs applicant details including the geographical area they have registered from. If you look in the where clause, all the geographical areas are split in 4 teams. What I need to do is add another 4 cloumns: TeamA, TeamB, TeamC and TeamD and then count the number of applicants for each team. I could get rid of all other columns I currently have, if that would make it easier. All I need is to produce a report that outputs the number of applicants for each team. I have no idea how to approach this so any help would be appreciated! Thanks SELECT DISTINCTApps.ApplicantId,Objects.FullName,Apps.CreatedDate,'Area' = CASE WHEN dbo.ObAtts.AttributeId=78 THEN 'North East'WHEN dbo.ObAtts.AttributeId=79 THEN 'North West'WHEN dbo.ObAtts.AttributeId=80 THEN 'Yorkshire'WHEN dbo.ObAtts.AttributeId=81 THEN 'East Midlands'WHEN dbo.ObAtts.AttributeId=82 THEN 'West Midlands'WHEN dbo.ObAtts.AttributeId=83 THEN 'Eastern England'WHEN dbo.ObAtts.AttributeId=84 THEN 'South East England'WHEN dbo.ObAtts.AttributeId=85 THEN 'South West England'WHEN dbo.ObAtts.AttributeId=86 THEN 'London'WHEN dbo.ObAtts.AttributeId=87 THEN 'Scotland'WHEN dbo.ObAtts.AttributeId=88 THEN 'Wales'WHEN dbo.ObAtts.AttributeId=89 THEN 'Northern Ireland'ENDFROM AppsINNER JOIN Objects ON Apps.ApplicantId = Objects.ObjectidLEFT JOIN Sector ON Applicants.ApplicantId = Sector.ObjectIdINNER JOIN ObAtts ON Apps.ApplicantId = ObAtts.ObjectIdWHERE Sector.SectorId=9AND (Apps.StatusId) IN (57,58,60,61,62,63,64,65,69) AND Apps.CreatedDate BETWEEN '09/01/2010' AND '10/01/2010'AND ( --Team A ((ObAtts.AttributeId IN (78,79,80,87,88,89))) --Team B OR ((ObAtts.AttributeId IN (81,82,83,85))) --Team C OR ((ObAtts.AttributeId=84)) --Team D OR ((ObAtts.AttributeId=86)))ORDER BY Area |
|
Humate
Posting Yak Master
101 Posts |
Posted - 2010-11-10 : 07:00:12
|
Not 100% sure without the tables, but something like:SELECT CASE WHEN ObAtts.AttributeId IN (78,79,80,87,88,89) THEN TeamA WHEN ObAtts.AttributeId IN (81,82,83,85) THEN TeamB WHEN ObAtts.AttributeId =84 THEN TeamC WHEN ObAtts.AttributeId =86 THEN TeamD END as Team, COUNT(*) as Applicants FROM AppsINNER JOIN Objects ON Apps.ApplicantId = Objects.ObjectidLEFT JOIN Sector ON Applicants.ApplicantId = Sector.ObjectIdINNER JOIN ObAtts ON Apps.ApplicantId = ObAtts.ObjectIdWHERE Sector.SectorId=9AND (Apps.StatusId) IN (57,58,60,61,62,63,64,65,69) AND Apps.CreatedDate BETWEEN '09/01/2010' AND '10/01/2010' |
 |
|
iradev
Starting Member
45 Posts |
Posted - 2010-11-10 : 07:49:43
|
quote: Originally posted by Humate Not 100% sure without the tables, but something like:SELECT CASE WHEN ObAtts.AttributeId IN (78,79,80,87,88,89) THEN TeamA WHEN ObAtts.AttributeId IN (81,82,83,85) THEN TeamB WHEN ObAtts.AttributeId =84 THEN TeamC WHEN ObAtts.AttributeId =86 THEN TeamD END as Team, COUNT(*) as Applicants FROM AppsINNER JOIN Objects ON Apps.ApplicantId = Objects.ObjectidLEFT JOIN Sector ON Applicants.ApplicantId = Sector.ObjectIdINNER JOIN ObAtts ON Apps.ApplicantId = ObAtts.ObjectIdWHERE Sector.SectorId=9AND (Apps.StatusId) IN (57,58,60,61,62,63,64,65,69) AND Apps.CreatedDate BETWEEN '09/01/2010' AND '10/01/2010'
Thanks Humate! I got the following output: Team Applicant-----------------TeamA 1TeamA 8TeamA 8TeamB 5TeamB 8TeamB 13TeamC 28TeamB 9TeamD 57TeamA 1TeamA 4 Any idea how I can get the total for each team, likeTeam A 30Team B 35 instead of the many team rows? |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-11-10 : 08:04:11
|
select Team,sum(Applicants) as Applicants from(SELECT CASE WHEN ObAtts.AttributeId IN (78,79,80,87,88,89) THEN TeamAWHEN ObAtts.AttributeId IN (81,82,83,85) THEN TeamBWHEN ObAtts.AttributeId =84 THEN TeamCWHEN ObAtts.AttributeId =86 THEN TeamD END as Team, COUNT(*) as ApplicantsFROM AppsINNER JOIN Objects ON Apps.ApplicantId = Objects.ObjectidLEFT JOIN Sector ON Applicants.ApplicantId = Sector.ObjectIdINNER JOIN ObAtts ON Apps.ApplicantId = ObAtts.ObjectIdWHERE Sector.SectorId=9AND (Apps.StatusId) IN (57,58,60,61,62,63,64,65,69) AND Apps.CreatedDate BETWEEN '09/01/2010' AND '10/01/2010') as tMadhivananFailing to plan is Planning to fail |
 |
|
iradev
Starting Member
45 Posts |
Posted - 2010-11-10 : 08:18:52
|
quote: Originally posted by madhivanan select Team,sum(Applicants) as Applicants from(SELECT CASE WHEN ObAtts.AttributeId IN (78,79,80,87,88,89) THEN TeamAWHEN ObAtts.AttributeId IN (81,82,83,85) THEN TeamBWHEN ObAtts.AttributeId =84 THEN TeamCWHEN ObAtts.AttributeId =86 THEN TeamD END as Team, COUNT(*) as ApplicantsFROM AppsINNER JOIN Objects ON Apps.ApplicantId = Objects.ObjectidLEFT JOIN Sector ON Applicants.ApplicantId = Sector.ObjectIdINNER JOIN ObAtts ON Apps.ApplicantId = ObAtts.ObjectIdWHERE Sector.SectorId=9AND (Apps.StatusId) IN (57,58,60,61,62,63,64,65,69) AND Apps.CreatedDate BETWEEN '09/01/2010' AND '10/01/2010') as tMadhivananFailing to plan is Planning to fail
Thanks for the reply Madhivanan. I'm getting: Msg 4104, Level 16, State 1, Line 1The multi-part identifier "ObjectAttributes.AttributeId" could not be bound. |
 |
|
Humate
Posting Yak Master
101 Posts |
Posted - 2010-11-10 : 08:57:44
|
select Team,sum(Applicants) as Applicants from(SELECTCASE WHENObAtts.AttributeId IN (78,79,80,87,88,89) THEN TeamAWHEN ObAtts.AttributeId IN (81,82,83,85) THEN TeamBWHEN ObAtts.AttributeId =84 THEN TeamCWHEN ObAtts.AttributeId =86 THEN TeamD END as Team,COUNT(*) as ApplicantsFROM AppsINNER JOIN Objects ON Apps.ApplicantId = Objects.ObjectidLEFT JOIN Sector ON Applicants.ApplicantId = Sector.ObjectIdINNER JOIN ObAtts ON Apps.ApplicantId = ObAtts.ObjectIdWHERE Sector.SectorId=9AND (Apps.StatusId) IN (57,58,60,61,62,63,64,65,69)AND Apps.CreatedDate BETWEEN '09/01/2010' AND '10/01/2010') as tGROUP BY Team |
 |
|
iradev
Starting Member
45 Posts |
Posted - 2010-11-10 : 09:37:39
|
quote: Originally posted by Humate select Team,sum(Applicants) as Applicants from(SELECTCASE WHENObAtts.AttributeId IN (78,79,80,87,88,89) THEN TeamAWHEN ObAtts.AttributeId IN (81,82,83,85) THEN TeamBWHEN ObAtts.AttributeId =84 THEN TeamCWHEN ObAtts.AttributeId =86 THEN TeamD END as Team,COUNT(*) as ApplicantsFROM AppsINNER JOIN Objects ON Apps.ApplicantId = Objects.ObjectidLEFT JOIN Sector ON Applicants.ApplicantId = Sector.ObjectIdINNER JOIN ObAtts ON Apps.ApplicantId = ObAtts.ObjectIdWHERE Sector.SectorId=9AND (Apps.StatusId) IN (57,58,60,61,62,63,64,65,69)AND Apps.CreatedDate BETWEEN '09/01/2010' AND '10/01/2010') as tGROUP BY Team
Perfect! Thank you both for the quick response |
 |
|
|
|
|