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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:40:35+00:00 2026-05-21T16:40:35+00:00

I have a requirement that I’m struggling to implement. If possible, I’d like to

  • 0

I have a requirement that I’m struggling to implement. If possible, I’d like to achieve this with native T-SQL.

I have the following tables:

CUSTOMER
========
ID,
Name

FIELDDEF
========
ID,
Name
FieldType   (Char  T, N, D   for Text, Number or Date)

CUSTOMERFIELD
=============
ID,
CustomerID,
FieldDefID,
CaptureDate,
ValueText,
ValueNumber,
ValueDate

Basically, the purpose of these tables is to provide an extensible custom field system. The idea is that the user creates new field definitions that can be a text, number or date field. Then, they create values for these fields in the ValueText, ValueNumber OR ValueDate field.

Example:

*Customer*
1,BOB
2,JIM

*FieldDef*
1,Mobile,T
1,DateOfBirth,D

*CustomerField*
ID,CustomerID,FieldDefID,CaptureDate,ValueText,ValueNumber,ValueDate
1,1,1,2011-01-1,07123456789,NULL,NULL
2,1,2,2011-01-1,NULL,NULL,09-DEC-1980
3,1,1,2011-01-2,07123498787,NULL,NULL

I need to create a view that looks like this:

*CustomerView*
ID,Name,Mobile,DateOfBirth
1,BOB,07123498787,09-DEC-1980

Note that Bob’s mobile is the second one in the list, because it uses the most recent capture date.

Ideally, I need this to be extensible, so if I create a new field def in the future, it is automatically picked up in the CustomerView.

Is this possible in T-SQL at all?

Thanks,

Simon.

  • 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-21T16:40:36+00:00Added an answer on May 21, 2026 at 4:40 pm

    This would not be possible with a view, unless the view is dynamically recreated on the fly every time FieldDef changes because view schemas are locked-in at creation time. However, it may be possible with a stored procedure, which may or may not work depending on how you are using it.

    Edit 1

    Here is a sample query that works just for your current field names, and would have to be modified by dynamic SQL to work in general:

    Edit 2

    Modified to grab the newest values from the customer field table

    with CustomerFieldNewest as (
        select
            cf1.*
        from
            customerfield cf1
        inner join
            (
                select
                    customerid,
                    fielddefid,
                    max(capturedate) as maxcapturedate
                from
                    customerfield cf2
                group by
                    customerid,
                    fielddefid
            ) cf2 on cf1.customerid = cf2.customerid
                and cf1.fielddefid = cf2.fielddefid
                and cf1.capturedate = cf2.maxcapturedate
    )
    ,CustomerFieldPivot as (
        select
            C.ID as ID
            ,max(case when F.Name = 'Mobile' then CF.ValueText end) as Mobile
            ,max(case when F.Name = 'DateOfBirth' then CF.ValueDate end) as DateOfBirth
        from
            Customer C
        left join
            CustomerFieldNewest CF on C.ID = CF.CustomerID
        left join
            FieldDef F on F.ID = CF.FieldDefID
        group by
            C.ID
    )
    select
        C.*
        ,P.Mobile
        ,P.DateOfBirth
    from
        Customer C
    left join
        CustomerFieldPivot P on C.ID = P.ID
    

    Edit 3

    Here is T-SQL code to generate the view on the fly based on the current set of fields in FieldDef (this assumes the view CustomerView already exists, so you will need to create it first as a blank definition or you will get an error). I’m not sure about the performance of all this, but it should work correctly.

    declare @sql varchar(max)
    declare @fielddef varchar(max)
    declare @fieldlist varchar(max)
    
    select
        @fielddef = coalesce(@fielddef + ', ' + CHAR(13) + CHAR(10), '') +
            '           max(case when F.Name = ''' + F.Name + ''' then CF.' +
                case F.FieldType
                    when 'T' then 'ValueText'
                    when 'N' then 'ValueNumber'
                    when 'D' then 'ValueDate'
                end
            + ' end) as [' + F.Name + ']'
    
        ,@fieldlist = coalesce(@fieldlist + ', ' + CHAR(13) + CHAR(10), '') +
            '       [' + F.Name + ']'
    from
        FieldDef F
    
    set @sql = '
        alter view [CustomerView] as
    
        with CustomerFieldNewest as (
            select
                cf1.*
            from
                customerfield cf1
            inner join
                (
                    select
                        customerid,
                        fielddefid,
                        max(capturedate) as maxcapturedate
                    from
                        customerfield cf2
                    group by
                        customerid,
                        fielddefid
                ) cf2 on cf1.customerid = cf2.customerid
                    and cf1.fielddefid = cf2.fielddefid
                    and cf1.capturedate = cf2.maxcapturedate
        )
        ,CustomerFieldPivot as (
            select
                C.ID as ID,
    ' + @fielddef + '
            from
                Customer C
            left join
                CustomerFieldNewest CF on C.ID = CF.CustomerID
            left join
                FieldDef F on F.ID = CF.FieldDefID
            group by
                C.ID
        )
        select
            C.*,
    ' + @fieldlist + '
        from
            Customer C
        left join
            CustomerFieldPivot P on C.ID = P.ID
    '
    
    print @sql
    exec(@sql)
    
    select * from CustomerView
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this requirement that i need to Update/Save all records from DatabaseA on
I have a requirement that in a SQL Server backed website which is essentially
I have a requirement that very closely matches this Jquery demo , which is
I have this requirement that states that I must have a function signature as
Using SQL Server 2008, I have a requirement that email addresses in my user
I have this requirement that I need to replace URL in CSS, so far
In our project we have requirement that, after receiving sms message from third party
I have a requirement that when the user clicks on [X] button of the
I have a requirement that I want to check the request headers and according
I have a requirement that a user is allowed only to enter string value

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.