sql - Temporary sequence within a SELECT -


i need join 2 tables part of sql query, , whoever set database's login table did not include column unique id each login. need join login table each login line 1 or more rows of other table, , want able uniquely identify each row login without having rely on user id , login timestamp being unique.

i'd this:

select l.*, pp.personpositionid (select login.*, next_unique_bigint() loginid login) l, personposition pp, personpositionstatus pps l.personid = pp.personid , [...] 

i use random(), i'd rather have temporary unique "id's" sequential (or predictable) rather random. i've done search i'm trying do, haven't found i'm looking for.

is there way in sql, postgresql? i'm guessing there , don't know keywords search for.

bonus points if have different way keep login records strait giving each row temporary id.

edit: overall purpose of count of logins given date range. each user has set of positions (roles? jobs?) hold 1 point in time another, , need count logins according position(s) user held @ time logged in. each user can have 1 or more positions @ given time. can have 0 positions if user account deactivated, naturally no logins recorded @ point in time when have 0 positions.

why don't add serial primary key column table?

alter table login add column login_id serial; alter table login add constraint login_pkey primary key(login_id); 

the first operation rewrite table , take lock time. run

vaccum full analyze login; 

inferior alternatives: row_number() pointed out @joachim. maximum performance can leave over clause empty:

row_number() on () rn 

aside: use as keyword column aliases (while noise table aliases).

or can use ctid poor man's surrogate primary key. faster:

details:
in-order sequence generation

example on dba.se:
numbering rows consecutively number of tables


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -