sql >> Databasteknik >  >> RDS >> Sqlserver

SQL Group BY, Top N artiklar för varje grupp

;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. Använda Oracle JDeveloper med MySQL Database Service på Oracle Cloud Platform, del 2

  2. Skapa en Excel-fil (.xlsx) med PL/SQL

  3. Hur man optimerar MySQL-prestanda med MySQLTuner

  4. Hur ROUND() fungerar i MariaDB