Hi,
I want to show the date as default value in query result when no results returned from the database.
Tried my best to get the result but when there is no data on particular date the row not appeared with default date.
Please go through the below query and expected result attachment and do the need full.
SELECT Convert(Date, GetDate()) AS JobDate, PLH.ShiftId, PLM.RunningMachine, PLF.FirstCastingMachine, PLH.TotalOperator, PLH.TotalTrainee, PLA.MedicalLeave, PLA.OffDayLeave, PLA.MisleadLeave, PLA.AnnualLeave, PLA.OthersLeave
FROM ProductionLogHeader AS PLH
LEFT OUTER JOIN ProductionLogAbsentDetails AS PLA ON PLH.Uniid = PLA.LogId
LEFT JOIN (SELECT LogId, COUNT(MachineId) AS RunningMachine FROM ProductionLogRunningMachine GROUP BY LogId) AS PLM ON PLH.Uniid = PLM.LogId
LEFT JOIN (Select LogId, Count(MachineId) As FirstCastingMachine FROM ProductionLogFirstCastingMachine GROUP BY LogId) AS PLF ON PLH.Uniid = PLF.LogId
WHERE JobDate = Convert(Date,GETDATE()) AND ShiftId = 2
UNION ALL
SELECT Convert(Date, DATEADD(DD, DATEDIFF(DY, 0, GETDATE()), -1)) AS JobDate, PLH.ShiftId, PLM.RunningMachine, PLF.FirstCastingMachine, PLH.TotalOperator, PLH.TotalTrainee, PLA.MedicalLeave, PLA.OffDayLeave, PLA.MisleadLeave, PLA.AnnualLeave, PLA.OthersLeave
FROM ProductionLogHeader AS PLH
LEFT OUTER JOIN ProductionLogAbsentDetails AS PLA ON PLH.Uniid = PLA.LogId
LEFT JOIN (SELECT LogId, COUNT(MachineId) AS RunningMachine FROM ProductionLogRunningMachine GROUP BY LogId) AS PLM ON PLH.Uniid = PLM.LogId
LEFT JOIN (Select LogId, Count(MachineId) As FirstCastingMachine FROM ProductionLogFirstCastingMachine GROUP BY LogId) AS PLF ON PLH.Uniid = PLF.LogId
WHERE JobDate = DATEADD(DD, DATEDIFF(DY, 0, GETDATE()), -1) AND ShiftId = 3