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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:52:19+00:00 2026-05-18T11:52:19+00:00

I have a complicated query that I just cant create. I’m thinking of doing

  • 0

I have a complicated query that I just cant create. I’m thinking of doing it with JOIN’s but don’t know where to start. I’ll just try to explain my problem as clearly as possible.

I am creating software that deals with compensations. Every compensation can have several links (like in a chain). Each link is basically a customer. So, I’ll just tell you the table structures:

Customer

CustomerID | Name

Compensation

CompensationID | CustomerID | Date

Link

LinkID | CompensationID | CustomerID | Sequential

Now, the Sequential field increases with every link added. Let me demonstrate by filling the tables with some data:

CustomerID | Name
-----------+-----
0          | Foo
1          | Bar
2          | Baz

CompensationID | CustomerID | Date
---------------+------------+------
0              | 0          | 2-2-2010
1              | 1          | 2-3-2010


    LinkID | CompensationID | CustomerID | Sequential
    -------+----------------+------------+-----------
  0             0             0           0
  1             0             2           1
  2             0             1           2

So no matter what the LinkID/CompensationID/CustomerID the Sequential always goes from 0 to N in the Links table (depending on how many links a compensation has).

Now here is the problem: I want to list all Compensation’s whose Link’s meet the following requirements:

Search in Links
CustomerID = A
Sequential = 0 (first link) AND Sequential = LAST (in this case 2)
List compensations

Here is what I got so far:

SELECT * FROM Compensation JOIN Link ON Compensation.ID = Link.CompensationID
WHERE Link.CustomerID=A AND Link.Sequential = 0 AND Link.Sequential=LAST

This is more or less pseudo SQL since I know Link.Sequential cant be 0 and another value at the same time, but I don’t know how to do this.

Any help would be appreciated.

Thank you.

P.S.
Sorry for the big wall of text.

  • 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-05-18T11:52:19+00:00Added an answer on May 18, 2026 at 11:52 am

    If subqueries in where statements work how I remember:

    select * 
    from 
      Compensation
    left join
      Link
    on Compensation.CompensationID = Link.CompensationID
    where
      Link.CustomerID = :A
      AND (
        Link.Sequential = 0
        OR
        Link.Sequential = (
          select MAX(Sequential) from Link where Link.CustomerID = :A
        )
      )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been thinking about a complicated view/query I need to create but I
I have a query that is more complicated, but basically creates an HTML page
I have a Linq-to-Entities query that is not complicated but requires an .include and/or
Hi I have a really complicated dynamic query that i want to use to
I have a complicated report that I need to draw with GDI+ (I don't
Just out of curiosity, for applications that have a fairly complicated module tree, would
I have a complicated SELECT query that filters on a time range, and I
I have a fairly complicated linq query that returns an item and a string[]
I have this complicated SQL query for Oracle that I want to visualize in
I have a complicated MySQL query which takes a lot of time, selecting from

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.