3

这是我的表格“AuctionDetails”

拍卖

以下选择:

select string_agg("AuctionNO",',' ) as  "AuctionNO"
      ,sum("QuntityInAuction" ) as "QuntityInAuction"
      ,"AmmanatPattiID"
      ,"EntryPassDetailsId" 
      ,"BrokerID"
      ,"TraderID"
      ,"IsSold"
      ,"IsActive"
      ,"IsExit"
      ,"IsNew"
      ,"CreationDate"
from "AuctionDetails"
group by "AmmanatPattiID"
        ,"EntryPassDetailsId"  
        ,"TraderID"
        ,"IsSold"
        ,"IsActive"
        ,"IsExit"
        ,"IsNew"
        ,"BrokerID"
        ,"CreationDate"

给了我这个结果:

这是我的结果

但我需要像这样的记录

AuctionNo                                QunatityInAuction  AmmanatpattiID  EntryPassDetailID  BrokerID  Trader ID  IsSold  ISActive  ISExit  IsNew  CreationDate
AU8797897,AU8797886,AU596220196F37379    1050               -1               228,229            42         42         f         t       f      t      2013-10-10

最后,我需要一个最新的交易者和经纪人条目,在我们的例子中是“42”,数量总和,以及拍卖编号的串联......

4

2 回答 2

2

Postgres wiki 描述了如何定义自己的 FIRST 和 LAST 聚合函数。例如:

-- Create a function that always returns the last non-NULL item
CREATE OR REPLACE FUNCTION public.last_agg ( anyelement, anyelement )
RETURNS anyelement LANGUAGE SQL IMMUTABLE STRICT AS $$
        SELECT $2;
$$;

-- And then wrap an aggregate around it
CREATE AGGREGATE public.LAST (
        sfunc    = public.last_agg,
        basetype = anyelement,
        stype    = anyelement
);

该页面在这里: https://wiki.postgresql.org/wiki/First/last_(aggregate)

于 2017-12-17T00:55:17.280 回答
0

有多种方法可以做到这一点。聚合函数和窗口函数的组合或窗口函数和DISTINCT...的组合

SELECT a.*, b.*
FROM  (
   SELECT string_agg("AuctionNO", ',') AS  "AuctionNO"
         ,sum("QuntityInAuction") AS "QuntityInAuction"
   FROM   "AuctionDetails"
   ) a
CROSS JOIN (
   SELECT "AmmanatPattiID"
         ,"EntryPassDetailsId" 
         ,"BrokerID"
         ,"TraderID"
         ,"IsSold"
         ,"IsActive"
         ,"IsExit"
         ,"IsNew"
         ,"CreationDate"
   FROM   "AuctionDetails"
   ORDER  BY "AuctionID" DESC
   LIMIT  1
   ) b

对于整个表的单个结果行的简单情况,这可能是最简单的。

于 2013-10-10T07:15:11.033 回答