lf.fn.distinct usage issue

105 views
Skip to first unread message

Abishek Anand

unread,
Dec 10, 2015, 7:52:50 AM12/10/15
to lovefield-users
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Minimal example of using Lovefield</title>
<script src="../components/lovefield/dist/lovefield.min.js"></script>
</head>
<body>
<script>

var schemaBuilder = lf.schema.create('todo', 1);

schemaBuilder.createTable('Item').
addColumn('id', lf.Type.INTEGER).
addColumn('description', lf.Type.STRING).
addColumn('deadline', lf.Type.DATE_TIME).
addColumn('done', lf.Type.BOOLEAN).
addPrimaryKey(['id']).
addIndex('idxDeadline', ['deadline'], false, lf.Order.DESC);

var todoDb;
var item;
schemaBuilder.connect().then(function(db) {
todoDb = db;
item = db.getSchema().table('Item');
var row = item.createRow({
'id': 1,
'description': 'Get a cup of coffee',
'deadline': new Date(),
'done': false
});

return db.insertOrReplace().into(item).values([row]).exec();
}).then(function() {
return todoDb.select(lf.fn.distinct(item.id, item.description, item.deadline, item.done)).from(item).where(item.done.eq(false)).exec();
}).then(function(results) {
results.forEach(function(row) {
console.log(row);
});
});

</script>
</body>
</html>

I have modified the basic example a little to select every individual column in the lf.fn.distinct function. The function appears to be returning only the values in the first column passed into the lf.fn.distinct function(item.id) and not the other columns. Is there a way to use distinct that will return all the column values?

Arthur Hsu

unread,
Dec 10, 2015, 1:55:06 PM12/10/15
to lovefield-users
lf.fn.distinct takes only one column at a time


So that's why only the first column passed in is honored.

Abishek Anand

unread,
Dec 10, 2015, 2:04:34 PM12/10/15
to lovefield-users
So is there a way to execute the equivalent of 

SELECT DISTINCT(p.id, a.id, a.name)
  FROM photo p
  LEFT OUTER JOIN album a
    ON p.albumId = a.id
?

dpa...@chromium.org

unread,
Dec 10, 2015, 9:31:58 PM12/10/15
to lovefield-users
Do you need to do an outer join instead of a normal (inner join) here? If you can do inner join, then you can do the following.

db.select(p.id, a.id, a.name).
 
from(p, a).
 
where(p.albumId.eq(a.id)).
  groupBy
(p.id, a.id, a.name).
 
exec();
Reply all
Reply to author
Forward
0 new messages