Solved

Latest columnn value in SQL or PostgreSQL

Posted on 2014-09-19
2
186 Views
Last Modified: 2014-09-26
I am trying to get the latest ...roles.name as role_name value.  My current query below returns one or many values of the roles.name for a team_member because they can have more than one role.  I am trying to just return the latest one.  I tried wrapping it in a MAX() function within the SELECT statement but no luck.

SELECT
    MIN(events.event_log.timestamp)                            AS min_login_timestamp,
    MAX(events.event_log.timestamp)                            AS max_login_timestamp,
    public.assets.first_name || ' ' || public.assets.last_name AS team_member,
    CASE
        WHEN public.project_markets.market IS NULL
        THEN 'Corporate'
        ELSE public.project_markets.market
    END,
    public.project_people_roles.name AS role_name
FROM
    events.event
INNER JOIN
    events.event_log
ON
    (
        events.event.id = events.event_log.event_id)
INNER JOIN
    public.assets_user_xref
ON
    (
        events.event_log.user_id = public.assets_user_xref.user_id)
INNER JOIN
    public.assets
ON
    (
        public.assets_user_xref.assets_id = public.assets.id)
FULL OUTER JOIN
    public.unitek_pcs
ON
    (
        public.assets.pc6 = public.unitek_pcs.pc6)
FULL OUTER JOIN
    public.project_pc_market_xref
ON
    (
        public.unitek_pcs.id = public.project_pc_market_xref.pc_id)
FULL OUTER JOIN
    public.project_markets
ON
    (
        public.project_pc_market_xref.market_id = public.project_markets.id)
INNER JOIN
    public.project_instance_people
ON
    (
        public.assets.id = public.project_instance_people.asset_id)
INNER JOIN
    public.project_people_roles
ON
    (
        public.project_instance_people.project_people_role_id = public.project_people_roles.id)
WHERE
    events.event_log.event_id IN (4) OR project_people_roles.role_id IN (44, 45, 46)

GROUP BY assets.first_name, assets.last_name, project_markets.market, project_people_roles.name;

Open in new window

0
Comment
Question by:szadroga
2 Comments
 
LVL 22

Expert Comment

by:earth man2
ID: 40333664
Window functions are your friend ....
http://www.postgresql.org/docs/9.2/static/tutorial-window.html

rank = 1 gives you the topmost value(s).
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40334029
rank() can return more than a single 1 per partition (when there are ties in the data).

use row_number() for this need (it will only ever give a single 1 per partition)

see: http://www.postgresql.org/docs/9.1/static/functions-window.html

+ info on OVER() http://www.postgresql.org/docs/9.1/static/tutorial-window.html

You don't state which timestamp or table this should apply to, but here is an EXAMPLE of how it might be used in your query:

INNER JOIN (
            SELECT
                  *
                , ROW_NUMBER() OVER (PARTITION BY event_id
                                     ORDER BY timestamp DESC) AS rn
            FROM events.event_log
      ) evlog  ON events.event.id = evlog.event_id
                        AND evlog.rn IN (1,2)                --<< HERE is where you limit the result

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Query stumper 3 36
Help with Sorting Full Text results 2 13
T-SQL: Nested CASE Statements 4 24
SQL Query Syntax Join 4 27
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now