Question

I have the following query which returns a result with person details. I have 2 person types in the database as OWNER and CONTACT. I only need name and address from the OWNER and the email from the CONTACT.

I have written the following query and please check.

SELECT DISTINCT 
        s.[id],
        s.[entity_id],
            s.[transfered],
        s.[meta],
        s.[operation]
        c.[Company_Name] as CompanyName,
        ISNULL(pa.HouseName,'') as HouseName,
        ISNULL(pa.Street,'') as Street,
        ISNULL(pa.Town,'') as Town,
        ISNULL(pa.City,'') as City,
        ISNULL(pa.County,'') as County,
        ISNULL(pa.Country,'') as Country,
        ISNULL(pa.Postcode,'') as PostCode,     
        ISNULL(p.Title,'') as Title,
        ISNULL(p.Forenames,'') as FirstName,
        ISNULL(p.Surname,'') as LastName,
        ISNULL(pc.Email,'') as Email

  FROM 
        dbo.COMPANY c INNER JOIN dbo.Sage_Company_Sync s ON s.entity_id = c.Company_ID
        INNER JOIN dbo.CompanyPostalAddress cpa ON c.Company_ID = cpa.CompanyId
        INNER JOIN dbo.POSTAL_ADDRESS pa ON cpa.PostalAddressId = pa.Address_ID
        LEFT JOIN dbo.CompanyPerson cp ON cp.CompanyId = c.company_id
        INNER JOIN dbo.PERSON p ON p.Person_ID = cp.PersonId
        INNER JOIN dbo.PERSON pc ON pc.Person_ID = cp.PersonId

  WHERE 
        cpa.PostalAddressTypeId = 2
        and cp.PersonTypeId in (1,2)
        and s.transfered = 0
        and s.entity_type = 'company'       
  ORDER BY 
        s.operation ASC

The above query returns 2 rows. One for OWNER and one for COMPANY. My question is how can I make my query only return name and address details for OWNER and email for CONTACT in one row?

Update

Results

 id entity_id   transfered  meta    operation   CompanyName HouseName   Street  Town    City    County  Country PostCode    Title   FirstName   LastName    Email   PersonType
5   25  0       U   Testing     2  (home Address)   Stoneleigh  Epsom   Surrey  Uk  KTJ19 0PQ   Mr  Dfdf    Dfdf    test@gmail.com  CONTACT
5   25  0       U   Testing     2  (home Address)   Stoneleigh  Epsom   Surrey  Uk  KT1J9 0PQ   Mr  Testing     Testing     OWNER

Desired Results

id  entity_id   transfered  meta    operation   CompanyName HouseName   Street  Town    City    County  Country PostCode    Title   FirstName   LastName    Email   PersonType
5   25  0       U   Testing     2 Westways (home Address)   Stoneleigh  Ezpsofm Surrey  Uk  KT19 Q  Mr  Testing     Testing test@gmail.com  OWNER

The email is actually CONTACT persons email.

Thanks,

Was it helpful?

Solution

You can store the result of your query in a temporary table or a Common Table Expression and build another query on that.

Consider the following example:

DECLARE @tbl TABLE (ID INT, Name VARCHAR(20), Email VARCHAR(20), Relation VARCHAR(20))

INSERT INTO @tbl VALUES (1, 'Somebody', 'xyz@domain.com', 'Employer'), (1, 'Someone', 'Test', 'Employee');

WITH cte1 AS
(
    SELECT * FROM @tbl WHERE Relation = 'Employer'
),
cte2 AS
(
    SELECT t.ID, t.Name, c.Email, t.Relation FROM cte1 c INNER JOIN @tbl t ON c.ID = t.ID WHERE t.relation = 'Employee'
)

SELECT* FROM cte2

You can also check out this SQLFiddle example:

SQLFiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top