Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8535263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:23:30+00:00 2026-06-11T10:23:30+00:00

(PostgreSQL 8.4) Table trackingMessages stores tracking events between mobile devices (tm_nl_mobileid) and fixed devices

  • 0

(PostgreSQL 8.4)
Table “trackingMessages” stores tracking events between mobile devices (tm_nl_mobileid) and fixed devices (tm_nl_fixedId).

CREATE TABLE trackingMessages
(
  tm_id SERIAL PRIMARY KEY,           -- PK
  tm_nl_mobileId INTEGER,             -- FK to mobile
  tm_nl_fixedId INTEGER,              -- FK to fixed
  tm_date INTEGER,                    -- Network time
  tm_messageType INTEGER,             -- 0=disconnect, 1=connect
  CONSTRAINT tm_unique_row
    UNIQUE (tm_nl_mobileId, tm_nl_fixedId, tm_date, tm_messageType)
);

Problem here is that it’s possible that the same mobile will connect to the same fixed twice (or more times) subsequently. I don’t want to see the subsequent ones, but it’s OK to see a mobile connected to a same fixed at a later date, provided there was a connection to a different fixed in between.

I think I’m close but not quite. I’ve been using the following CTE (found here on Stack Overflow)

WITH cte AS 
(
  SELECT tm_nl_fixedid, tm_date, Row_number() OVER (
    partition BY tm_nl_fixedid
    ORDER BY tm_date ASC
  ) RN 
  FROM   trackingMessages
) 
SELECT * FROM cte 
  WHERE tm_nl_mobileid = 150 AND tm_messagetype = 1
  ORDER BY tm_date;

Gives me the following results

32;1316538756;1
21;1316539069;1
32;1316539194;2
32;1316539221;3
21;1316539235;2

The problem here is that the last column should be 1, 1, 1, 2, 1, because that third “32” is in fact a duplicate tracking event (twice in a row at the same fixed) and that last connection to “21” is OK because “32” was in between.

Please don’t suggest a cursor, this is what I am currently trying to move away from. The cursor solution does work, but it’s too slow given the amount of records I have to deal with. I’d much rather fix the CTE and only select where RN = 1 … unless you have a better idea!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:23:31+00:00Added an answer on June 11, 2026 at 10:23 am

    Well, you’re not that close because row_number() cannot track sequences by two groups at the same time. PARTITION BY tm_nl_fixedid ORDER BY date RESTART ON GAP does not exist, there’s no such thing.

    Itzik Ben-Gan has a solution for the islands and gaps problem you are facing (several solutions, actually). The idea is to order rows by the main criteria (date) and then by partitioning criteria + main criteria. Difference between ordinals will remain the same as they belong to the same partitioning criteria and date series.

    with cte as
    (
      select *,
          -- While order by date and order by something-else, date
          -- run along, they belong to the same sequence
             row_number() over (order by tm_date)
           - row_number() over (order by tm_nl_fixedid, tm_date) grp
        from trackingMessages
    )
    select *,
        -- Now we can get ordinal number grouped by each sequence
           row_number() over (partition by tm_nl_fixedid, grp
                              order by tm_date) rn
      from cte
     order by tm_date
    

    Here is Sql Fiddle with example.

    And here is chapter 5 of Sql Server MVP Deep Dives with several solutions to islands and gaps problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Table in PostgreSQL database: CREATE TABLE pmss_recordmodel ( id serial NOT NULL, Name character
Given a Postgresql table schema: create table thing ( id serial primary key, key
I searched type convertion table between PostgreSQL and C#, but I couldn't find anything.
I have a postgresql table containing movements of different items (models) between warehouses. For
I need to create a means of viewing and editing a postgresql table through
I'm trying to insert a row into a PostgreSQL table with a serial primary
I have a PostgreSQL table with the following schema - CREATE TABLE test (
I am referring to http://www.if-not-true-then-false.com/2009/11/howto-create-postgresql-table-partitioning-part-1/ To reproduce the problem, here is some simple steps
I have a table in PostgreSQL where the schema looks like this: CREATE TABLE
When I create a table in PostgreSQL, the SQL I use looks like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.