Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How I can show detail rows as columns (free configurable user properties) with MS SQL

How I can show detail rows as columns (free configurable user properties) with MS SQL

Scheduled Pinned Locked Moved Database
databasedesignquestionlounge
1 Posts 1 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Dear Experts Please enlighten me. Please Note: The following is reduced to a minimum to keep it simple. In the original design there is at least one table more to hold the UserPropertyDefinitions and some other stuff to keep it more general to hold UserProperties and their definitions for several tables. Let assume we have a Master table ...

    CREATE TABLE Products
    (
    Id int,
    Name char(50)
    );
    INSERT INTO Products(Id, Name) VALUES (1, 'Prod1'),(2, 'Prod2');

    ... and a table which holds the user defined properties and values

    CREATE TABLE ProductUserProperties
    (
    Id int,
    MasterId int,
    PropertyName char(50),
    PropertyValue char(50)
    );
    INSERT INTO ProductUserProperties (Id, MasterId, PropertyName, PropertyValue) VALUES
    (1, 1, 'PropA', 'Prod1PropA_UserValue'),
    (2, 1, 'PropB', 'Prod1PropB_UserValue'),
    (3, 2, 'PropA', 'Prod2PropA_UserValue'),
    (4, 2, 'PropB', 'Prod2PropB_UserValue');

    A simple query by left joins ...

    SELECT
    Products.Id Products_Id,
    Products.Name Products_Name,
    ProductUserProperties.Id ProductUserProperties_Id,
    ProductUserProperties.MasterId ProductUserProperties_MasterId,
    ProductUserProperties.PropertyName ProductUserProperties_PropertyName,
    ProductUserProperties.PropertyValue ProductUserProperties_PropertyValue
    FROM Products
    LEFT JOIN ProductUserProperties ON ProductUserProperties.MasterId = Products.Id
    ORDER BY Products.Id, ProductUserProperties.PropertyName

    ...results in (column names shortened):

    Prod_Id Prod_Name ProdUsrProps_Id ProdUsProp_MstrId PropertyName PropertyValue
    1 Prod1 1 1 PropA Prod1PropA_UserValue
    1 Prod1 2 1 PropB Prod1PropB_UserValue
    2 Prod2 3 2 PropA Prod2PropA_UserValue
    2 Prod2 4 2 PropB Prod2PropB_UserValue

    But what I like to have is something like this

    Products_Id Products_Name PropA PropB ... and more properties
    1 Prod1 Prod1PropA_UserValue Prod1PropB_UserValue ...
    2 Prod2 Prod2PropA_UserValue Prod2PropB_UserValue ....

    Finally the Question

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups