Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The task is supposed to run once a year and send a notification to the Personnel Manager informing us about the number of active crew.

SQL Statement

Expand
titleSQL Statement
Code Block
languagesql
declare @HTV varchar(max);
set @HTV = cast(
				(select td = Convert(varchar(40),d.Org)
					+'</td><td>' + Isnull(Convert(varchar,d.Employed),'')
					+'</td><td>' + Isnull(Convert(varchar,d.ActEmployees),'')
				from (select o.[NAME] Org,
						(Select Count(PIN) From (
						Select X.PIN From
						(Select a.PIN From AUDIT_PW001P01 a 
						where dbo.ad_IsClient(a.CLIENT,o.NUMORGID)=1 and a.repl_GMTModifiEDDate>=DATEADD(year,-1,getdate())
							and a.EMPLOYMENTSTARTDATE is not null and (a.EMPLOYMENTENDDATE is null or a.EMPLOYMENTENDDATE>a.repl_ModifiedDate)
						Union all
						Select p.PIN From PW001P01 p 
						where dbo.ad_IsClient(p.CLIENT,o.NUMORGID)=1 
							and p.EMPLOYMENTSTARTDATE is not null and (p.EMPLOYMENTENDDATE is null or p.EMPLOYMENTENDDATE>DATEADD(year,-1,getdate()))
						) X
						Group by x.PIN) Y) Employed,
						(Select Count(PIN) From (
						Select PIN From PW001P03 Where Code in (Select CODE From PW001C12 Where OPTIONS like '%S%')
										and Convert(date,DATEFROM)<=Convert(date,getdate())
										and Convert(date,Isnull(Isnull(DATETO,TODATEESTIMATED),Getdate()))>=Convert(date,dateadd(year,-1,getdate()))
										and PLANNED!='Y'
										and PIN in (Select PIN From AUDIT_PW001P01 where dbo.ad_IsClient(CLIENT,o.NUMORGID)=1)
						Group by PIN) Y) ActEmployees
					from pworg o
					where o.orgtype=2 and o.[NAME] like '%crew%pool%' 
				      ) as d
				order by d.Org asc
				for xml path( 'tr' ), type)
			as varchar(max) )

set @HTV = '<table cellpadding="3" cellspacing="0" border="1">'
          +'<tr><th>Organization</th><th>Employed Crew</th><th>Active Crew</th></tr>'
          +Replace(Replace(Replace(@HTV,
							'&lt;', '<'), '&gt;', '>')
							,'>bgcolor=',' bgcolor=')
          +'</table>'
Select 
@HTV as #b,
'Artem.Finkelshtein@Adonishr.com' as #email

Field Specification

Organization - Crew Pool name;

...