如何比较postgresql中的两行?

时间:2015-08-18 09:08:17

标签: sql postgresql

我有一张表tab,其中包含:

item identifier quantity methodid
10      1         20         2
10      1         30         3
11      1         10         3
11      1         12.5       3
11      2         20         5
12      1         20         1
12      1         30         1

我需要编写一个函数来检查methodiditem是否存在重复identifier的情况。 在上面的示例中item 11 identifier 1有两行methodid 3表示它是重复的,item 12 idfentifier 1也有重复的行。< / p>

我不需要对数据做任何事情来识别这种情况。

我不需要找到重复的位置和内容...只是说有重复。

我唯一的信息是identifier

  CREATE OR REPLACE FUNCTION func(identifier integer)
      RETURNS integer AS
    $BODY$
    declare
    errorcode int;
    begin
        if _____________   then
            errorcode =1;
            raise exception 'there id duplication in this identifier';  
        END IF;

        continue work

        return 0;           
        exception
            when raise_exception then
    return errorcode;
    end;                
    $BODY$
    LANGUAGE plpgsql VOLATILE

在空白处我想提出一个检查重复的查询。

如何编写执行检查的查询? 功能结构可以改变。但我需要知道何时提出异常。

4 个答案:

答案 0 :(得分:1)

使用 def pathParams: Directive1[List[String]] = { val prv = provide(List.empty[String]) def somePathParams(ctxPathLens: Lens[RequestContext, Path]) = extract(ctx => (Slash ~ Segments).apply(ctxPathLens.get(ctx))).flatMap { case Matched(_, Tuple1(path)) => path.takeRight(1) match { case last :: Nil => last.split(';').toList match { case lastHead :: lastTail => provide(lastTail) & mapRequestContext( ctxPathLens.set(_, Path((path.dropRight(1) :+ lastHead).mkString("/", "/", "")))) case _ => prv } case _ => prv } case _ => prv } val unmatchedPath = somePathParams(Lens.lensu((ctx, path) => ctx.mapUnmatchedPath(_ => path), _.unmatchedPath)) val requestPath = somePathParams(Lens.lensu((ctx, path) => ctx.mapRequest(r => r.withUri(r.uri.withPath(path))) , _.request.uri.path)) unmatchedPath.tflatMap(_ => Directive.Empty) & requestPath } def pathParamsMap: Directive1[Map[String, String]] = pathParams.map(_.map(_.split('=').toList match { case key :: Nil => key -> "" case key :: values => key -> values.mkString("=") case _ => ??? }).toMap) def optionalPathParam(name: String): Directive1[Option[String]] = pathParamsMap.map(_.get(name)) def optionalPathParamSessionId:Directive1[Option[UUID]] = optionalPathParam(jsessionidKey).map(_.flatMap(j => Try(UUID.fromString(j)).toOption))

group by

其中select item, identifier, methodid, count(*) from tab group by item, identifier, methodid having count(*) > 1 仅用于返回重复的行。

答案 1 :(得分:1)

要检查是否根据所选列复制了任何数据集,您可以按这些列进行分组并计算出现次数。

所以在你的情况下你可以这样做:

SELECT 1 FROM tab GROUP BY item, identifier, methodid HAVING COUNT(*) > 1;

要将此功能合并到您的功能中,您只需检查它是否存在:

if EXISTS (SELECT 1 ...) then

答案 2 :(得分:0)

尝试以下操作可能会得到结果集。

首先为我们拥有的表生成一个行号。

为此,以下是查询。

select *,ROW_NUMBER() over (partition by item,identifier,methodid order by item) as RowID

from tab;

然后你会得到如下结果。

Item   Identifier  quantity  methodid  RowID

10         1        20          2       1

10         1        30          3       1    

11         1        10          3       1    

11         1        12.5        3       2    

11         2         20         5       1    

12         1         20         1       1    

12         1         30         1       2    

12         1         40         2       1

因此,从此结果集中,您可以尝试使用以下查询,然后您将获得结果

select * from (
select *,ROW_NUMBER() over (partition by item,identifier,methodid order by item) as rowid
from tab) as p
where p.rowid = 1

感谢。

答案 3 :(得分:0)

line = ''.join(line.split())
TypeError: sequence item 0: expected str instance, bytes found

值大于1的每个排名行都是重复的行