Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Latest columnn value in SQL or PostgreSQL

Posted on 2014-09-19
2
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 49

Accepted Solution

by:
PortletPaul earned 2000 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

688 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