我想拉除除Oracle SQL中最后一行之外的所有行
我的数据库是这样的
Prikey - Auto_increment
common - varchar
miles - int
所以我想总结除了按公钥分组的主键排序的最后一行之外的所有行。这意味着对于每个不同的共同点,里程将被加总(除了最后一个)
答案 0 :(得分:5)
注意:此答案发布后问题已更改。前两个查询适用于原始问题。最后一个查询(在附录中)适用于更新的问题。
这应该可以解决问题,尽管对于较大的表来说它会有点慢:
SELECT prikey, authnum FROM myTable
WHERE prikey <> (SELECT MAX(prikey) FROM myTable)
ORDER BY prikey
此查询较长,但对于较大的表,它应该更快。我会留给你决定:
SELECT * FROM (
SELECT
prikey,
authnum,
ROW_NUMBER() OVER (ORDER BY prikey DESC) AS RowRank
FROM myTable)
WHERE RowRank <> 1
ORDER BY prikey
附录该问题有更新;这是更新的答案。
SELECT
common,
SUM(miles)
FROM (
SELECT
common,
miles,
ROW_NUMBER() OVER (PARTITION BY common ORDER BY prikey DESC) AS RowRank
FROM myTable
)
WHERE RowRank <> 1
GROUP BY common
答案 1 :(得分:0)
看起来我有点太晚了,但这是我的贡献,类似于Ed Gibbs的第一个解决方案,但不是计算表中每个值的最大ID,然后比较我使用内联视图得到它一次。 / p>
SELECT d1.prikey, d1.authnum FROM myTable d1, (SELECT MAX(prikey) prikey myTable FROM myTable) d2 WHERE d1.prikey != d2.prikey
至少我认为如果你不想使用Google Analytics,这会更有效率。
答案 2 :(得分:0)
查询以检索表中除第一行和最后一行之外的所有记录
select * from table_name
where primary_id_column not in
(
select top 1 * from table_name order by primary_id_column asc
)
and
primary_id_column not in
(
select top 1 * from table_name order by primary_id_column desc
)