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

Make get_resource_sql() do a better job of tidying the SQL text #21

Open
Moohan opened this issue Jan 15, 2024 · 1 comment
Open

Make get_resource_sql() do a better job of tidying the SQL text #21

Moohan opened this issue Jan 15, 2024 · 1 comment

Comments

@Moohan
Copy link
Member

Moohan commented Jan 15, 2024

I used get_resource_sql() to return some populations with the HB name, I used it because it allows me to do a join and therefore get all the data I needed as 1 API call, this is faster and was important because it was in a Shiny dashboard.

I managed to get the query to work eventually but there were a few quirks which could probably be handled internally by the function (or at the very least included in the documentation) so that users don't have to figure it out by trial and error.

hb_pop_est <- phsopendata::get_resource_sql(
    sql = paste(
      "SELECT%20pops.\"Year\", pops.\"HB\", lookup.\"HBName\", pops.\"AllAges\"",
      "FROM \"27a72cc8-d6d8-430c-8b4f-3109a9ceadb1\" pops",
      "JOIN \"652ff726-e676-4a20-abda-435b98dd7bdc\" lookup",
      "ON%20pops.\"HB\" = lookup.\"HB\"",
      "WHERE%20pops.\"Sex\" = 'All' AND%20pops.\"Year\" > 2006",
      sep = "%20"
    )
  )

From memory, I think the 'rules' are:

  • Variable names must be quoted (which likely means escaping quotes) \"
  • Need a space (but encoded as %20) before an after every SQL keyword unless the next bit contains quotes.
@Moohan
Copy link
Member Author

Moohan commented Sep 25, 2024

Also make sure to add this to documentation.

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

1 participant