Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

groupBy datetime #10

Closed
Locke opened this issue Jun 19, 2013 · 5 comments
Closed

groupBy datetime #10

Locke opened this issue Jun 19, 2013 · 5 comments

Comments

@Locke
Copy link
Contributor

Locke commented Jun 19, 2013

Feature request: I want to count items grouped by a DATETIME field. To group on the day I am doing this in sqlite3:

SELECT COUNT(id) AS sum, strftime("%Y-%m-%d", date) AS year_month_date FROM myTable GROUP BY strftime("%Y-%m-%d", date);
@dresende
Copy link
Owner

Ignore the comment on the other side, I didn't see the ticket here.

@dresende
Copy link
Owner

You can do this already, it's just not that straightforward.

var query = require("sql-query");
var sql   = (new query.Query()).select();

sql.from("myTable")
   .count('id', 'sum')
   .fun('strftime', [ '%Y-%m-%d', 'date' ], 'year_month_date')
.groupBy('year_month_date');

console.log(sql.build());
// SELECT COUNT(`id`) AS `sum`, STRFTIME(`%Y-%m-%d`, `date`) AS `year_month_date` FROM `myTable` GROUP BY `year_month_date`

@dresende
Copy link
Owner

Wait, there's a problem in that query... the string format is escaped as an identifier, I'm going to try and fix this.

@dresende
Copy link
Owner

Every string passed to functions is supposed to be an identifier. What do you think about creating a special object? Example:

var query = require("sql-query");
var sql   = (new query.Query()).select();

sql.from("myTable")
   .count('id', 'sum')
   .fun('strftime', [ query.Text('%Y-%m-%d'), 'date' ], 'year_month_date')
.groupBy('year_month_date');

console.log(sql.build());
// SELECT COUNT(`id`) AS `sum`, STRFTIME('%Y-%m-%d', `date`) AS `year_month_date` FROM `myTable` GROUP BY `year_month_date`

@Locke
Copy link
Contributor Author

Locke commented Jun 20, 2013

I think this could be a good way, but I leave that to you as I want to use this functionallity in node-orm2 (see referenced ticket above).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants