В SQL Server 2012, используя оконную функцию ROW_NUMBER()
, вы можете достичь этого.Создайте ROW_NUMBER()
для установки RowNum
для каждой строки и на основе результата, используя UNION ALL
, он вернет результат:
;WITH CTE AS (
SELECT id, location, [date], Total_Sales,
ROW_NUMBER() OVER (ORDER BY Id) AS RowNum
FROM TableName
)
SELECT CAST(id AS VARCHAR(30)) AS Id,
location,
CAST([date] AS VARCHAR(30)) AS [date],
CAST(Total_Sales AS VARCHAR(30)) AS Total_Sales
FROM CTE
WHERE RowNum BETWEEN 1 AND 10
UNION
SELECT '11-20 ids' AS id,
'11-20 AS locations' as location,
'11-20 dates' AS [date],
SUM(Total_Sales) AS Total_Sales
FROM CTE
WHERE RowNum BETWEEN 11 AND 20
Демонстрация на db <> fiddle