PostgreSQL array error -
i new @ postgresql , create 2 table
create table hour( id integer, hour timestamp ) ; create table activity_time( id integer, day_hours varchar[][] ) ; day_hours column array of combinations number of week, , id of hour table .value of column {{1,5},{2,5},{2,6}}. tried creat trigger, when delete hour hours table , in activity_time have remove in day_hours hour exist . example, if delete id=6 hour result of day_hours {{1,5},{2,5}} , or if day_hours {{3,2},{3,4},{4,2}} , if delete id=2 hour result of day_hours {{3,4}}. know how write trigger, can't write procedure execute array
with summary ( select "unnest" ( "day_hours" [ 1 : array_length("day_hours",1)][ 1 : 1 ]:: int [] ) days, "unnest" ( "day_hours" [ 1 : array_length("day_hours",1)][ 2 : 2 ]:: int [] ) hours "activity_time" "id" = 3 ) select array(select array[ s.days,s.hours] summary s s.hours!= 2 ) this give error :could not find array type data type integer[] error,
the problem function array() (i mean function array() , not constructor array[]) not accept array argument. query:
select array(select array[1,2]); give error:
error: not find array type data type integer[] the function array() accept instead set argument. use function unnest convert array in set:
select array(select unnest(array[1,2])); -- result: -- array -- ------- -- {1,2} however unnest elements of bidimensional array in 1 dimensional array. workaroud can first cast arrays text, build array of texts, cast again in text, remove quotes , cast text result in bidimensional array: query so:
with summary ( select unnest ( day_hours [ 1 : array_length(day_hours,1)][ 1 : 1 ]:: int [] ) days, unnest ( day_hours [ 1 : array_length(day_hours,1)][ 2 : 2 ]:: int [] ) hours activity_time id = 3 ) select replace(array(select array[ s.days,s.hours]::text -- <<< change line summary s s.hours!= 2 )::text, '"','')::int[][] array; -- <<< change line this returns:
array --------------------- {{1,5},{2,5},{2,6}} (1 row)
Comments
Post a Comment