Skip to main content

Queries that contain complex SQL

Scenario

There are several ways to query data that are not supported by report definitions. An example is the Haversine formula used at the FSG enterprise layer within the Booking application solution. The query is found in the Browse tab of the FSG-Data-Address HaversineFormula Connect-SQL rule.

haveshine
The haversine formula determines the great-circle distance between two points on a sphere given their latitude and longitudes. Important in navigation, it is a special case in spherical trigonometry, the law of haversines, that relates the sides and angles of spherical triangles.

Solution design

SELECT pyGUID AS "pyGUID",

Reference AS "Reference", IsFor AS "IsFor", Street AS "Street", City AS "City", State AS "State", PostalCode AS "PostalCode", Country AS "Country", Latitude AS "Latitude", Longitude AS "Longitude", Distance AS "Distance"

FROM

( SELECT z.pyguid AS pyGUID, z.reference AS Reference, z.isfor AS IsFor, z.street AS Street, z.city AS City, z.state AS State, z.postalcode AS PostalCode, z.country AS Country, z.latitude AS Latitude, z.longitude AS Longitude, p.radius, p.distanceunit * DEGREES(ACOS(COS(RADIANS(p.latpoint)) * COS(RADIANS(z.latitude)) * COS(RADIANS(p.longpoint - z.longitude)) + SIN(RADIANS(p.latpoint)) * SIN(RADIANS(z.latitude)))) AS Distance FROM {Class:FSG-Data-Address} AS z

JOIN ( /* these are the query parameters * SELECT {AddressPage.Latitude Decimal } AS latpoint, {AddressPage.Longitude Decimal } AS longpoint, {AddressPage.Distance Decimal } AS radius, {AddressPage.pyNote Decimal } AS distanceunit, {AddressPage.IsFor } AS isfor ) AS p ON p.isfor = z.isfor)

AS d WHERE distance <= radius ORDER BY distance LIMIT 15

Note that the RDB-List step within the Code-Pega-List Connect_SQL_pr_fsg_data_address activity that sources the D_AddressesWithinDistance Data Page sources.

It is impossible to define this type of query using a report definition because it has two FROM-clause SELECTs, one aliased “z,” and the other aliased “d.” Unlike a report definition, a Connect SQL rule lacks the ability to dynamically modify its filter conditions based on a parameter value being empty. Unless the report definition is configured to generate “is null” when a parameter lacks a value, Pega ignores the filter condition, which, in some cases, can be risky unless a limit is placed on the number of returned rows.

Within the HaversineFormula query, there is no need to generate the filter conditions. It does not make sense to execute the query unless a value is supplied for every query parameter, with the exception of the IsFor column, currently either “HOTEL” or “VENUE.”

Care must be taken when using Connect-SQL rules as the column names may not be returned as aliased. For example, despite aliasing the lower-case postal code column to camel-case PostalCode, the column name is returned all lower-case, the same as it exists in a Postgres database. 

In PostgreSQL unquoted names are case-insensitive. e.g. Select Street as STREET or Street or street, provide the column name as "street "only. if you want to get column name as "Street", then column  should be placed within the quotes  e.g. Select Street as “Street”.


This Topic is available in the following Module:

If you are having problems with your training, please review the Pega Academy Support FAQs.

Did you find this content helpful?

Want to help us improve this content?

We'd prefer it if you saw us at our best.

Pega Academy has detected you are using a browser which may prevent you from experiencing the site as intended. To improve your experience, please update your browser.

Close Deprecation Notice