1

我有一个大问题,我一直在努力解决。阅读较早的类似问题并没有取得成果。

我的表有 [id]、[parentid]、[data1]、[data2] 等列。在某些情况下,只有一条记录具有相同的 [parentid],而其他记录具有 2-10。

我想按 [parentid] 分组并打印出具有最大 [id] 的记录的所有数据。因此,我只会获得共享相同 [parentid] 编号的记录的所有“最新”详细信息。

我希望这是一个可以理解的目标:)。

我还尝试在 Crystal Reports XI 和 Qlikview 11 中解决这个问题,但没有取得重大成功。

提前感谢您的帮助!

4

3 回答 3

1

A solution with would be:

Script:

Table:
Load id, 
     parentid, 
     d1, 
     d2
inline
[
  id, parentid, d1, d2
  1, 0,   Hep, 01-04-2010
  2, 1,   Hap, 09-04-2010
  3, 1,   Hup, 10-10-2012
  4, 2,   Bep, 01-12-2009
  5, 4,   Ceg, 02-10-2010
  6, 4,   Pen, 05-10-2009
  7, 4,   Heg, 01-10-2009
  8, 4,   Ran, 08-01-2010
];

Then I added the fields id and parentid to the dashboard. To calulate the results use a table diagram where parentid is the dimension. Add a formula

=max(id)

and name it 'max(id)'

Then you get the following result:

enter image description here

于 2013-09-26T07:38:24.160 回答
1
select * from your_table
where id in 
(
  select max(id)
  from your_table
  group by parentid
)
于 2013-09-25T14:41:12.250 回答
1

您的 ID 列中的值可以重复使用吗?如果不是,那么 juergen 的答案将起作用。如果它们可以重复使用,您将需要在查询中使用同一个表两次,一次获取每个父 ID 的最大 ID,一次获取每个最大 ID/父 ID 的行。像这样:

select
t1.*
from aTable t1,
(select parentid, max(id) as id
 from aTable group by parentid) t2
where t1.id = t2.id
and t1.parentid = t2.parentid

SQLFiddle!

于 2013-09-25T15:29:33.443 回答