Поместите его в подзапрос:
SELECT *
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY column1) AS rowid,
hrl.column0,
(SELECT top 1 bus from hc WHERE column2 = hc.column2) AS c,
'Site' as Based,
column1, column2, column3, column4,
CASE WHEN status = '1' Then 'Active'
WHEN status = '2' Then 'Canceled'
ELSE
'Unknown'
END as column5,
column6,
(select count(*) from hrl where column1 = hrl.column1) as [cnt]
FROM hrl
INNER JOIN hc on hc.column2 = hrl.column2
INNER JOIN rsc on rsc.column3 = hrl.column3
LEFT OUTER JOIN gmt on gmt.[column7] = hrl.[column7]
where bus LIKE '%test%'
) sq
WHERE rowid >= 1 AND rowid <= 10
ORDER BY [cnt] DESC
Если вы хотите, чтобы TOP 10 column1
строк упорядочивалось тогда по [cnt]
, вы можете использовать:
SELECT *
FROM (
SELECT TOP 10
hrl.column0,
(SELECT top 1 bus from hc WHERE column2 = hc.column2) AS c,
'Site' as Based,
column1, column2, column3, column4,
CASE WHEN status = '1' Then 'Active'
WHEN status = '2' Then 'Canceled'
ELSE 'Unknown'
END as column5,
column6,
(select count(*) from hrl where column1 = hrl.column1) as [cnt]
FROM hrl
INNER JOIN hc on hc.column2 = hrl.column2
INNER JOIN rsc on rsc.column3 = hrl.column3
LEFT OUTER JOIN gmt on gmt.[column7] = hrl.[column7]
WHERE bus LIKE '%test%'
ORDER BY column1
) sq
ORDER BY [cnt] DESC
Или, если вам просто нужно ORDER BY [cnt] DESC
, попробуйте это:
SELECT TOP 10
hrl.column0,
(SELECT top 1 bus from hc WHERE column2 = hc.column2) AS c,
'Site' as Based,
column1, column2, column3, column4,
CASE WHEN status = '1' Then 'Active'
WHEN status = '2' Then 'Canceled'
ELSE 'Unknown'
END as column5,
column6,
(select count(*) from hrl where column1 = hrl.column1) as [cnt]
FROM hrl
INNER JOIN hc on hc.column2 = hrl.column2
INNER JOIN rsc on rsc.column3 = hrl.column3
LEFT OUTER JOIN gmt on gmt.[column7] = hrl.[column7]
WHERE bus LIKE '%test%'
ORDER BY [cnt] DESC