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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:39:20+00:00 2026-05-26T08:39:20+00:00

I have been thinking about a complicated view/query I need to create but I

  • 0

I have been thinking about a complicated view/query I need to create but I can’t seem to find a good (or working) solution to my problem. First I will give you a piece of my database structure to help explain my problem:

sql server diagram

Every Agreement usually has 3 persons involved: a client, an endclient, and a contactperson. These role types are defined in the PersonAgreementInvolvementRole table. What I want is to create a view where I can get all three person names involved in the agreement, so something like this:

AgreementID  |  ClientName  |  EndClientName  |  ContactPersonName

          1  |  Company1    |     Company2    |     Smith

Because I need to go to the same (Person) table everytime to get the name of the person involved, I don’t know how I can do this best. I first tried something like this:

select ag.StartDate, ag.EndDate, ag.PriceRate, ag.TaskDescription, pc.Name as ClientName, pec.Name as EndClientName
from dbo.Agreement ag inner join dbo.PersonAgreementInvolvement pai
on ag.AgreementID = pai.AgreementID
inner join dbo.Person pc
on pai.PersonID = pc.PersonID
inner join dbo.PersonAgreementInvolvementRole pairc
on pai.PersonAgreementInvolvementRoleID = pairc.PersonAgreementInvolvementRoleID
inner join dbo.Person pec
on pai.PersonID = pec.PersonID
inner join dbo.PersonAgreementInvolvementRole pairec
on pai.PersonAgreementInvolvementRoleID = pairec.PersonAgreementInvolvementRoleID
where pairec.Value = 'Client'
and
pairc.Value = 'EndClient'

but that didn’t work (it returned no data at all), after that I tried a query with a union but that also didn’t work.

Any of you have an idea? More information can be provided if needed, just ask!

Example data:

I have an agreement (AgreementID = 1) with two persons involved. There is a person with the name “Google” which has the InvolvementRole “Client” and another person named “Microsoft” which has the InvolvementRole “EndClient”. What I would like to get from my query is the following:

AgreementID  -  Client  -  EndClient
-------------------- -----------------
1               Google     Microsoft

EDIT: SOLVED! Ultimately, this query did the job:

SELECT  ag.StartDate, ag.EndDate, ag.PriceRate, CAST( ag.TaskDescription AS VARCHAR(8000)),
        MIN(CASE WHEN pair.Value = 'Klant' THEN pc.Name END) as Klant, 
        MIN(CASE WHEN pair.Value = 'Eindklant' THEN pc.Name END) as Eindklant,
        MIN(CASE WHEN pair.Value = 'ContactPerson' THEN pc.Name END) as ContactPersonName
FROM dbo.Agreement ag
INNER JOIN dbo.PersonAgreementInvolvement pai 
ON ag.AgreementID = pai.AgreementID 
inner join dbo.PersonAgreementInvolvementRole pair
on pai.PersonAgreementInvolvementRoleID = pair.PersonAgreementInvolvementRoleID
LEFT JOIN dbo.Person pc
ON pai.PersonID = pc.PersonID 
GROUP BY ag.StartDate, ag.EndDate, ag.PriceRate, CAST( ag.TaskDescription AS VARCHAR(8000))

Thanks to everyone who helped me solve it! 🙂

  • 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-26T08:39:20+00:00Added an answer on May 26, 2026 at 8:39 am

    If I understood your structure correctly, this should do the trick:

    UPDATED QUERY

    WITH PersonsInvolved AS
    (
        SELECT  AgreementID,
                MIN(CASE WHEN pai.Value = 'Client' THEN pc.Name END) as ClientName,
                MIN(CASE WHEN pai.Value = 'EndClient' THEN pc.Name END) as EndClientName,
                MIN(CASE WHEN pai.Value = 'ContactPerson' THEN pc.Name END) as ContactPersonName
        FROM dbo.PersonAgreementInvolvement pai
        LEFT JOIN dbo.Person pc
        ON pai.PersonID = pc.PersonID
        GROUP BY AgreementID
    )
    SELECT  ag.StartDate, ag.EndDate, ag.PriceRate, ag.TaskDescription,
            pin.ClientName, pin.EndClientName, pin.ContactPersonName
    FROM dbo.Agreement ag 
    INNER JOIN PersonsInvolved pin
    ON ag.AgreementID = pin.AgreementID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that I have been thinking about, but I can't really
I have been thinking about the optimal way to create an XML file using
I have been thinking about this issue and I can't figure it out. Perhaps
I have been thinking about this. I am aware you can use json_encode(); to
I have been thinking about building a client to client program. But the way
I have been thinking about a regular expression that can transform a list like
I have been thinking about adding a button to a View which when pressed
I have been thinking about this problem for long enough but cannot come up
Just something I have been thinking about. It is possible to create an android
I have been thinking about a neat way of load balancing and one thing

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.