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

Solusi lain selain Cursoring

Saya tidak yakin Anda benar-benar membutuhkan kueri rekursif sebanyak tabel Numbers/Tally untuk mendapatkan nomor urut yang setara untuk nilai "CURRENT" seperti nilai "PROPOSED".

With 
    Numbers As 
    (
    Select Row_Number() Over ( Order By C1.object_id ) As Value
    From sys.columns As C1
        Cross Join sys.columns As C2
    )
    , ProposedSequences As
    (
    Select  NTRIId
        , Row_Number() Over ( Order By NTRIId ) As Sequence
    From Table
    Where NTRITCode = 'PROPOSED'
    Group By NTRIId
    )
    , CurrentSequences As
    (
    Select RID, NTRITCode, NTRIId, Parameter, Usage, Rate
        , Numbers.Value As Sequence
    From Table
        Cross Join Numbers
    Where NTRITCode = 'Current'
        And Numbers.Value <= (Select Max(Sequence) From ProposedSequence)
    )
Select Sequence, RID, NTRITCode, NTRIId, Parameter, Usage, Rate
From CurrentSequences
Union All
Select PS.Sequence, T.RID, T.NTRITCode, T.NTRIId, T.Parameter, T.Usage, T.Rate
From ProposedSequences As PS
    Join Table As T
        On T.NTRIId = PS.NTRIId
Order By PS.Sequence, T.NTRITCode


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. cara mengubah string menjadi kueri di sql server

  2. SQL Server 2008 Kolom yang ditentukan ditentukan beberapa kali

  3. Koreksi Rencana Otomatis di SQL Server

  4. Gunakan APP_NAME() untuk Mendapatkan Nama Aplikasi dari Sesi Saat Ini di SQL Server

  5. Microsoft Access vs SQL Server