Is there any huge issue with the following SQL? This should be getting timesheets for only one week and it is a DOG!!
Code:
select s.surname + ', ' + s.forename EmpName
,b.businessunit BusinessUnit
,r.region Region
,case when isnull(c.firstname,'') = '' then c.searchname else c.searchname + ', ' + c.firstname end ClientName
,f.serviceline Team
,convert(char(13),w.date,101) Date
,j.name JobName
,ca.description Activity
,w.chargetypeid
,w.detail
,case when w.jobischargeable = 1 then 'Chargeable' else 'Non-Chargeable' end ChargeStatus
,case when w.chargetypeid=24 then isnull(W.Units,0) else 0 end as HoursWorked
,case when w.chargetypeid=25 and w.[ExpenseTypeID] = 2 then isnull(w.units,0) else 0 end as Miles
,case when w.chargetypeid=25 and w.[ExpenseTypeID] = 1 then isnull(w.rate*w.units,0) else 0 end as Meals
,case when w.chargetypeid=25 and w.[ExpenseTypeID] NOT IN (1,2) then isnull(w.rate*w.units,0) else 0 end as OtherExp
,case when datepart(dw,w.date) = 2 and w.chargetypeid = 24 then isnull(w.units,0) else 0 end MonHrs
,case when datepart(dw,w.date) = 3 and w.chargetypeid = 24 then isnull(w.units,0) else 0 end TuesHrs
,case when datepart(dw,w.date) = 4 and w.chargetypeid = 24 then isnull(w.units,0) else 0 end WedHrs
,case when datepart(dw,w.date) = 5 and w.chargetypeid = 24 then isnull(w.units,0) else 0 end ThursHrs
,case when datepart(dw,w.date) = 6 and w.chargetypeid = 24 then isnull(w.units,0) else 0 end FriHrs
,case when datepart(dw,w.date) = 7 and w.chargetypeid = 24 then isnull(w.units,0) else 0 end SatHrs
,case when datepart(dw,w.date) = 1 and w.chargetypeid = 24 then isnull(w.units,0) else 0 end SunHrs
from ssrsWIP w
join tblstaff s on w.staffid = s.staffid
join tblserviceline F on w.staffservicelineid = F.servicelineid
join tblbusinessunit b on w.staffbusinessunitid = b.businessunitid
join sgtmarket mk on F.marketid = mk.marketid
join sgtregion r on mk.regionid = r.regionid
join tblclient c on w.clientid = c.clientid
join tbljob j on w.jobid = j.jobid
join tblchargeaccs ca on w.chargeacid = ca.chargeacid
WHERE w.staffServiceLineID in (Select teamID from @teams)
AND ((w.chargetypeid = 24 and W.Date >= @weekbegin and W.Date <= @sunday) or (w.chargetypeid = 25 and w.tsday >= @weekBegin and w.tsday <= @sunday) and isnull(w.approved,0)<>0
AND (case when w.chargetypeid=24 and w.chargeacid<>131 then isnull(W.Units,0) else 0 end <> 0
OR case when w.chargetypeid=25 and w.[ExpenseTypeID] = 2 then isnull(w.units,0) else 0 end <> 0
OR case when w.chargetypeid=25 and w.[ExpenseTypeID] = 1 then isnull(w.rate*w.units,0) else 0 end <> 0
OR case when w.chargetypeid=25 and w.[ExpenseTypeID] NOT IN (1,2) then isnull(w.rate*w.units,0) else 0 end <> 0))
order by Date, clientname, jobname, activity