I have this SQL:
[tt]
SELECT * FROM
(SELECT PS5YR_PN_PIN_FK, PS5YR_PN_WORK_TYPE,
PS5YR_PN_FY, PS5YR_PN_PROGRAM_AMT AS AMT
FROM S4111000.PSFIVEYR_PROJ_NO
WHERE (PS5YR_PN_FY BETWEEN 2018 AND 2022)
)
PIVOT
(
SUM(AMT)
FOR PS5YR_PN_FY IN (2018, 2019, 2020, 2021, 2022)
)
[/tt]
and the outcome of it looks like this:
[pre]
PS5YR_PN_PIN_FK PS5YR_PN_WORK_TYPE 2018 2019 2020 2021 2022
60 1536 600 600 600 600 600
63 1001 13814
71 1001 5100
78 1536 200 200 200 200 200
82 1509 930
89 2011 30419 1412 1928 16607
89 4551 523
92 5021 76
109 2521 450
123 2513 500 17000
[/pre]
All empty spaces in columns 2018 to 2022 are NULLs
What do I need to do to my SQL to have the outcome like this:
Replace NULLs with 0 (zeros)
[pre]
PS5YR_PN_PIN_FK PS5YR_PN_WORK_TYPE 2018 2019 2020 2021 2022
60 1536 600 600 600 600 600
63 1001 0 13814 0 0 0
71 1001 0 5100 0 0 0
78 1536 200 200 200 200 200
82 1509 0 930 0 0 0
89 2011 0 30419 1412 1928 16607
89 4551 523 0 0 0 0
92 5021 0 76 0 0 0
109 2521 0 0 450 0 0
123 2513 500 0 0 17000 0
[/pre]
The Select statement is a part of an Insert statement. Insert statement adds records to a table where all fields need to have values, so NULLs are not allowed.
Have fun.
---- Andy
There is a great need for a sarcasm font.
[tt]
SELECT * FROM
(SELECT PS5YR_PN_PIN_FK, PS5YR_PN_WORK_TYPE,
PS5YR_PN_FY, PS5YR_PN_PROGRAM_AMT AS AMT
FROM S4111000.PSFIVEYR_PROJ_NO
WHERE (PS5YR_PN_FY BETWEEN 2018 AND 2022)
)
PIVOT
(
SUM(AMT)
FOR PS5YR_PN_FY IN (2018, 2019, 2020, 2021, 2022)
)
[/tt]
and the outcome of it looks like this:
[pre]
PS5YR_PN_PIN_FK PS5YR_PN_WORK_TYPE 2018 2019 2020 2021 2022
60 1536 600 600 600 600 600
63 1001 13814
71 1001 5100
78 1536 200 200 200 200 200
82 1509 930
89 2011 30419 1412 1928 16607
89 4551 523
92 5021 76
109 2521 450
123 2513 500 17000
[/pre]
All empty spaces in columns 2018 to 2022 are NULLs
What do I need to do to my SQL to have the outcome like this:
Replace NULLs with 0 (zeros)
[pre]
PS5YR_PN_PIN_FK PS5YR_PN_WORK_TYPE 2018 2019 2020 2021 2022
60 1536 600 600 600 600 600
63 1001 0 13814 0 0 0
71 1001 0 5100 0 0 0
78 1536 200 200 200 200 200
82 1509 0 930 0 0 0
89 2011 0 30419 1412 1928 16607
89 4551 523 0 0 0 0
92 5021 0 76 0 0 0
109 2521 0 0 450 0 0
123 2513 500 0 0 17000 0
[/pre]
The Select statement is a part of an Insert statement. Insert statement adds records to a table where all fields need to have values, so NULLs are not allowed.
Have fun.
---- Andy
There is a great need for a sarcasm font.