psql如何从复合数组中选择

时间:2015-08-21 00:17:03

标签: sql postgresql select psql compositetype

CREATE TYPE complex AS (
start_time timestamp,
amount int,
);

给定一个变量

my_complexes complex[]

你怎么像桌子一样查询?

SELECT amount FROM my_complexes;

Gives"关系不存在"。

SELECT mc.amount FROM (SELECT my_complexes) mc;

给予"金额不存在"。

如果它是相关的,my_complexes作为函数的参数传入。我想再选择那个阵列。

2 个答案:

答案 0 :(得分:1)

您应该使用:

# Catsylvanian money is a strange thing: they have a coin for every
# denomination (including zero!). A wonky change machine in
# Catsylvania takes any coin of value N and returns 3 new coins,
# valued at N/2, N/3 and N/4 (rounding down).
#
# Write a method `wonky_coins(n)` that returns the number of coins you
# are left with if you take all non-zero coins and keep feeding them
# back into the machine until you are left with only zero-value coins.
#
# Difficulty: 3/5

describe "#wonky_coins" do
  it "handles a coin of value 1" do
    wonky_coins(1).should == 3
  end

  it "handles a coin of value 5" do
    wonky_coins(5).should == 11
    # 11
    # => [2, 1, 1]
    # => [[1, 0, 0], [0, 0, 0], [0, 0, 0]]
    # => [[[0, 0, 0], 0, 0], [0, 0, 0], [0, 0, 0]]
  end

  it "handles a coin of value 6" do
    wonky_coins(6).should == 15
  end

  it "handles being given the zero coin" do
    wonky_coins(0).should == 1
  end
end

完整示例:

SELECT (your_column_from_table_ my_complexes).amount FROM my_complexes;

答案 1 :(得分:0)

我失踪的似乎是UNNEST(my_complexes)

SELECT mc.amount FROM UNNEST(my_complexes) mc;