{"action":"created","issue":{"url":"https://api.github.com/repos/Masterminds/squirrel/issues/148","repository_url":"https://api.github.com/repos/Masterminds/squirrel","labels_url":"https://api.github.com/repos/Masterminds/squirrel/issues/148/labels{/name}","comments_url":"https://api.github.com/repos/Masterminds/squirrel/issues/148/comments","events_url":"https://api.github.com/repos/Masterminds/squirrel/issues/148/events","html_url":"https://github.com/Masterminds/squirrel/issues/148","id":361378897,"node_id":"MDU6SXNzdWUzNjEzNzg4OTc=","number":148,"title":"Select with case and variables","user":{"login":"wayneashleyberry","id":727262,"node_id":"MDQ6VXNlcjcyNzI2Mg==","avatar_url":"https://avatars1.githubusercontent.com/u/727262?v=4","gravatar_id":"","url":"https://api.github.com/users/wayneashleyberry","html_url":"https://github.com/wayneashleyberry","followers_url":"https://api.github.com/users/wayneashleyberry/followers","following_url":"https://api.github.com/users/wayneashleyberry/following{/other_user}","gists_url":"https://api.github.com/users/wayneashleyberry/gists{/gist_id}","starred_url":"https://api.github.com/users/wayneashleyberry/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wayneashleyberry/subscriptions","organizations_url":"https://api.github.com/users/wayneashleyberry/orgs","repos_url":"https://api.github.com/users/wayneashleyberry/repos","events_url":"https://api.github.com/users/wayneashleyberry/events{/privacy}","received_events_url":"https://api.github.com/users/wayneashleyberry/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-09-18T16:09:07Z","updated_at":"2018-09-19T16:11:46Z","closed_at":null,"author_association":"NONE","body":"I have a sql query with a `select case`, that contains dynamic variables. Would it be possible to express this with squirrel?\r\n\r\n```sql\r\nselect id, title, descriptor, text_color, image_url,\r\ncase\r\n    when available_age is not null\r\n    then date_sub(%s, interval (%d - available_age) day)\r\n    else available_timestamp\r\n```\r\n\r\nAt the moment I've had to just format the string separately and pass it into the `.Select()` method on the query builder:\r\n\r\n\r\n```go\r\ncolumns := fmt.Sprintf(`\r\n    id, title, descriptor, text_color, image_url,\r\n    case\r\n        when available_age is not null\r\n        then date_sub(%s, interval (%d - available_age) day)\r\n        else available_timestamp\r\n`, now.Format(datetime.StampSQL), age)\r\n\r\nquery, args, err := sq.\r\n    Select(columns).\r\n    From(\"story\").\r\n    ...\r\n```"},"comment":{"url":"https://api.github.com/repos/Masterminds/squirrel/issues/comments/422863260","html_url":"https://github.com/Masterminds/squirrel/issues/148#issuecomment-422863260","issue_url":"https://api.github.com/repos/Masterminds/squirrel/issues/148","id":422863260,"node_id":"MDEyOklzc3VlQ29tbWVudDQyMjg2MzI2MA==","user":{"login":"wayneashleyberry","id":727262,"node_id":"MDQ6VXNlcjcyNzI2Mg==","avatar_url":"https://avatars1.githubusercontent.com/u/727262?v=4","gravatar_id":"","url":"https://api.github.com/users/wayneashleyberry","html_url":"https://github.com/wayneashleyberry","followers_url":"https://api.github.com/users/wayneashleyberry/followers","following_url":"https://api.github.com/users/wayneashleyberry/following{/other_user}","gists_url":"https://api.github.com/users/wayneashleyberry/gists{/gist_id}","starred_url":"https://api.github.com/users/wayneashleyberry/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wayneashleyberry/subscriptions","organizations_url":"https://api.github.com/users/wayneashleyberry/orgs","repos_url":"https://api.github.com/users/wayneashleyberry/repos","events_url":"https://api.github.com/users/wayneashleyberry/events{/privacy}","received_events_url":"https://api.github.com/users/wayneashleyberry/received_events","type":"User","site_admin":false},"created_at":"2018-09-19T16:11:46Z","updated_at":"2018-09-19T16:11:46Z","author_association":"NONE","body":"Ah, that works! The `as` part of the `case` statement is missing though.\r\n\r\nraw sql:\r\n\r\n```sql\r\ncase\r\n  when available_age is not null\r\n  then date_sub(\"2018-09-19 17:06:49\", interval (10 - available_age) day)\r\n  else available_timestamp\r\nend as available_timestamp\r\n```\r\n\r\ngenerated sql:\r\n\r\n```sql\r\nCASE WHEN available_age is not null THEN date_sub(?, interval (? - available_age) day) ELSE available_timestamp END\r\n```\r\n\r\nUsing this code:\r\n\r\n```go\r\navailable := sq.Case().\r\n\t\tWhen(\r\n\t\t\t\"available_age is not null\",\r\n\t\t\tsq.Expr(\"date_sub(?, interval (? - available_age) day)\", now.Format(datetime.StampSQL), age),\r\n\t\t).\r\n\t\tElse(\"available_timestamp\")\r\n\r\nquery, args, err := sq.\r\n\t\tSelect(columns...).\r\n\t\tColumn(available).\r\n```"}}
{"actor":{"display_login":"wayneashleyberry"}}