SSMS
 sql >> Teknologi Basis Data >  >> Database Tools >> SSMS

Bagaimana cara mengambil data dari SQL Server berdasarkan contoh di bawah ini?

Mungkin Anda bisa menggunakan solusi seperti di bawah iniLihat demo kerja

declare @d date='2018-Jun-03'

; with Indexer as 
(
    select 
        *, 
        rn= row_number() over(partition by CustName order by RecordedTime),
        rn2=row_number() over(partition by CustName order by RecordedTime desc)
    from records
)
,GetValidCustomerRecords as
(
    select 
        CustName,
        Country,
        RecordedTime,
        Audit   = case when cast(RecordedTime as date)[email protected] and rn=1 then 'add' else 'change' end,
        History = case 
                    when cast(RecordedTime as date)[email protected] and rn=1 
                    then 'new' 
                    when cast(RecordedTime as date)<@d and rn=1 
                    then 'before'
                    else 'current' end
    from Indexer i 
    where CustName in
    (
    select 
        distinct CustName 
    from records
    where cast(RecordedTime as date)[email protected]
    ) 
    and (rn=1 or rn2=1) and cast(RecordedTime as date)<[email protected]
)

select * from GetValidCustomerRecords
order by CustName, RecordedTime



  1. DBeaver
  2.   
  3. phpMyAdmin
  4.   
  5. Navicat
  6.   
  7. SSMS
  8.   
  9. MySQL Workbench
  10.   
  11. SQLyog
  1. pulihkan tab setelah membuka kembali SQL Server Management Studio

  2. Buat skrip sisipan dari hasil yang dipilih

  3. Database Default SQL Server saat Kueri - master

  4. Perbarui bidang bit sql dalam database

  5. Bagaimana cara menjatuhkan semua tabel dalam database SQL Server?