我在Union all
收到语法错误。我知道我不能这样做。但有人可以帮助我。
SELECT
ID,
date1,
date2 row_number() OVER (PARTITION BY ID ORDER BY date1 DESC ) as RN1,
1 as Range
FROM
(
SELECT ID,date1,
rn = row_number() OVER (PARTITION BY ID ORDER BY date1 )
FROM listing_History (nolock)
WHERE [date1] <= CONVERT(DATE,DATEADD(MONTH, -6, GETDATE())) AND
ID in (SELECT txt FROM [dbo].[fn_ListToTable](@listStr, ','))
) AS A ORDER BY date1 DESC, date2 DESC
UNION ALL
SELECT
ID,
Date1,
date2 row_number() OVER (PARTITION BY ID ORDER BY date1 DESC ) as RN1,
1 as Range
FROM
(
SELECT
ID,
date1,
rn = row_number() OVER (PARTITION BY ID ORDER BY date1 )
FROM listing_History (nolock)
WHERE [status_date] <= CONVERT(DATE,DATEADD(MONTH, -3, GETDATE()) -1)
AND
ID in (SELECT txt FROM [dbo].[fn_ListToTable](@listStr, ','))
) AS A ORDER BY date1 DESC,date2 desc
答案 0 :(得分:1)
最后选择Order by
和UNION
UNION ALL
SELECT
ID,
date1,
date2 row_number() OVER (PARTITION BY ID ORDER BY date1 DESC ) as RN1,
1 as Range
FROM
(
SELECT ID,date1,
rn = row_number() OVER (PARTITION BY ID ORDER BY date1 )
FROM listing_History (nolock)
WHERE [date1] <= CONVERT(DATE,DATEADD(MONTH, -6, GETDATE()))
AND
ID in (SELECT txt FROM [dbo].[fn_ListToTable](@listStr, ','))
) AS A --ORDER BY date1 DESC, date2 DESC Remove order by from here
UNION ALL
SELECT
ID,
Date1,
date2 row_number() OVER (PARTITION BY ID ORDER BY date1 DESC ) as RN1,
1 as Range
FROM
(
SELECT ID,date1,
rn = row_number() OVER (PARTITION BY ID ORDER BY date1 )
FROM listing_History with(nolock)
WHERE [status_date] <= CONVERT(DATE,DATEADD(MONTH, -6, GETDATE()))
AND
ID in (SELECT txt FROM [dbo].[fn_ListToTable](@listStr, ','))
)AS A
ORDER BY date1 DESC, date2 DESC
答案 1 :(得分:1)
从最后一个ORDER BY
中移除所有UNION
,并确保您在ORDER BY
中引用的列名与第一个SELECT
对应。