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

Brug for hjælp til at finde den korrekte T-SQL-forespørgsel

;WITH cte AS
(
SELECT *,
        DATEDIFF(DAY,0,DateRequested)-
        ROW_NUMBER() OVER (PARTITION BY UserId ORDER BY DateRequested) AS Grp
FROM YourTable  
WHERE Approved = 1 /*Presumably - but your example data doesn't show this.*/
)
SELECT UserId, 
       MIN(DateRequested) AS DateFrom, 
       MAX(DateRequested) AS DateTo  
FROM cte 
GROUP BY UserId,Grp


  1. Gem array i SQL Server 2008

  2. COPY funktion i PostgreSQL

  3. Hvilken kolonne skal det klyngede indeks sættes på?

  4. Hibernate - Ingen egnet driver fundet til jdbc:mysql//*