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

TSQL untuk mendapatkan catatan DISTINCT yang dipesan dan kemudian menerapkan nomor baris

Saya pikir inilah yang Anda cari:Anda ingin semua baris dengan nomor baris yang bukan Tanggal Aktivasi maksimum

DECLARE @tbl TABLE
(
    CustomerName    VARCHAR(20),
    ProductName     VARCHAR(20),
    KeyCode         INT,
    ActivationDate  DATETIME
)


INSERT INTO @tbl
SELECT 'cmp1', 'game', 28734, GETDATE() -1    UNION ALL
SELECT 'cmp1', 'game', 28734, GETDATE() -1.5  UNION ALL
SELECT 'cmp1', 'game', 28734, GETDATE() -1.2  UNION ALL
SELECT 'cmp1', 'game', 28734, GETDATE() -1.8  UNION ALL
SELECT 'cmp1', 'game', 28734, GETDATE()       UNION ALL
SELECT 'cmp1', 'game', 28734, GETDATE() -17   UNION ALL
SELECT 'cmp2', 'game', 28736, GETDATE() -1    UNION ALL
SELECT 'cmp2', 'game', 28736, GETDATE() -1.5  UNION ALL
SELECT 'cmp2', 'game', 28736, GETDATE() -1.2  UNION ALL
SELECT 'cmp2', 'game', 28736, GETDATE() -1.8  UNION ALL
SELECT 'cmp2', 'game', 28736, GETDATE()       UNION ALL
SELECT 'cmp2', 'game', 28736, GETDATE() -17     


SELECT        ROW_NUMBER() OVER(ORDER BY ActivationDate DESC) RowNumber,
              CustomerName, 
              ProductName, 
              KeyCode, 
              ActivationDate        
FROM @tbl workTable
WHERE ActivationDate != 
    (
        SELECT MAX(ActivationDate)
        FROM @tbl checkTable
        WHERE workTable.CustomerName = checkTable.CustomerName
            AND workTable.ProductName = checkTable.ProductName
            AND workTable.KeyCode = checkTable.KeyCode
    )
RowNumber     CustomerName     ProductName     KeyCode  ActivationDate
1             cmp1             game            28734    2011-02-24 08:40:45.790
2             cmp2             game            28736    2011-02-24 08:40:45.790
3             cmp2             game            28736    2011-02-24 03:52:45.793
4             cmp1             game            28734    2011-02-24 03:52:45.793
5             cmp1             game            28734    2011-02-23 20:40:45.790
6             cmp2             game            28736    2011-02-23 20:40:45.790
7             cmp2             game            28736    2011-02-23 13:28:45.790
8             cmp1             game            28734    2011-02-23 13:28:45.790
9             cmp1             game            28734    2011-02-08 08:40:45.790
10            cmp2             game            28736    2011-02-08 08:40:45.790

Jika Anda ingin lebih dari satu...

SELECT  ROW_NUMBER() OVER(ORDER BY ActivationDate DESC) RowNumber,
        CustomerName, 
        ProductName, 
        KeyCode, 
        ActivationDate      
FROM @tbl workTable
WHERE NOT ActivationDate IN 
    (
        SELECT TOP 2 ActivationDate
        FROM @tbl checkTable
        WHERE workTable.CustomerName = checkTable.CustomerName
            AND workTable.ProductName = checkTable.ProductName
            AND workTable.KeyCode = checkTable.KeyCode
        ORDER BY ActivationDate DESC
    )


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Eksekusi kueri SSRS gagal untuk kumpulan data

  2. Bagaimana cara mendapatkan Waktu dari format DateTime dalam SQL?

  3. Pindahkan satu nilai baris ke yang lain dalam SQL

  4. Cara mendapatkan baris karyawan dengan bayaran tertinggi

  5. Pernyataan SQL UPDATE untuk mengganti dua nilai dalam dua baris