I have a script that returns results that are including the RN = 1 column, So all rows have this value 1 with column name RN.
If I try to change select * to select my as 'column_name' I get strange results where the column names are in all the returned fields.
So how do exclude the rn=1 column?
--Wrong SQL Edition (Dairy XL)
with cte as
(
SELECT
--damstaticfarmdata.sfdid,
--TskOsAndSqlCheck.toasId,
-- TskFarmBasic.FrmCountry as 'Region',
-- damstaticfarmdata.sfdlelycentercode as 'Lely Center Code',
LelyCenter.LceName as 'Lely Center Name',
damstaticfarmdata.sfdcustomermovexcode as 'Customer Movex Code',
damstaticfarmdata.sfdfarmname as 'Farm Name',
--damstaticfarmdata.sfdfarmlicensetypeid as 'License Type',
-- CASE
-- WHEN sfdfarmlicensetypeid = 3 then 'Farm'
-- WHEN sfdfarmlicensetypeid = 4 then 'Farm No Maintenance'
-- END as 'License Type',
-- damstaticfarmdata.sfdcurrentversion as 'Current Version',
--TskFarmBasic.FrmCurrentVersion,
TskOsAndSqlCheck.toasOsInfo as 'Windows Version',
TskOsAndSqlCheck.toasSqlVersion as 'SQL Version',
TskOsAndSqlCheck.toasSqlDatabaseSizeInMB 'Database Size (MB)',
damstaticfarmdata.sfdnrrobots as 'Nr of Robots',
-- damstaticfarmdata.sfdfarmlicense as 'Horizon/T4C Product Key',
--TskFarmBasic.FrmFarmLicense,
TskOsAndSqlCheck.toasTime as 'BM Last Upload Time',
ROW_NUMBER() OVER (PARTITION BY sfdcustomermovexcode ORDER BY toasTime DESC) AS rn
FROM LelyCenter INNER JOIN
damstaticfarmdata ON LelyCenter.LceMovexCode = damstaticfarmdata.sfdlelycentercode INNER JOIN
TskFarmBasic ON damstaticfarmdata.sfdcustomermovexcode = TskFarmBasic.FrmCustomerMovexCode INNER JOIN
TskOsAndSqlCheck ON TskFarmBasic.FrmId = TskOsAndSqlCheck.toasFrmId
Where FrmCountry in ('CA', 'US') and sfdnrrobots > 7 and toasSqlVersion like '%Express%' and toasTime > '2023-01-01'
)
select
*
from cte where rn = 1
Order by 'Lely Center Name' asc, 'Farm Name' asc
If I try this
select 'Lely Center Name','Customer Movex Code','Farm Name','Windows Version','SQL Version','Database Size (MB)','Nr of Robots','BM Last Upload Time'
I get this
Msg 408, Level 16, State 1, Line 41 A constant expression was encountered in the ORDER BY list, position 1.