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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:48:10+00:00 2026-05-13T01:48:10+00:00

I have a table used by one piece of 3rd party software (say user_fields)

  • 0

I have a table used by one piece of 3rd party software (say user_fields) that has for each row a user_id, a field_id and a field_value. Crappy I know.

I also have another user table of my own, users, that contains the values needed to populate this, except the field_id which is “magic” and corresponds to the custom field mapping.

How can I write a select query that will turn this:

user_id | company_name       | first_name | country
---------------------------------------------------
1       | Nutty Choc Company | Si         | GB
2       | Ljubljanske R Us   | Pie        | IT

Into this?

user_id | field_value        | field_id
---------------------------------------
1       | Nutty Choc Company | 10
1       | Si                 | 11
1       | GB                 | 12
2       | Ljubljanske R Us   | 10
2       | Pie                | 11
2       | IT                 | 12

I’m using Postgresql 8.4, and intend the query to be fed to insert.

I had a stab at it by using unnest and an array, to expand the fields into rows, but it unnests after processing any other field in the select, and so just duplicates the value for the unnested rows.

Any ideas?

Cheers,
Si

EDIT: formatting and clarification that dodgy name-value table is 3rd party.

  • 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-13T01:48:11+00:00Added an answer on May 13, 2026 at 1:48 am

    I’m adding this as a separate answer since it’s quite different from the other that I’ve posted. You could try something like this:

    SELECT
         U.id,
         UF.id AS field_id,
         CASE UF.id
              WHEN 10 THEN U.first_name
              WHEN 11 THEN U.country
              WHEN 12 THEN U.company_name
              ELSE NULL
         END AS field_value
    FROM
         Users U
    CROSS JOIN User_Fields UF
    

    This is assuming that you have a User_Fields table (or whatever it might be named) which gives you all of the user fields that are possible. You would have to keep your CASE statement up to date though.

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

Sidebar

Related Questions

I have a table that contains in each row an input and a save
I am working with a microcontroller that has an external EEPROM containing tables of
One of our teams is developing a database that will be somewhat large (~500GB)
I have renamed a table in a SQL Server 2008 database, from eL_CourseStepUserNotes to
I have a table in a mySQL database we'll call 'tbl' where the fields
I'm implementing a database where several tables have string data as candidate keys (eg:
I've been tasked with doing refactoring to a Java Swing application we have here
I have 2 tables, Table1: id,int1,int2,int3,int4,int5 Table2: integers (autoincrement),blobdata The query I want to
By some setting as using IDENTITY_INSERT we have duplicate entry in identity column, What
I am trying to define a one to many relationship between Category and Project

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.