sql >> Database teknologi >  >> RDS >> Sqlserver

Hvordan får man det maksimale rækkenummer pr. gruppe/partition i SQL Server?

Prøv dette:

SELECT a.*, CASE WHEN totalPayments>1 THEN 'NO' ELSE 'YES' END IsFirstPayment
  FROM(
                SELECT  p.payment_id,     
                                p.user_id,     
                                ROW_NUMBER() OVER (PARTITION BY p.user_id ORDER BY p.payment_date DESC) AS paymentNumber,
                                SUM(1) OVER (PARTITION BY p.user_id) AS totalPayments
                    FROM payment p 
            ) a
WHERE   paymentNumber = 1       


  1. Sådan rollback dataframe.to_sql i python i SQLAlchemy?

  2. dublerede poster i SQL JOIN

  3. Sådan opdaterer du ID i tabel 2 fra tabel 1 i Oracle sql

  4. Sådan bruges CASE Statement i MySQL