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

SQL Group BY, Item N Teratas untuk setiap Grup

;WITH s AS 
(
  SELECT StoreID, UPCCode, tds, rn = ROW_NUMBER()
  OVER (PARTITION BY StoreID ORDER BY tds DESC)
  FROM 
  (
    SELECT StoreID, UPCCode, tds = SUM(TotalDollarSales)
    FROM Sales
    GROUP BY StoreID, UPCCode
  ) AS s2
)
SELECT StoreID, UPCCode, TotalDollarSales = tds
FROM s
WHERE rn <= 5
ORDER BY StoreID, TotalDollarSales 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. Pengkodean karakter default SQL Server

  2. Apakah ada perbedaan kinerja antara CTE , Sub-Query, Tabel Sementara atau Variabel Tabel?

  3. Alternatif SQL Server Management Studio untuk menelusuri/mengedit tabel dan menjalankan kueri

  4. Pembaruan SQL permintaan baris top1

  5. SQL Server:drop table cascade setara?