My table is something like this:
_id time name isNotFirstEntry 1 55555 aaa 0 2 66666 1 3 77777 1 4 88888 bbb 0 5 99998 4 6 99999 4
ie for duplicate entries, name
is null and the _id
of the original entry is stored in isNotFirstEntry.
I want do retrieve _id, time, name, no. of duplicate entries as n where name!=null
I tried something like this: select _id, time, name, (select count(isNotFirstEntry) from mytable where isNotFirstEntry=_id) as n from mytable where name!=null
but of course that doesnt work. The _id in the nested query is not the same as that of the outside part. I need to access _id of the outside part from the nested query.
Im using android sdk, so i can use sqlite only.
You need to do a join. You can do this as a correlated subquery:
select mt._id, mt.time, mt.name,
(select count(mt2.isNotFirstEntry)
from mytable mt2
where mt2.isNotFirstEntry=mt._id
) as n
from mytable mt
where mt.name!=null
Or as an explicit join with group by:
select mt._id, mt.time, mt.name, count(mt2._id) as n
from mytable mt left outer join
mytable mt2
on mt._id = mt2.isNotFirstEntry
where mt.name is not null
group by mt._id, mt.time, mt.name
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.