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 9236545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:19:06+00:00 2026-06-18T07:19:06+00:00

In my previous question ( Select a distinct RowId based on series of trainnumbers

  • 0

In my previous question (Select a distinct RowId based on series of trainnumbers) I got a nice list of RowIds.

Now I’d like to make that list complete and totally awesome! 🙂

My raw data(Excel 2010) looks like this:

Time    Wagons     Delete     DayType    Plate    trainnumber   RowId
05.28    1                       1      0901-046     2          38676
08.20    2                       1      0901-003     2          18676
05.25    2            x          1      0901-046     2          28676
15.28    2                       1      0901-046     2          3676
23.20    3                       1      0601-001     2          3867
05.08    3                       1      0901-046     2          3876
00.28    L            x          1      0901-046     2          8676
00.00                            1      0901-046     2          367

I need a list that groups the Primary RowIds(the list from the previous question) with the RowIds that match it by the following criteria:

  • on the Primary and Matching rows the following must be the SAME:
    • trainnumber
    • DayType
    • Time
  • on the Primary and Matching rows the Plate must be DIFFERENT
  • Wagons must be MORE THAN 1 (The column contains rows with numbers, letters and nothing)
  • Delete must be empty

When matches are found, I need the RowId of the match.

Ideally a dataset like this:

PrimaryRowId    Match#1    Match#2    Match#3    Match#4
15674            5465        456       5456        45656
5564             231         132       1321        7862

It’s possible that there’s more matches per Primary RowId, but that’s ok.

My SQL skills is somewhat limited, so that’s why I’m asking you guys. 🙂

I think it might be something like this:

SELECT RowId
FROM Conversion
WHERE trainnumber=trainnumber and daytype=daytype and 
      time=time and plate<>plate and Wagons>1 and delete="" 
GROUP BY RowId

But it would only give me one(1) RowId at a time. :-/

  • 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-18T07:19:08+00:00Added an answer on June 18, 2026 at 7:19 am

    Chris,

    To use SQL to match items like this, you need to tell SQL to find the matches in a pair of tables (actually two copies of the same table). This is known as a self join.

    SELECT A.Time,
           A.TrainNumber,
           A.DayType,
           MIN(A.Rowid) AS Master
    FROM CONVERSION AS A
    INNER JOIN CONVERSION AS B ON A.TrainNumber=B.TrainNumber
    AND A.DayType=B.DayType
    AND A.Time=B.Time
    WHERE A.Wagons>"1"
      AND B.Wagons>"1"
      AND A.Plate<>B.Plate
      AND A.Delete IS NULL
      AND B.Delete IS NULL
    GROUP BY A.Time,
             A.TrainNumber,
             A.DayType;
    

    The two copies of Conversion are given the aliases A and B.

    On the INNER JOIN line, we specify the fields which have to match.

    On the WHERE line, we specify the fields which have to be different, as well as the other conditions (which must be specified for both A and B).

    The first column gives the lowest row ID for each matching train, and the second column the other matching row IDs.

    Now we have a list of master rows, we can join this again to Conversion to produce a list of slave rows :

    SELECT D.Master,
           C.Rowid
    FROM CONVERSION AS C
    INNER JOIN
      (SELECT A.Time,
              A.TrainNumber,
              A.DayType,
              MIN(A.Rowid) AS Master
       FROM CONVERSION AS A
       INNER JOIN CONVERSION AS B ON A.TrainNumber=B.TrainNumber
       AND A.DayType=B.DayType
       AND A.Time=B.Time
       WHERE A.Wagons>"1"
         AND B.Wagons>"1"
         AND A.Plate<>B.Plate
         AND A.Delete IS NULL
         AND B.Delete IS NULL
       GROUP BY A.Time,
                A.TrainNumber,
                A.DayType) AS D ON C.TrainNumber=D.TrainNumber
    AND C.DayType=D.DayType
    AND C.Time=D.Time
    WHERE C.Delete IS NULL
      AND D.Master<C.Rowid
    ORDER BY D.Master,
             C.Rowid;
    

    I’ve tested this with a little extra test data on SQL Fiddle

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

Sidebar

Related Questions

Based on my previous question I am running a query like so: SELECT DISTINCT
This question is based on my previous question which I got a working answer
After my previous question , I have an SQL query like the following: SELECT
As an extension to my previous question , I would like to automatically select
Just got this answer from a previous question and it works a treat! SELECT
I have question i.e how can i select a previous div CODE:- if($i >
In reference to my Previous Question someone said in select query if I set
This is a follow up to my previous question , in t-sql SELECT SCOPE_IDENTITY()
Following on from a previous question i asked, I'm now trying to figure out
I had a asked a previous question about generating a tree like structure which

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.