我有一个这样的数据集,其中'Y'表示每个参考ID的主记录。
Query query = new Query();
DateTime dateTest = new DateTime(2014, 1, 1, 0, 0);
query.addCriteria(Criteria.where("monthYear").gte(dateTest.toDateTimeISO()));
System.out.println(query.toString());
List<Model> spendingsList = dbConfig.getMongoOperations().find(query, Model.class, collectionName);
http://sqlfiddle.com/#!9/f1198
如您所见,在某些情况下,没有指出主要记录。我想运行一个查询来突出显示这些行。
这是我试过的:
答案 0 :(得分:0)
SELECT DISTINCT x.*
FROM groups x
LEFT
JOIN groups y
ON y.referenceid = x.referenceid
AND y.isreferencerecord = 'y'
WHERE y.id IS NULL;
+----+-------+-------------+-------------------+-------------------------+
| id | Name | ReferenceID | IsReferenceRecord | GroupHasReferenceRecord |
+----+-------+-------------+-------------------+-------------------------+
| 7 | Pink | 3 | NULL | NULL |
| 8 | White | 3 | NULL | NULL |
+----+-------+-------------+-------------------+-------------------------+
sqlfiddle.com /#!9 / f1198 / 2
答案 1 :(得分:0)
不确定你是否希望你的桌子的所有原始记录带有Y / N标志......如果是这样的话,试试这个:
SELECT g1.id
,g1.Name
,g1.ReferenceID
,g1.IsReferenceRecord
,case when g1.Name = g2.Name then 'N'
else 'Y'
end as GroupHasReferenceRecord
FROM Groups as g1
LEFT JOIN (select *
from Groups
where ReferenceId not in (select distinct ReferenceID
from Groups
where IsReferenceRecord = 'Y'
)
) as g2
ON g1.Name=g2.Name