Skip to content

How to do union/unionAll with order for different queries? #586

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

Closed
ktalebian opened this issue Feb 13, 2023 · 3 comments · Fixed by #591
Closed

How to do union/unionAll with order for different queries? #586

ktalebian opened this issue Feb 13, 2023 · 3 comments · Fixed by #591

Comments

@ktalebian
Copy link

I can do the following:

 SqlBuilder.select(columName)
            .from(tableName)
            .where(userId, isEqualTo(id))
            .and(type, isEqualTo("type1"))
            .unionAll()
            .select(columnName)
            .from(tableName)
            .where(userId, isEqualTo(id))
            .and(type, isEqualTo("type2"));

However, I need to do an orderBy and a limit on each queries individually. If I add a .orderBy().limit() then I cannot do a .unionAll

How do I achieve this then?

@jeffgbutler
Copy link
Member

If I understand your request properly, then I don't think that would be valid SQL. I've run a couple of tests to confirm with HSQLDB. For example, this causes a syntax error in HSQLDB:

select * from AnimalData where id < 10 limit 5
union all
select * from AnimalData where id > 20 limit 3

If I've misunderstood your request, please show an example of SQL that works and let me know what database you are using.

@ktalebian
Copy link
Author

ktalebian commented Feb 17, 2023

What you have is not a valid query, but this would be a valid query:

(select * from AnimalData where id < 10 limit 5)
union all
(select * from AnimalData where id > 20 limit 3);

We are currently using that query in the XML file - trying to replace it with dynamic SQL

@jeffgbutler
Copy link
Member

Got it. I think I can make this happen.

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

Successfully merging a pull request may close this issue.

2 participants