-
Notifications
You must be signed in to change notification settings - Fork 80
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
Case conditions #9
Comments
Hi, postgresql case condition currently is not supported. |
I didn't mean case conditions for a specific engine, is this something that can be achieved for other things? Is this something you guys are planning to add in the near future? |
If it's not for particular dialect, what exactly do you mean? Could you provide some examples? |
I mean a query like this -
Here's an example - This is supported by many databases (SQL Server, Oracle, Postgres, MySQL) |
I didn't know that's kind of general thing, so now we could try to find general solution for that... |
Is there a way we can modify the existing code to implement the Case statements.... |
As a workaround you can do that with
var builder = require('json-sql')();
var sql = builder.build({
type: 'select',
expression: {
pattern: 'case when {field} = {conditionValue} then {thenValue} else {elseValue} end',
values: {
field: {field: 'a'},
conditionValue: 1,
thenValue: 1,
elseValue: 0
}
},
alias: 'col1'
});
console.log('query:', sql.query)
// query: select * from case when "a" = 1 then 1 else 0 end as "col1"; Example above generates not valid query, there should be row-generator expression.
var builder = require('json-sql')();
var sql = builder.build({
type: 'select',
fields: [{
expression: {
pattern: 'case when {field} = {conditionValue} then {thenValue} else {elseValue} end',
values: {
field: {field: 'a'},
conditionValue: 1,
thenValue: 1,
elseValue: 0
}
},
alias: 'col1'
}],
table: 't'
});
console.log('query:', sql.query)
// query: select case when "a" = 1 then 1 else 0 end as "col1" from "t"; |
For more information take a look at the docs |
Can we have
|
Hey, I couldn't figure a way to do this - is there a way to have case conditions (case when x then ... etc) in a select query?
Thanks
The text was updated successfully, but these errors were encountered: