Sqlserver
 sql >> Teknologi Basis Data >  >> RDS >> Sqlserver

Bagaimana cara menggunakan XQuery untuk mensimulasikan STRING_AGG() (penggabungan string yang dikelompokkan)?

Apakah Anda mencari ini?

select 
 isnull(STUFF(a.x.query('for $s in  entity/info/cleaned_name return <x>{concat(",",$s)}</x>').value('.','varchar(max)'),1,1,''),'') as 'Nazwa podmiotu' 
,isnull(STUFF(a.x.query('for $s in proceeding/signatures return <x>{concat(",",$s)}</x>').value('.','varchar(max)'),1,1,''),'') as 'Sygnatura'
,isnull(STUFF(a.x.query('for $s in entity
                         return <x>
                                {
                                concat(", ",($s/address/zip_code/text())[1]," "
                                           ,($s/address/town/text())[1]," "
                                           ,($s/address/street/text())[1]," "
                                           ,($s/address/house_number/text())[1],"/"
                                           ,($s/address/flat_number/text())[1]
                                          )
                                }
                                </x>').value('.','varchar(max)'),1,2,''),'') 
    from @xml.nodes('/root/Row') as a(x);

Hasilnya

Nazwa podmiotu          Sygnatura                           AllAdresses
Kate Smith,John Smith   V GU 86/18,V GUp 9/19,V GUp 8/19    00-001 London  Downing Street 1 /1, 00-001 Washington  Pennsylvania Avenue 1/1

PERBARUI Beberapa alamat dan data yang identik

Anda dapat mencoba ini (sesuai dengan komentar Anda)

Data pengujian Anda dengan satu alamat kedua dan satu alamat yang disalin:

declare @xml as xml = '<root>
    <Row>
        <proceeding>
            <signatures>V GU 86/18</signatures>
            <signatures>V GUp 9/19</signatures>
            <signatures>V GUp 8/19</signatures>
        </proceeding>
        <entity>
            <info>
                <cleaned_name>Kate Smith</cleaned_name>
            </info>
            <address>
                <town>London </town>
                <house_number>1 </house_number>
                <flat_number>1</flat_number>
                <street>Downing Street</street>
                <zip_code>00-001</zip_code>
            </address>
            <address>
                <town>Yorkshire </town>
                <house_number>1 </house_number>
                <flat_number>1</flat_number>
                <street>Morning Street</street>
                <zip_code>00-999</zip_code>
            </address>
        </entity>
        <entity>
            <info>
                <cleaned_name>John Smith</cleaned_name>
            </info>
            <address>
                <town>Washington </town>
                <house_number>1</house_number>
                <flat_number>1</flat_number>
                <street>Pennsylvania Avenue</street>
                <zip_code>00-001</zip_code>
            </address>
            <address>
                <town>Washington </town>
                <house_number>1</house_number>
                <flat_number>1</flat_number>
                <street>Pennsylvania Avenue</street>
                <zip_code>00-001</zip_code>
            </address>
        </entity>
    </Row>
</root>'

--Permintaan

select 
 isnull(STUFF(a.x.query('for $s in  entity/info/cleaned_name return <x>{concat(",",$s)}</x>').value('.','varchar(max)'),1,1,''),'') as 'Nazwa podmiotu' 
,isnull(STUFF(a.x.query('for $s in proceeding/signatures return <x>{concat(",",$s)}</x>').value('.','varchar(max)'),1,1,''),'') as 'Sygnatura'
,isnull(STUFF(a.x.query('for $s in entity/address
                            return
                            <x>{concat(", ",($s/zip_code/text())[1]," "
                                           ,($s/town/text())[1]," "
                                           ,($s/street/text())[1]," "
                                           ,($s/house_number/text())[1],"/"
                                           ,($s/flat_number/text())[1]
                                       )}</x>')
                   .query('for $a in distinct-values(/x/text()) return $a').value('.','varchar(max)'),1,2,''),'') 
    from @xml.nodes('/root/Row') as a(x);

Ide singkatnya:

Kami menggunakan XQuery pertama untuk membuat fragmen XML sederhana seperti ini

<x>, 00-001 London  Downing Street 1 /1</x>
<x>, 00-999 Yorkshire  Morning Street 1 /1</x>
<x>, 00-001 Washington  Pennsylvania Avenue 1/1</x>
<x>, 00-001 Washington  Pennsylvania Avenue 1/1</x>

Dengan ini kita dapat menggunakan XQuery kedua dan menempatkan distinct-values() di sana.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Menghubungkan Genero ke SQL Server

  2. Menggunakan variabel dalam pernyataan SEPERTI SQL

  3. Hapus semua karakter setelah spasi pertama di SSRS 2005

  4. T-SQL Menghapus semua baris dari tabel ketika subquery salah format

  5. Cara mengembalikan cadangan dengan nama yang berbeda