代码之家  ›  专栏  ›  技术社区  ›  lock

为什么SQL中没有产品聚合函数?

  •  46
  • lock  · 技术社区  · 14 年前

    我在找类似的东西 SELECT PRODUCT(table.price) FROM table GROUP BY table.sale 类似于 SUM 作品。

    我是否遗漏了文件上的内容,或者真的没有 PRODUCT 功能?

    如果是这样,为什么不呢?

    注意:我在postgres、mysql和mssql中查找了函数,但没有找到,所以我假设所有的sql都不支持它。

    9 回复  |  直到 14 年前
        1
  •  26
  •   onedaywhen    14 年前

    PRODUCT CONCATENATE

    log exp SUM

    Itzik Ben-Gan's blog

        2
  •  49
  •   Community CDub    8 年前

    SELECT
        GrpID,
        CASE
           WHEN MinVal = 0 THEN 0
           WHEN Neg % 2 = 1 THEN -1 * EXP(ABSMult)
           ELSE EXP(ABSMult)
        END
    FROM
        (
        SELECT
           GrpID, 
           --log of +ve row values
           SUM(LOG(ABS(NULLIF(Value, 0)))) AS ABSMult,
           --count of -ve values. Even = +ve result.
           SUM(SIGN(CASE WHEN Value < 0 THEN 1 ELSE 0 END)) AS Neg,
           --anything * zero = zero
           MIN(ABS(Value)) AS MinVal
        FROM
           Mytable
        GROUP BY
           GrpID
        ) foo
    

    SQL Server Query - groupwise multiplication

        3
  •  20
  •   Lord Peter    14 年前

    select exp (sum (ln (table.price))) from table ...
    
        4
  •  8
  •   Fenton    14 年前

    SELECT
        Exp(Sum(IIf(Abs([Num])=0,0,Log(Abs([Num])))))*IIf(Min(Abs([Num]))=0,0,1)*(1-2*(Sum(IIf([Num]>=0,0,1)) Mod 2)) AS P
    FROM
       Table1
    

    http://productfunctionsql.codeplex.com/

        5
  •  6
  •   David Airapetyan    12 年前

    declare @Floats as table (value float)
    insert into @Floats values (0.9)
    insert into @Floats values (0.9)
    insert into @Floats values (0.9)
    
    declare @multiplier float = null
    
    select 
        @multiplier = isnull(@multiplier, '1') * value
    from @Floats
    
    select @multiplier
    

        6
  •  3
  •   naiem    13 年前

        7
  •  1
  •   Nicolás Sierra    13 年前

    SELECT group_concat(table.price) FROM table GROUP BY table.sale

    php.net manual

        8
  •  0
  •   TomáÅ¡ Záluský    6 年前

    with recursive t(c) as (
      select unnest(array[2,5,7,8])
    ), p(a) as (
      select array_agg(c) from t
      union all
      select p.a[2:]
      from p
      cross join generate_series(1, p.a[1])
    )
    select count(*) from p where cardinality(a) = 0;
    
        9
  •  0
  •   TomáÅ¡ Záluský    6 年前

       with recursive t(c) as (
         select unnest(array[2,5,7,8])
       ), r(c,n) as (
         select t.c, row_number() over () from t
       ), p(c,n) as (
         select c, n from r where n = 1
         union all
         select r.c * p.c, r.n from p join r on p.n + 1 = r.n
       )
       select c from p where n = (select max(n) from p);
    

       with recursive t(sale,price) as (
         select 'multiplication', 2 union
         select 'multiplication', 5 union
         select 'multiplication', 7 union
         select 'multiplication', 8 union
         select 'trivial', 1 union
         select 'trivial', 8 union
         select 'negatives work', -2 union
         select 'negatives work', -3 union
         select 'negatives work', -5 union
         select 'look ma, zero works too!', 1 union
         select 'look ma, zero works too!', 0 union
         select 'look ma, zero works too!', 2
       ), r(sale,price,n,maxn) as (
         select t.sale, t.price, row_number() over (partition by sale), count(1) over (partition by sale)
         from t
       ), p(sale,price,n,maxn) as (
         select sale, price, n, maxn
         from r where n = 1
         union all
         select p.sale, r.price * p.price, r.n, r.maxn
         from p
         join r on p.sale = r.sale and p.n + 1 = r.n
       )
       select sale, price
       from p
       where n = maxn
       order by sale;
    

    sale,price
    "look ma, zero works too!",0
    multiplication,560
    negatives work,-30
    trivial,8