Skip to content
Advertisement

MS-Access Query to PostgreSQL View

I am converting a microsoft access query into a postgresql view. The query has obvious components that I have found reasonable answers to. However, I am still stuck on getting the final result:

Realizing I need to use CASE SUM WHEN, here is what I have worked out so far:

The goal is to count the number of instances in which the Sec_ID has the following:

  • has (Type = LODE and Status = Active) = SUM integer
  • has (Type = LODE and Loc_Date between 8/31/2017 and 9/1/2018) = SUM Integer

My primary issue is getting a SUM integer to populate in the new columns

Advertisement

Answer

Case expressions are the equivalent to the Access IIF() functions, but WHEN isn’t a function so it isn’t used by passing a set of parameters. Think of it as being a tiny where clause instead, it evaluates one or more predicates to determine what to do, and the action taken is established by what you specify after THEN

By the way, you should NOT be relying on MM/DD/YYYY as dates in Postgres

nb: Aggregate functions ignore NULL, take this example:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement