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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:29:30+00:00 2026-06-17T22:29:30+00:00

I have the following sql view result which contains the audited changes for every

  • 0

I have the following sql view result which contains the audited changes for every field for every table in the system:

enter image description here

In this case the above image tell us that both read and write permissions were revoked for the user Lucas for the subscription called MySubscription.

I need to display that info in a grid however that is not what I want to display, I mean, I don´t want to display IDs. I need to display “Read” instead of 50, “Write” instead of 51, “Lucas” instead of 1 and “MySubscription” instead of 6.

To do that I would like to improve the sql view to get the values instead of their IDs as I mention above. The result that I am looking for is this one:
enter image description here

The database contains the tables Subscriptions, ProductPermissions and TenantUsers to get the needed info using joins.

Could you please give me some clues about how could I achieve what I need? Thank you.

  • 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-17T22:29:32+00:00Added an answer on June 17, 2026 at 10:29 pm

    You could do this with a series of LEFT JOINs, some casting might be required to get the datatype of the joining column the same as NewValue (I’ve assumed a column called Name in all your joining tables, this may need changing):

    SELECT  a.AuditLogId,
            a.Operation,
            a.[Table],
            a.RowId,
            a.Name,
            [OldValue] = COALESCE(s_Old.Name, pp_old.Name, t_Old.Name),
            [NewValue] = COALESCE(s_New.Name, pp_New.Name, t_New.Name)
    FROM    AuditLog a
            LEFT JOIN Subscriptions s_Old
                ON a.OldValue = CAST(s_Old.SubscriptionID AS VARCHAR)
                AND a.Name = 'SubscriptionID'
            LEFT JOIN ProductPermissions pp_Old
                ON a.OldValue = CAST(p_Old.ProductPermissionID AS VARCHAR)
                AND a.Name = 'ProductPermissionId'
            LEFT JOIN TenantUsers t_Old
                ON a.OldValue = CAST(t_Old.TenantUserId AS VARCHAR)
                AND a.Name = 'TenantUsers'
            LEFT JOIN Subscriptions s_New
                ON a.NewValue = CAST(s_New.SubscriptionID AS VARCHAR)
                AND a.Name = 'SubscriptionID'
            LEFT JOIN ProductPermissions pp_New
                ON a.NewValue = CAST(p_New.ProductPermissionID AS VARCHAR)
                AND a.Name = 'ProductPermissionId'
            LEFT JOIN TenantUsers t_New
                ON a.NewValue = CAST(t_New.TenantUserId AS VARCHAR)
                AND a.Name = 'TenantUsers'
    

    If required you could then PIVOT this into one row per transaction:

    SELECT  a.AuditLogId,
            a.Operation,
            a.[Table],
            a.RowId,
            [OldSubscriptionValue] = MAX(s_old.Name),
            [OldProductPermissionValue] = MAX(pp_old.Name),
            [OldTennantUserValue] = MAX(t_old.Name),
            [NewSubscriptionValue] = MAX(s_New.Name),
            [NewProductPermissionValue] = MAX(pp_New.Name),
            [NewTennantUserValue] = MAX(t_New.Name)
    FROM    AuditLog a
            LEFT JOIN Subscriptions s_Old
                ON a.OldValue = CAST(s_Old.SubscriptionID AS VARCHAR)
                AND a.Name = 'SubscriptionID'
            LEFT JOIN ProductPermissions pp_Old
                ON a.OldValue = CAST(p_Old.ProductPermissionID AS VARCHAR)
                AND a.Name = 'ProductPermissionId'
            LEFT JOIN TenantUsers t_Old
                ON a.OldValue = CAST(t_Old.TenantUserId AS VARCHAR)
                AND a.Name = 'TenantUsers'
            LEFT JOIN Subscriptions s_New
                ON a.NewValue = CAST(s_New.SubscriptionID AS VARCHAR)
                AND a.Name = 'SubscriptionID'
            LEFT JOIN ProductPermissions pp_New
                ON a.NewValue = CAST(p_New.ProductPermissionID AS VARCHAR)
                AND a.Name = 'ProductPermissionId'
            LEFT JOIN TenantUsers t_New
                ON a.NewValue = CAST(t_New.TenantUserId AS VARCHAR)
                AND a.Name = 'TenantUsers'
    GROUP BY a.AuditLogId, a.Operation, a.[Table], a.RowId;
    

    It is a pretty dirty solution, I would be inclined to store this data in the format you want to select it in i.e. instead of 50/51, store read/write directly in the NewValue column.

    Alternatively, if you did want the second format with one row per transaction then I’d be inclined to store it in this way. It could be worth reading about the Entity-Attribute-Value Antipattern.

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

Sidebar

Related Questions

Following this wikipedia article SQL join I wanted to have a clear view on
I have the following SQL snippet within a select statement: CASE WHEN wot.id LIKE
I have the following SQL Server stored procedure : BEGIN TRAN CREATE TABLE #TempTable
I have the following SQL statement which has a very bad performance: SELECT frmInstLastModifiedDate
I have the following SQL query which I use to fetch some rows from
I have a SQL Server view with following data: ID clientID surveyID questionID q_optionID
Hi I currently have a table view which is being filled via Core Data.
I have the following SQL statement in a legacy system I'm refactoring. It is
I have following SQL statementL: select DATE(bla), count(*) from tableA group by DATE(bla) UNION
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER

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.