2

I am trying to display multiple authors per title in a single column. At the moment there a repeating rows, due to the fact that some Titles have more than 1 FirstName. Is there a form of concatenation that can be used to resolve this and display all the authors in a single filed and perhaps separated by a comma.

This is my current query:

SELECT 
    Submission.Title, Researcher.FirstName, Submission.Type
FROM 
    Submission 
INNER JOIN 
    ((Faculty 
INNER JOIN 
    School ON Faculty.FacultyID = School.[FacultyID]) 
INNER JOIN 
    (Researcher 
INNER JOIN 
    ResearcherSubmission ON Researcher.ResearcherID = ResearcherSubmission.ResearcherID) 
             ON School.SchoolID = Researcher.SchoolID) 
             ON Submission.SubmissionID = ResearcherSubmission.SubmissionID
GROUP BY
    Submission.Title, Researcher.FirstName, Submission.Type;

This the output it generates:

[current output[1]

this is the output I am trying to generate:

Title                        FirstName                         Type
---------------------------------------------------------------------------
21st Century Business        Matthew, Teshar                   Book Chapter
A Family Tree...             Keshant, Lawrence                 Book Chapter
Benefits of BPM...           Jafta                             Journal Article
Business Innovation          Matthew, Morna, Teshar            Book Chapter
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kivan Ilangakoon
  • 457
  • 1
  • 10
  • 24

3 Answers3

3

You can do this in your application logic as well.

But if you want to do this with a query. You should be able do something like this:

SELECT DISTINCT
    sm.Title,
    STUFF(
        (SELECT      ', ' + r.FirstName
        FROM    ResearcherSubmission rs
            INNER JOIN Researcher r ON r.ResearcherID = rs.ResearcherID
        WHERE      sm.SubmissionID = rs.SubmissionID
        FOR XML PATH('')), 1, 2, '') AS FirstNames,
        sm.Type
FROM Submission sm
Sam Segers
  • 1,951
  • 2
  • 22
  • 28
3

You may inclde the concantenation logic within a CROSS APPLY

SELECT
      Submission.Title
    , CA.FirstNames
    , Submission.Type
FROM Submission
      CROSS APPLY (
            SELECT
                  STUFF((
                        SELECT /* DISTINCT ??? */
                              ', ' + r.FirstName
                        FROM ResearcherSubmission rs
                              INNER JOIN Researcher r ON r.ResearcherID = rs.ResearcherID
                        WHERE Submission.SubmissionID = rs.SubmissionID
                        FOR XML PATH (''), TYPE
                        ).value('.', 'NVARCHAR(MAX)'), 1, 2, ' ')
          ) AS CA (FirstNames)
GROUP BY
      Submission.Title
    , CA.FirstNames
    , Submission.Type
;

NB: I'm not sure if you need to include DISTINCT into the subquery when concatenating the names, e.g. if these was 'Jane' (Smith) and 'Jane' (Jones) do you want the final list as: 'Jane' or 'Jane, Jane'?

Paul Maxwell
  • 33,002
  • 3
  • 32
  • 51
1

You can use the below query to generate the o/p as you want from the o/p that you have got.

    CREATE TABLE #temptable(Title VARCHAR(200), FirstName VARCHAR(200), Type VARCHAR(200))
    INSERT INTO #temptable
    SELECT 'Book1','Matt','Chapter' UNION
    SELECT 'Book1','Tesh','Chapter' UNION
    SELECT 'BPM','Jafta','Article'  UNION
    SELECT 'Ethics','William','Journal' UNION
    SELECT 'Ethics','Lawrence','Journal' UNION
    SELECT 'Ethics','Vincent','Journal' UNION
    SELECT 'Cellular','Jane','Conference'
    SELECT  Title
           ,STUFF((SELECT ', ' + CAST(FirstName AS VARCHAR(10)) [text()]
             FROM #temptable 
             WHERE Title = t.Title
             FOR XML PATH(''), TYPE)
            .value('.','NVARCHAR(MAX)'),1,2,' ') List_Output
            ,Type
    FROM #temptable t
    GROUP BY Title,Type
Abhishek
  • 2,482
  • 1
  • 21
  • 32
  • I see. it does display as expected, but what happens when I create new books with different Names and Types.. isnt there away to simply pickup from database without typing out the Titles, Name and Type? – Kivan Ilangakoon Sep 27 '15 at 12:01
  • @KivanIlangakoon - So, I added one new entry with different Name and Type and the o/p will come as expected. In any case if I misunderstood by what you meant could you please edit your question with the correct requirement. – Abhishek Sep 27 '15 at 13:01