Hi @BHVS ,
Welcome to Microsoft Q&A!
After checking, your insert statement was not the same as the source data and target data provided in the snapshots. The value of [PROD_UNTS] was 0 in two rows.
I updated them as below:
INSERT [dbo].[EMPTIME] ([CDATE], [PRDSTLE], [EMP_ADJ_SIGN_ON_TIME], [PROD_UNTS]) VALUES (CAST(N'2021-07-11 09:30:10.090' AS DateTime), N'SANTOOR', CAST(9.00 AS Decimal(4, 2)), 1)
GO
INSERT [dbo].[EMPTIME] ([CDATE], [PRDSTLE], [EMP_ADJ_SIGN_ON_TIME], [PROD_UNTS]) VALUES (CAST(N'2021-07-11 12:43:10.090' AS DateTime), N'CINTHOL ', CAST(12.43 AS Decimal(4, 2)), 22)
GO
Please also refer below query using sum(case when...) which is alternative to pivot.
select PRDSTLE,
isnull(sum(case when h = 7 then PROD_UNTS end),0) as [7 A:M to 8 A:M],
isnull(sum(case when h = 8 then PROD_UNTS end),0) as [8 A:M to 9 A:M],
isnull(sum(case when h = 9 then PROD_UNTS end),0) as [9 A:M to 10 A:M],
isnull(sum(case when h = 10 then PROD_UNTS end),0) as [10 A:M to 11 A:M],
isnull(sum(case when h = 11 then PROD_UNTS end),0) as [11 A:M to 12 P:M],
isnull(sum(case when h = 12 then PROD_UNTS end),0) as [Remaining time]
from
(select PRDSTLE, PROD_UNTS, IIF(EMP_ADJ_SIGN_ON_TIME>=12,12,cast(EMP_ADJ_SIGN_ON_TIME as int)) h from EMPTIME) a
group by PRDSTLE
Output:
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.