I need to join two tables as part of a sql query, and whoever set up the database's login
table did not include a column with a unique id for each login. I need to join login
with a table where each login will line up with one or more rows of the other table, and I want to be able to uniquely identify each row from login
without having to rely on the user id and the login timestamp being unique.
I'd like to do something like this:
SELECT l.*, pp.personpositionid
FROM (SELECT login.*, next_unique_bigint() AS loginid FROM login) l, personposition pp, personpositionstatus pps
WHERE l.personid = pp.personid
AND [...]
I could use random(), but I'd rather have the temporary unique "id's" be sequential (or even just predictable) rather than random. I've done a search for what I'm trying to do, but haven't found what I'm looking for.
Is there a way to do this in SQL, especially PostgreSQL? I'm guessing there is and I just don't know what keywords to search for.
Bonus points if you have a different way to keep the login records strait than giving each row a temporary id.
EDIT: The overall purpose of this is to get a count of logins for a given date range. Each user has a set of Positions (roles? jobs?) that they hold from one point in time to another, and I need to count logins according to what position(s) the user held at the time they logged in. Each user can have 1 or more positions at any given time. They can only have 0 positions if their user account is deactivated, so naturally no logins will be recorded at a point in time when they have 0 positions.