使用表中的新值更新数组

时间:2015-10-25 14:52:37

标签: postgresql plpgsql postgresql-9.4

我有一些物品的价格表:

|----------------------------|
| ItemID (INT) | Price (INT) |
|--------------|-------------|
|       1      |      50     |
|--------------|-------------|
|       2      |      36     |
|--------------|-------------|
|       3      |      11     |
|--------------|-------------|
|       4      |      22     |
|--------------|-------------|
|       5      |      54     |
|--------------|-------------|
|       6      |      38     |
|--------------|-------------|
|       7      |       2     |
|--------------|-------------|
|       8      |       1     |
|--------------|-------------|
|       9      |      39     |
|----------------------------|

此表包含大多数实际价格,每小时更新一次(例如)。

我喜欢"推车"在购物车创建时实际购买的商品和价格:

|-------------------------------------------------------|
| CartID (INT) | Items (INTEGER[]) | Prices (INTEGER[]) |
|--------------|-------------------|--------------------|
|       1      |      {1,2,3}      |     {50,25,310}    |
|--------------|-------------------|--------------------|
|       2      |      {4,5,6}      |    {1337,20,32}    |
|--------------|-------------------|--------------------|
|       3      |     {1,9,6,7}     |   {258,356,711,2}  |
|-------------------------------------------------------|

在某些情况下,我必须将此购物车中的价格更新为实际价格。

我的功能(未完成):

CREATE OR REPLACE FUNCTION public.prices_update(startindex integer)
 RETURNS void
 LANGUAGE plpgsql
 SECURITY DEFINER
AS $function$
DECLARE
    cart RECORD;
    prices INTEGER[];
    t RECORD;
BEGIN
    FOR bet IN
        SELECT * FROM Carts WHERE CartID > startindex
    LOOP
        prices := ARRAY[]::INTEGER[];
        FOR t IN
            SELECT ItemID, Price FROM Prices WHERE ItemID = ANY(cart.Items)
        LOOP
            prices := prices || t.Price;
        END LOOP;
        RAISE NOTICE '%', prices;
    END LOOP;
END;
$function$

所以,我坚持新的价格阵列创建。价格的位置应与数组中的项目的位置相关联。怎么做?或者可能有更聪明的解决方案来节省价格?

1 个答案:

答案 0 :(得分:1)

此查询选择resultObj(1). AnlaysisType = 'multivariate' GroupSolution = false SignalType = 'distributed' Processing = 'raw' alpha = 10 crossvalidation = 1 dprime = 6.5811 bestLambda = [] bestBetas = [] scores = [] fitObj = [] ,其价格来自表格carts

items

使用上述查询更新select cartid, array_agg(c.itemid) items, array_agg(i.price) prices from ( select cartid, unnest(items) itemid from carts where cartid > 0 -- startindex ) c join items i using(itemid) group by 1 order by 1; cartid | items | prices --------+-----------+-------------- 1 | {1,2,3} | {50,36,11} 2 | {4,5,6} | {22,54,38} 3 | {1,6,7,9} | {50,38,2,39} (3 rows)

carts

如果必须保留数组元素的顺序,则查询必须更复杂一些。在聚合元素的阶段,使用row_number()的其他顺序。

update carts c
set items = n.items, prices = n.prices
from (
    select cartid, array_agg(c.itemid) items, array_agg(i.price) prices
    from (
        select cartid, unnest(items) itemid
        from carts
        where cartid > 0        -- startindex
        ) c
        join items i using(itemid)
    group by 1
    order by 1
    ) n
where n.cartid = c.cartid;
相关问题