Your solution looks nice :)
Extending question - at this time I can't make tests, so i have question - what do you think: If there will be SRC0-SRC9 tables (all tables with 12 parts) which option will be better?
first:
select ID,
max(case when SRC='0' and PART='01' then DATA end) as SRC0_DATA0
max(case when SRC='0' and PART='02' then DATA end) as SRC0_DATA1
max(case when SRC='0' and PART='03' then DATA end) as SRC0_DATA2
max(case when SRC='1' and PART='01' then DATA end) as SRC1_DATA0
max(case when SRC='1' and PART='02' then DATA end) as SRC1_DATA1
max(case when SRC='1' and PART='03' then DATA end) as SRC1_DATA2
max(case when SRC='2' and PART='01' then DATA end) as SRC1_DATA0
max(case when SRC='2' and PART='02' then DATA end) as SRC1_DATA1
max(case when SRC='2' and PART='03' then DATA end) as SRC1_DATA2
from (
select '0' as SRC, * from SRC0
union all
select '1' as SRC, * from SRC1
union all
select '2' as SRC, * from SRC2
)
group by 1
second:
select ID,
max(case when SRC='0' then DATA0 end) as SRC0_DATA0
max(case when SRC='0' then DATA1 end) as SRC0_DATA1
max(case when SRC='0' then DATA2 end) as SRC0_DATA2
max(case when SRC='1' then DATA0 end) as SRC1_DATA0
max(case when SRC='1' then DATA1 end) as SRC1_DATA1
max(case when SRC='1' then DATA2 end) as SRC1_DATA2
max(case when SRC='1' then DATA0 end) as SRC2_DATA0
max(case when SRC='1' then DATA1 end) as SRC2_DATA1
max(case when SRC='1' then DATA2 end) as SRC2_DATA2
from (
select '0' as SRC, ID,
max(case when PART='01' then DATA end) as DATA0
max(case when PART='02' then DATA end) as DATA1
max(case when PART='03' then DATA end) as DATA2
from SRC0
group by 1,2
union all
select '1' as SRC, ID,
max(case when PART='01' then DATA end) as DATA0
max(case when PART='02' then DATA end) as DATA1
max(case when PART='03' then DATA end) as DATA2
from SRC1
group by 1,2
union all
select '2' as SRC, ID,
max(case when PART='01' then DATA end) as DATA0
max(case when PART='02' then DATA end) as DATA1
max(case when PART='03' then DATA end) as DATA2
from SRC2
group by 1,2
)
group by 1
third option is to left join aggregations with CASE&GROUP.
--
View mentioned at beginning:
Create view WIDE2 as
Select
ID,
max(case when PART='01' then Data end) as DATA0,
max(case when PART='02' then Data end) as DATA1,
max(case when PART='03' then Data end) as DATA2,
max(case when PART='04' then Data end) as DATA3,
max(case when PART='05' then Data end) as DATA4,
max(case when PART='06' then Data end) as DATA5,
max(case when PART='07' then Data end) as DATA6,
max(case when PART='08' then Data end) as DATA7,
max(case when PART='09' then Data end) as DATA8,
max(case when PART='10' then Data end) as DATA9,
max(case when PART='11' then Data end) as DATA10,
max(case when PART='12' then Data end) as DATA11
from SRC
group by ID;
explain select * from wide2;
*** Help information returned. 18 rows.
*** Total elapsed time was 1 second.
Explanation
---------------------------------------------------------------------------
1) First, we lock a distinct SAS."pseudo table" for read on a RowHash to prevent global deadlock for SAS.a0.
2) Next, we lock SAS.a0 in view wide2 for read.
3) We do an all-AMPs SUM step to aggregate from SAS.a0 in view wide2 by way of an all-rows scan with no residual conditions , grouping by field1 ( SAS.SRC.ID). Aggregate Intermediate Results are computed globally, then placed in Spool 4. The size of Spool 4 is estimated with high confidence to be 2,000 rows (234,000 bytes). The estimated time for this step is 0.17 seconds.
4) We do an all-AMPs RETRIEVE step from Spool 4 (Last Use) by way of an all-rows scan into Spool 2 (group_amps), which is built locally on the AMPs. The size of Spool 2 is estimated with high confidence to be 2,000 rows (582,000 bytes). The estimated time for this step is 0.04 seconds.
5) Finally, we send out an END TRANSACTION step to all AMPs involved in processing the request.
-> The contents of Spool 2 are sent back to the user as the result of statement 1. The total estimated time is 0.22 seconds.
↧