shweta
shweta

Reputation: 11

Stuff function returns null values

When i am trying the stuff function in this query:

select Project.* from TimePayrollTimesheet3 tp inner join
(SELECT t1.parentrecordID,
    abc = 
    STUFF (
        (SELECT   
                ',' +
                caprojectnumber
        FROM TimePayrollTimesheet3_jobtimes t
        where t1.parentrecordID = t.parentrecordID
        FOR XML PATH('')
        ), 1, 1, ''
    )
FROM TimePayrollTimesheet3_jobtimes t1  GROUP by t1.parentrecordID) Project
on tp.RecordId = project.ParentRecordId

It is working fine. relationship between TimePayrollTimesheet3 and TimePayrollTimesheet3_jobtimes is a parent child relationship. TimePayrollTimesheet3_jobtimes 's field PArentRecordID = TimePayrollTimesheet3 's field RecordID.

As soon as we make the query complex, the above query fails:

select t.DisplayName as 'Employee Name', t.ApproverName as 'Manager Name', r.BusinessUnitLookup as 'Employed Busines Unit Number' , b1.BusinessUnitName as 'Employed Business Unit Name', t.BusinessUnit as 'Job Business Unit Number',b.BusinessUnitName as 'Job Business Unit Name',
FORMAT (t.StartDate, 'dd/MM/yyyy') as StartDate,FORMAT (t.EndDateForLookups, 'dd/MM/yyyy ') as EndDate, cast(t.TotalTimeClaimed as numeric(10,2)) as 'Normal Hours', 
cast((t.TotalTimeClaimed - (t.sumleavehours + t.sumjobtimes)) as numeric(10,2)) as 'Uncharged Hours', cast(t.sumleavehours as numeric(10,2)) as 'Leave Hours', t.CurrentStateName as 'State'
, Project.abc
from TimePayrollTimesheet3 t
left join
(SELECT t1.parentrecordID,
    abc = 
    STUFF (
        (SELECT   
                ',' +
                caprojectnumber
        FROM TimePayrollTimesheet3_jobtimes t
        where t1.parentrecordID = t.parentrecordID
        FOR XML PATH('')
        ), 1, 1, ''
    )
FROM TimePayrollTimesheet3_jobtimes t1  GROUP by t1.parentrecordID) Project
on t.RecordId = project.ParentRecordId
inner join Resources2 r on t.EmployeeNumber = r.EmployeeNumber
inner join BusinessUnit b on b.BusinessUnitNumber = t.BusinessUnit
inner join BusinessUnit b1 on b1.BusinessUnitNumber = r.BusinessUnitLookup
where
t.TimesheetType = 'On-Leave Multiple Days'
and
t.CurrentStateName = 'Approved'
order by t.StartDate desc

Can someone please help?

Upvotes: 0

Views: 128

Answers (0)

Related Questions