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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:48:12+00:00 2026-06-17T09:48:12+00:00

I have a single table looking like this: Table extract Owner | Attribute |

  • 0

I have a single table looking like this:

Table extract

    Owner   | Attribute | value
----------------------------------------------------
    10      | COLOR     | BLUE
    10      | COLOR     | RED
    10      | COLOR     | GREEN
    10      | SIZE      | BIG
    20      | COLOR     | GREEN
    20      | SIZE      | MEDIUM
    20      | MEMORY    | 16G
    20      | MEMORY    | 32G
    30      | COLOR     | RED
    30      | COLOR     | BLUE
    30      | MEMORY    | 64G

Is there a SQL which will calculate a combination of all attribute with a single index (last column in the result):

Owner   | Attribute | Value | Rule_No
10      | COLOR     | BLUE  | 1
10      | SIZE      | BIG   | 1
10      | COLOR     | RED   | 2
10      | SIZE      | BIG   | 2
10      | COLOR     | GREEN | 3
10      | SIZE      | BIG   | 3
20      | COLOR     | GREEN | 1
20      | SIZE      | MEDIUM| 1
20      | MEMORY    | 16G   | 1
20      | COLOR     | GREEN | 2
20      | SIZE      | MEDIUM| 2
20      | MEMORY    | 32G   | 2
30      | COLOR     | BLUE  | 1
30      | MEMORY    | 64G   | 1
30      | COLOR     | RED   | 2
30      | MEMORY    | 64G   | 2

The rule number would be unique per owner (rule ‘1’ for owner ’10’ is not related to rule ‘1’ for owner ’20’.

I tried to use the SQL cross join, but the number of attributes is not fixed, then I cannot use it (one cross join per attribute is needed) and I want the combination to be new rows instead new columns.

I am trying to use Talend Open Studio - Data Integration to do it but a solution using only SQL would be better for me.

  • 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-17T09:48:14+00:00Added an answer on June 17, 2026 at 9:48 am

    Do you really want the data in the form given in your question (which would then require further aggregation on Rule_No to be useful in most likely situations), or are you ultimately seeking to pivot it? That is, rules are joined together (with each attribute becoming its own column) as follows:

    +---------+-------+-------+--------+--------+
    | Rule_No | Owner | COLOR | SIZE   | MEMORY |
    +---------+-------+-------+--------+--------+
    |       1 |    10 | BLUE  | BIG    | NULL   |
    |       2 |    10 | RED   | BIG    | NULL   |
    |       3 |    10 | GREEN | BIG    | NULL   |
    |       1 |    20 | GREEN | MEDIUM | 16G    |
    |       2 |    20 | GREEN | MEDIUM | 32G    |
    |       1 |    30 | RED   | NULL   | 64G    |
    |       2 |    30 | BLUE  | NULL   | 64G    |
    +---------+-------+-------+--------+--------+
    

    One can pivot such data with a query as follows:

    SELECT   @t:=IF(Owner=@o,@t,0)+1 AS Rule_No,
             @o:=Owner AS Owner,
             `COLOR`,`SIZE`,`MEMORY`
    FROM     (SELECT DISTINCT Owner, @t:=0 FROM my_table) t0
    
      LEFT JOIN (
        SELECT Owner, value AS `COLOR`
        FROM   my_table
        WHERE  Attribute='COLOR'
      ) AS `t_COLOR` USING (Owner)
    
      LEFT JOIN (
        SELECT Owner, value AS `SIZE`
        FROM   my_table
        WHERE  Attribute='SIZE'
      ) AS `t_SIZE` USING (Owner)
    
      LEFT JOIN (
        SELECT Owner, value AS `MEMORY`
        FROM   my_table
        WHERE  Attribute='MEMORY'
      ) AS `t_MEMORY` USING (Owner)
    
    ORDER BY Owner, Rule_No
    

    As the attribute list is dynamic, one can use a query to construct the above SQL from which one prepares and executes a statement:

    SELECT CONCAT('
             SELECT   @t:=IF(Owner=@o,@t,0)+1 AS Rule_No,
                      @o:=Owner AS Owner,
                      ', GROUP_CONCAT(DISTINCT CONCAT(
                        '`',REPLACE(Attribute,'`','``'),'`'
                      )), '
             FROM     (SELECT DISTINCT Owner, @t:=0 FROM my_table) t0
           ', GROUP_CONCAT(DISTINCT CONCAT('
               LEFT JOIN (
                 SELECT Owner, value AS `',REPLACE(Attribute,'`','``'),'`
                 FROM   my_table
                 WHERE  Attribute=',QUOTE(Attribute),'
               ) AS `t_',REPLACE(Attribute,'`','``'),'` USING (Owner)
             ') SEPARATOR ''), '
             ORDER BY Owner, Rule_No
           ') INTO @sql
    FROM   my_table;
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    See it on sqlfiddle.

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

Sidebar

Related Questions

Let's say you have a database with a single table like... --------------------------------------------- | Name
I have a user table that has many columns, it looks roughly like this:
I have two tables. First table looks like this: Table: Records rid | user
I have a messages table which looks like this: +------------+-------------+----------+ | sender_id | created_at
I have a table Emp like this for example. ---------------------- eName | eId ----------------------
I'm working on a project that will have a single table holding lots and
I have a single product table with multiple fields which contain user evaluations of
I have the scenario where the data from a single table must be in
I have a huge access mdb file which contains a single table with 20-30
I have a simple database named customer with a single table data.I want to

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.