SQL 中的最后一行
Last row in SQL
我有这样的查询
select top 1* from table1 where organizationID= 79 order by D_Index desc
select top 1* from table1 where organizationID= 87 order by D_Index desc
select top 1* from table1 where organizationID= 19 order by D_Index desc
我在这里尝试获取 table 1 中针对 organizationID 的最后数据 .. 表示 79,87 和 19 的最后数据,并尝试与 union all 结合,但这显示错误
select top 1* from table1 where organizationID= 79 order by D_Index desc union all
select top 1* from table1 where organizationID= 87 order by D_Index desc union all
select top 1* from table1 where organizationID= 19 order by D_Index desc
如果我写这个
select top 1* from table1 where organizationID= 79 union all
select top 1* from table1 where organizationID= 87 union all
select top 1* from table1 where organizationID= 19
order by D_Index desc
然后这显示第 1 行 79 和 87 以及最后一行 19 但我想要最后一行反对 79,87 和 19
有什么帮助吗?
;WITH CTE as
(
SELECT
*,
row_number() over (partition by organizationID order by D_Index desc) rn
FROM
table1
WHERE organizationID in (19,79,87)
)
SELECT *
FROM CTE
WHERE rn = 1
没有 CTE(按要求)
SELECT *
FROM
(
SELECT
*,
row_number() over (partition by organizationID order by D_Index desc) rn
FROM
table1
WHERE organizationID in (19,79,87)
) x
WHERE rn = 1
@t-clausen.dk 的回答是去这里的方法,但这里有一种方法可以使您当前的方法起作用:
SELECT * FROM
(SELECT TOP 1 * FROM table1 WHERE organizationID = 79 ORDER BY D_Index DESC) t1
UNION ALL
SELECT * FROM
(SELECT TOP 1 * FROM table1 WHERE organizationID = 87 ORDER BY D_Index DESC) t2
UNION ALL
SELECT * FROM
(SELECT TOP 1 * FROM table1 WHERE organizationID = 19 ORDER BY D_Index DESC) t3;
发生错误是因为 ORDER BY
不能出现在联合查询(或任何查询)的中间。但是,如果我们将您的查询包装为子查询,那么我们可以对它们使用 ORDER BY
)。
同样,使用 ROW_NUMBER
是前往此处的方法,但如果您需要使用此类语法,只要编写正确,是可能的。
我有这样的查询
select top 1* from table1 where organizationID= 79 order by D_Index desc
select top 1* from table1 where organizationID= 87 order by D_Index desc
select top 1* from table1 where organizationID= 19 order by D_Index desc
我在这里尝试获取 table 1 中针对 organizationID 的最后数据 .. 表示 79,87 和 19 的最后数据,并尝试与 union all 结合,但这显示错误
select top 1* from table1 where organizationID= 79 order by D_Index desc union all
select top 1* from table1 where organizationID= 87 order by D_Index desc union all
select top 1* from table1 where organizationID= 19 order by D_Index desc
如果我写这个
select top 1* from table1 where organizationID= 79 union all
select top 1* from table1 where organizationID= 87 union all
select top 1* from table1 where organizationID= 19
order by D_Index desc
然后这显示第 1 行 79 和 87 以及最后一行 19 但我想要最后一行反对 79,87 和 19
有什么帮助吗?
;WITH CTE as
(
SELECT
*,
row_number() over (partition by organizationID order by D_Index desc) rn
FROM
table1
WHERE organizationID in (19,79,87)
)
SELECT *
FROM CTE
WHERE rn = 1
没有 CTE(按要求)
SELECT *
FROM
(
SELECT
*,
row_number() over (partition by organizationID order by D_Index desc) rn
FROM
table1
WHERE organizationID in (19,79,87)
) x
WHERE rn = 1
@t-clausen.dk 的回答是去这里的方法,但这里有一种方法可以使您当前的方法起作用:
SELECT * FROM
(SELECT TOP 1 * FROM table1 WHERE organizationID = 79 ORDER BY D_Index DESC) t1
UNION ALL
SELECT * FROM
(SELECT TOP 1 * FROM table1 WHERE organizationID = 87 ORDER BY D_Index DESC) t2
UNION ALL
SELECT * FROM
(SELECT TOP 1 * FROM table1 WHERE organizationID = 19 ORDER BY D_Index DESC) t3;
发生错误是因为 ORDER BY
不能出现在联合查询(或任何查询)的中间。但是,如果我们将您的查询包装为子查询,那么我们可以对它们使用 ORDER BY
)。
同样,使用 ROW_NUMBER
是前往此处的方法,但如果您需要使用此类语法,只要编写正确,是可能的。