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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:57:03+00:00 2026-06-14T03:57:03+00:00

Please see http://sqlfiddle.com/#!3/2506f/2/0 I have two tables. One is a general record, and the

  • 0

Please see http://sqlfiddle.com/#!3/2506f/2/0

I have two tables. One is a general record, and the other is a table containing related documents that link to that record.

In my example I’ve created a straightforward query which shows all records and their associated documents. This is fine, but I want a more complex situation.

In the ‘mainrecord’ table there is a ‘multiple’ field. If this is 0, then I only want the most recent document from the documents table (that is, with the highest ID). If it is 1, I want to join all linked documents.

So, rather than the result of the query being this:-

ID  NAME    MULTIPLE    DOCUMENTNAME    IDLINK
1   One     1           first document    1
1   One     1           second document   1
2   Two     0           third document    2
2   Two     0           fourth document   2
3   Three   1           fifth document    3
3   Three   1           sixth document    3

It should look like this:-

ID  NAME    MULTIPLE    DOCUMENTNAME    IDLINK
1   One     1           first document    1
1   One     1           second document   1
2   Two     0           fourth document   2
3   Three   1           fifth document    3
3   Three   1           sixth document    3

Is there a way of including this condition into my query to get the results I’m after. I’m happy to explain further if needed.

Thanks in advance.

  • 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-14T03:57:04+00:00Added an answer on June 14, 2026 at 3:57 am
    WITH myData
    AS
    (SELECT mainrecord.*, documentlinks.documentName, documentlinks.idlink,
    Row_number()
                      OVER (
                        partition BY mainrecord.ID
                        ORDER BY mainrecord.ID ASC) AS ROWNUM
    FROM mainrecord INNER JOIN documentlinks
    ON mainrecord.id = documentlinks.idlink)
    SELECT *
    FROM mydata o
    WHERE multiple = 0 AND rownum =
    (SELECT max(rownum) FROM mydata i WHERE i.id = o.id)
    UNION
    SELECT *
    FROM myData
    WHERE multiple = 1
    

    http://sqlfiddle.com/#!3/2506f/57

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

Sidebar

Related Questions

Please see: http://jsfiddle.net/XEbKy/3/ I have two layers of tabs. On the top layer I
Please see http://www.sqlfiddle.com/#!3/fb107/3 for an example schema and query I want to run. I
Please see the jsFiddle http://jsfiddle.net/Lijo/ryDnn/1/ . I have two columns layout. It is %
Please see this IBM site - http://www.ibm.com/us/en/ If you see the site banners, there
please see web site below that i designed (it's from a template): http://noorjamali.soscharge.com my
Basically my problem is with the page at http://wiki.diablocommunity.com/index.php?title=Tristram_Cathedral Please see the screen shots
Please go to: http://jlecologia.com/page1c.html to see the problem The top box look fine but
Can other Safari users please test http://recaptcha.net/learnmore.html for me, to see if the audio
please see: http://jsfiddle.net/4Z4fQ/19/ consider the following html: <a id=myElement href=http://www.google.com/> <img id=image src=http://www.google.com/intl/en_com/images/srpr/logo3w.png alt=google/>
please see: http://pastebin.com/5za3uCi1 I'm quite new to php and I'm editing the ventrilo status

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.