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

  • Home
  • SEARCH
  • 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 8271813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:56:24+00:00 2026-06-08T06:56:24+00:00

I have a table attach with huge data sets, it is a temp table

  • 0

I have a table attach with huge data sets, it is a temp table and created by sql:

    id   number   name
    1      X1     name1
    2      X2     name2
    3      X3     name3
    4      X4     name4

table attachment_map

    id    item    attach_id  file_id  versionid
    1      X1       1          100         0
    2      X2       0           0          1

table version

   id    attach_id   
    1      2       

I want to have query to get:

    id   number   name    item
    1      X1     name1    X1
    2      X2     name2    X2
    3      X3     name3
    4      X4     name4

As you can see, the return rows added new column which actually got from table attachment_map, there have three impossibles:

1).attach don’t have item.

2).Have item, by connecting to column attach_id of attachment_map.

2).Have item, by connecting to column attach_id of version.

I wrote a query but having poor performance, executed it slowly and it seems because of union. can everybody tell another way or how I can improvee it? thanks

 WITH tb AS
(SELECT t.*,
 i.item
 FROM attach t,
  attachment_map am,
  version v
 WHERE (am.attach_id = t.attachid
OR (am.file_id      = 0
AND am.version_id   = v.id))
)
SELECT * FROM tb
UNION
SELECT tb.*, NULL FROM tb, attach WHERE tp.id NOT IN (attach.id);
  • 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-08T06:56:25+00:00Added an answer on June 8, 2026 at 6:56 am

    Use two LEFT OUTER JOINs to link the three tables, and then CASE the item fields in the order of priority (first inspect attach, then attachment_map, then version).

    SELECT t.*
    , case when t.attach_id is null then
        case when am.attach_id is null then
            v.item
        else
            am_attach.item
        end
    else
        t.item
    end as chosen_item
    
    FROM attach t
    left outer join attachment_map am on am.attach_id = t.attachid
    left outer join version v on am.version_id   = v.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a table report. I have put no , agency name ,
I have created HTML TABLE from JSON data of PHP using JQuery. After creating
I have table of data that is sorted as follows: Item | Sample |
I have table with column Percentage varchar(10) Data in that table is Pecentage 2/10
I have window that contains a table on screen, now I want to attach
I have a large number of rows in a table within which I wish
I have a table inside a form that looks as follows: <td><input type=text name=code[0]
I have some tables like this: USERS TABLE: | id | created | active
I already have a view controller, is it possible to attach a table view
I have a requirement to export some data from a table (not all fields

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.