Sqlserver
 sql >> Baza danych >  >> RDS >> Sqlserver

Sortuj dane przed połączeniem za pomocą STUFF FOR XML

Wypróbuj to

SELECT  ROW_NUMBER() OVER ( ORDER BY Judge.EventJudgeID ) AS JudgeRow ,
        Judge.EventID ,
        Judge.Judge_PersonID ,
        STUFF(Select ',' + CAST(Fights.FightNumber AS VARCHAR(MAX)) AS [text()] From ( SELECT DISTINCT Fights.FightNumber
                FROM    dbo.tblFights Fights ,
                        dbo.tblFightJudge FRJudge
                WHERE   Fights.FightID = FRJudge.fightid
                        AND ( Judge.Judge_PersonID = FRJudge.judge1id
                              OR Judge.Judge_PersonID = FRJudge.judge2id
                              OR Judge.Judge_PersonID = FRJudge.judge3id
                            )
                      ) X 
             ORDER BY Fights.FightNumber
             FOR
                XML PATH('')
              ), 1, 1, '') AS BoutsJudged ,
        Persons.LastName + ' ' + Persons.FirstName AS JudgeName ,
        Events.EventName ,
        Events.EventDate
FROM    dbo.tblEventJudge Judge
        INNER JOIN dbo.tblPersons Persons ON PersonID = Judge_PersonID
        INNER JOIN dbo.tblEvents Events ON Events.EventID = Judge.EventID
WHERE   Judge.EventID = 1278;

Możesz sprawdzić poniżej sqls,

Przed :

Select *, 
       STUFF((Select Distinct ','+Cast(high as varchar(MAX)) 
              from master..spt_values where type = 'p' and number < 20  
              for xml Path('')),1,1,'')
from  master..spt_values where type = 'p' and number < 20

Po :

Select *, 
           STUFF((Select ','+Cast(high as varchar(MAX)) from (Select distinct     high 
    from master..spt_values where type = 'p' and number < 20) x Order by high  for xml Path('')),1,1,'')
    from  master..spt_values where type = 'p' and number < 20


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Jak działa funkcja CHAR() w SQL Server (T-SQL)

  2. Skrypt do automatycznego otwierania SQL Server Management Studio 2005 z innego interfejsu

  3. Jak uzyskać wszystkie dzieci rodzica, a następnie ich dzieci za pomocą rekurencji w zapytaniu?

  4. Ciągi połączeń programu SQL Server — kropka(.) lub (lokalne) lub (localdb)

  5. Nie można dołączyć przykładowej bazy danych w SQL Server 2012