-
-
Notifications
You must be signed in to change notification settings - Fork 325
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
The WITH clause #222
Comments
Hello. |
@neevek your example is perfect. I'd like to reproduce it with |
@fnc12 Hello, the table CREAT TABLE user_activity (
id integer primary key,
uid integer,
timestamp datetime
); The |
first of all I need to add column alias feature. I suppose it to have a syntax like this: // SELECT id userId
// FROM users
auto rows = storage.select(make_alias(&User::id, "userId")); that returns the same as // SELECT id
// FROM users
auto rows = storage.select(&User::id); |
please check out column aliases in #226 in |
It's great to see the progress update, but there's one thing, usage of custom alias, is too verbose to me, could it be possible to name and reference the alias with simple raw string, I am not sure if that would contradict with the design philosophy of the library, but that would make the query less verbose to some extent. |
@neevek I've thought about it. I was able to make syntax like this: auto rowsWithColumnAliases = storage.select(columns(as(&Employee::id, "EMP_ID"),
&Employee::name,
&Employee::age,
&Department::dept),
where(is_equal(alias("EMP_ID"), &Department::empId))); but this options has string repeats which makes API vulnerable for literal errors. One of the main advantages of the lib is a static linkage with all query elements so you cannot make any literal misspelling errors. auto myAlias = make_alias(&Employee::id, EMP_ID");
auto rowsWithColumnAliases = storage.select(columns(myAlias.as(),
&Employee::name,
&Employee::age,
&Department::dept),
where(is_equal(myAlias.get(), &Department::empId))); This option is not bad but requires two statements for one query. So I decided to move every string to a dedicated class and users can be sure that there can not be any misspelling mistake. Probably I forgot something and there is better way to perform aliases for |
@fnc12 I am OK with option one. I am no professional on this issue, one thing I can think of to avoid spelling errors is to perform string literal comparison at compile time when referencing aliases, if an alias doesn't exist, referencing it yields compile time error. |
|
I'm trying to reproduce your initial query. Looks like you have an error here: SELECT
uid, MIN(DATE(user_activity.timestamp)) register_date
FROM user_activity
GROUP BY uid
HAVING register_date >= DATE('now', '-7 days')
CREAT TABLE user_activity (
id integer primary key,
uid integer,
timestamp datetime
); So I guess you wanted to say |
What's the error? I cannot run SQL on phone now. I added a -- edit |
Anyway I'm still working on adding |
I totally understand that, but I needed to get the work done immediately, and that was the easiest and not so invasive way, that's why I didn't send pull request. I am expecting the with clause, thanks for the hard work. |
@neevek it's ok. This lib is totally open source and has open license so you're free to modify it as you want. First of all |
I've added |
I've added |
@fnc12 Anyways, I just wanted to pop in and say thanks for the great library! In case you were wondering, I wanted to use the Something like this: WITH q AS
(
SELECT *
FROM TASKS
WHERE PARENT_ID = 7
UNION ALL
SELECT m.*
FROM TASKS m
JOIN q
ON m.PARENT_ID = q.ID
)
SELECT *
FROM q
ORDER by q.PARENT_ID with a table like this: CREATE TABLE 'TASKS' ( 'ID' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 'DESCRIPTION' TEXT NOT NULL , 'STATUS' INTEGER NOT NULL , 'MODIFICATION' TEXT NOT NULL , PARENT_ID INTEGER) |
@thejinx0r it has not been implemented. It doesn't exist in TODO list but it doesn't meant that it will not be implemented cause issue list is a TODO list #2. |
* Shows yet another subquery hoisted into a CTE as required in issue fnc12#222
@neevek @thejinx0r Perhaps you would like to try https://github.com/FireDaemon/sqlite_orm/tree/CTEs, for which there's also a draft PR. There is an example there specifically for @neevek's query, along with a few others. I didn't have time to create good test data, but the query should work as expected. |
This library has very cool API design, thank you @fnc12 for the hard work.
I am looking for the WITH clause support in
sqlite_orm
so I can put subqueries aside and reuse them in my main SELECT statement. What I am trying to do is to calculate user retention of auser_activity
table as the following:with the following sample output:
I think queries of this complexity may not need to be constructed in the ORM way, raw SQL statements may be clearer and easier to understand in such cases.
The text was updated successfully, but these errors were encountered: