{"id":3775,"date":"2017-06-26T18:46:20","date_gmt":"2017-06-26T10:46:20","guid":{"rendered":"http:\/\/localhost\/help\/?page_id=3775"},"modified":"2024-11-22T14:48:17","modified_gmt":"2024-11-22T06:48:17","slug":"how-to-write-a-dataset-query","status":"publish","type":"page","link":"https:\/\/oihelp.corporate.ifs.com\/help\/p2-server\/connecting-your-data\/creating-a-dataset-datasource\/how-to-write-a-dataset-query\/","title":{"rendered":"How to Write a Dataset Query"},"content":{"rendered":"<p class=\"intro-text\"><\/p>\n<p class=\"intro-text\">Datasets are backed by SQL queries that return data when executed against their associated <a href=\"https:\/\/oihelp.corporate.ifs.com\/help\/p2-server\/connecting-your-data\/creating-a-dataset-datasource\/\">datasource<\/a>. IFS OI Server provides a number of keywords that allow a degree of customisation of such SQL queries, such as attaching user-supplied parameters to the query at execution time. These keywords are pre-processed before the SQL query is executed, so the one dataset definition can be reused for variations on the query (such as in a WHERE clause) rather than having to define multiple, similar dataset queries.<\/p>\n<p class=\"intro-text\">When the query is pre-processed, these keywords are replaced in the query as described in the table below. When used, keywords that accept parameters will appear as options of the dataset in IFS OI Explorer.<\/p>\n<p class=\"intro-text\">Datasets that have parameters attached corresponding to entity names can also make use of the Entity Native Name Mapping feature, allowing datasources that use different names for identifying the same asset to be queried transparently using the entity names defined in IFS OI Server.<\/p>\n<p class=\"intro-text\">The following keywords are associated with datasets:<\/p>\n<table>\n<tbody>\n<tr style=\"border: 1px solid #cccccc;\">\n<td style=\"padding-left: 5px;\"><strong>Keyword<\/strong><\/td>\n<td style=\"padding-left: 5px;\"><span style=\"color: #000000;\"><b>Usage<\/b><\/span><\/td>\n<td style=\"padding-left: 5px;\"><span style=\"color: #000000;\"><b>Replacement Example <br \/>\n(SQL Server)<\/b><\/span><\/td>\n<td style=\"padding-left: 5px;\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr style=\"border: 1px solid #cccccc;\">\n<td style=\"padding-left: 5px;\"><strong>Param<\/strong><\/td>\n<td style=\"padding-left: 5px;\">\n<p>PARAM(parameter_name,data_type)<\/p>\n<p>Example:<br \/>\nPARAM(prodStartTime,datetime)<\/p>\n<\/td>\n<td style=\"padding-left: 5px;\">\n<p>@parameter_name<\/p>\n<p>@prodStartTime<\/p>\n<\/td>\n<td>\n<p>A single value that is passed to the query by IFS OI Explorer. This appears in IFS OI Explorer as one of the parameters that the user must supply when configuring the dataset.<\/p>\n<ul>\n<li><strong>Parameter_name<\/strong>: The name of the parameter which will appear in IFS OI Explorer. This keyword is case sensitive and does not accept spaces.<\/li>\n<li><strong>Data_type<\/strong>: The type of data expected by the parameter. Valid values are: string, integer, boolean, datetime, entity.<\/li>\n<\/ul>\n<p>When using the Entity data type, this keyword will check its\u00a0supplied parameters to see if it uses an\u00a0entity name with a defined native name for the data source. If such a mapping exists, the native name will be applied to the query instead.<\/p>\n<\/td>\n<\/tr>\n<tr style=\"border: 1px solid #cccccc;\">\n<td style=\"padding-left: 5px;\"><strong>Params<\/strong><\/td>\n<td style=\"padding-left: 5px;\">\n<p>PARAMS(parameter_name_csv,data_type)<\/p>\n<p>Example:<br \/>\nPARAMS(names,entity)<\/p>\n<\/td>\n<td style=\"padding-left: 5px;\">\n<p>@parameter_name1, @parameter_name2<\/p>\n<p>@names1, @names2, \u2026 @namesX<\/p>\n<\/td>\n<td style=\"padding-left: 5px;\">\n<p>A comma-separated list of values that is passed to the query by IFS OI Explorer. This appears in IFS OI Explorer as one of the parameters that the user must supply when configuring the dataset.<\/p>\n<ul>\n<li><strong>Parameter_name_csv<\/strong>: The name of the parameter which will appear in IFS OI Explorer. This parameter accepts a comma-separated list of names and is mostly used with the <em>entity<\/em> data type. This keyword is case sensitive and does not accept spaces.<\/li>\n<li><strong>Data_type<\/strong>: The type of data expected by the parameter. Valid values are: string, integer, boolean, datetime, entity.<\/li>\n<\/ul>\n<p>When using the Entity data type, this keyword will check its\u00a0supplied parameters to see if they are entity names with a defined native name for the data source. If such a mapping exists, the native name will be applied to the query instead.<\/p>\n<\/td>\n<\/tr>\n<tr style=\"border: 1px solid #cccccc;\">\n<td style=\"padding-left: 5px;\"><strong>Sortby<\/strong><\/td>\n<td style=\"padding-left: 5px;\">\n<p>SORTBY(colname1 [direction][,colnameX [direction]\u2026])<\/p>\n<p>Example:<br \/>\nSORTBY(PRODUCTION_ENTITY_NAME)<\/p>\n<p>\u00a0<br \/>\nSORTBY(PRODUCTION_ENTITY_NAME DESC, PRODUCTION_DATE)<\/p>\n<\/td>\n<td style=\"padding-left: 5px;\">\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>ORDER BY PRODUCTION_ENTITY_NAME<\/p>\n<p>ORDER BY PRODUCTION_ENTITY_NAME DESC, PRODUCTION_DATE<\/p>\n<\/td>\n<td>\n<p>Specifies the name of the columns that determines the ordering of the query results. This does not appear in IFS OI Explorer when configuring the dataset.<\/p>\n<ul>\n<li><strong>col_name<\/strong>: The name of the column by which to sort. To sort by multiple columns, use a comma\u00a0to separate the column names. If you specify more than 1 column, the order of sorting is determined by the order in which you specify the columns.<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<tr style=\"border: 1px solid #cccccc;\">\n<td style=\"padding-left: 5px;\"><strong>Entityname<\/strong><\/td>\n<td style=\"padding-left: 5px;\">\n<p>ENTITYNAME(column_expression[,alias[,sort]])<\/p>\n<p>Example:<br \/>\nENTITYNAME(UPPER(name))<\/p>\n<p>ENTITYNAME(UPPER(name),Uppercase)<\/p>\n<p>ENTITYNAME(UPPER(name),Uppercase,asc)<\/p>\n<\/td>\n<td style=\"padding-left: 5px;\">\n<p>&nbsp;<\/p>\n<p>\u00a0<br \/>\nUPPER(name)<\/p>\n<p>UPPER(name) AS Uppercase<\/p>\n<p>UPPER(name) AS Uppercase<\/p>\n<p>&nbsp;<\/p>\n<\/td>\n<td style=\"padding-left: 5px;\">\n<p>Allows you to map an entity in IFS OI Server to a specific unique identifier in a source system.<\/p>\n<ul>\n<li><strong>Column_expression<\/strong>: This is what would normally go in the query to return the native name (eg. \u201cSELECT name_column FROM\u2026\u201d becomes \u201cSELECT ENTITYNAME(name_column) FROM\u2026\u201d). This can be a column name or a function expression.<\/li>\n<li><strong>Alias<\/strong>: (optional) An alternative name\/header to use for the column, which will be turned into an AS clause. This needs to be specified as a parameter to the keyword rather than AS in the column expression so the adaptor can figure out which column to do remapping on. If left empty, the column name will be unchanged.<\/li>\n<li><strong>Sort<\/strong>: (optional) \u201casc\u201d or \u201cdesc\u201d for specifying the sort direction after replacement (but see limitations below).<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2 class=\"page-subheading\">Example Simple Queries<\/h2>\n<p class=\"intro-text\">Choke data for oil wells for the selected timestamp and entity. The Explorer user must supply an entity name and a datetime when they configure the dataset.\u00a0<\/p>\n<pre class=\"wrap:true lang:default decode:true \">select top 1 Node,Choke,GLChoke,MaxChoke \r\nfrom OilWells \r\nwhere Node = PARAM(well,entity) and TimeStamp &lt;= PARAM(time,datetime) \r\norder by TimeStamp desc<\/pre>\n<p class=\"intro-text\">Get daily or monthly production values for a trend, for the selected completion, within the selected period. The Explorer user must supply a list of completions and the reading type when they configure the dataset.<\/p>\n<pre class=\"wrap:true lang:default decode:true\">select cost_center as COMPLETION,\u00a0OIL,\u00a0GAS,\u00a0WATER, prod_date as PROD_DATE \r\nfrom etp_production_values \r\nwhere cost_center in (PARAMS(completionNames,entity)) \r\nand reading_type = PARAM(readingType,string) \r\nand prod_date between '1-Jan-2014' \r\nand '1-Mar-2015'<\/pre>\n<p>&nbsp;<\/p>\n<h2 class=\"page-subheading\">Example SELECT Queries using Entityname()<\/h2>\n<p class=\"intro-text\">1.\u00a0Accepts entities, performs the query using their native names then substitutes the original entity names in the result set. Example:<\/p>\n<pre class=\"wrap:true lang:default decode:true \">select ENTITYNAME(cost_center_name) \r\nfrom P2_PRODUCTION_METRICS \r\nwhere cost_center_name \r\nin (PARAMS(entities,entity))<\/pre>\n<p>&nbsp;<\/p>\n<p class=\"intro-text\">2.\u00a0Accepts entities, performs the query using their native names then substitutes the original entity names in the result set.\u00a0<br \/>\nHowever, the returned column will be titled \u201cCentre\u201d rather than \u201ccost_center_name\u201d. Example:<\/p>\n<pre class=\"wrap:true lang:default decode:true \">select ENTITYNAME(cost_center_name,Centre) \r\nfrom P2_PRODUCTION_METRICS \r\nwhere cost_center_name \r\nin (PARAMS(entities,entity))<\/pre>\n<p class=\"intro-text\">\u00a0<\/p>\n<p class=\"intro-text\">3.\u00a0Accepts entities, performs the query using their native names then substitutes the original entity names in the result set.<br \/>\nHowever, the results will be sorted in reverse alphabetical order and the column will have its original title (\u201ccost_center_name\u201d). Example:<\/p>\n<pre class=\"wrap:true lang:default decode:true \">select ENTITYNAME(cost_center_name,,desc) \r\nfrom P2_PRODUCTION_METRICS \r\nwhere cost_center_name \r\nin (PARAMS(entities,entity))<\/pre>\n<p>&nbsp;<\/p>\n<p class=\"intro-text\">4.\u00a0Accepts entities, performs the query using their native names then substitutes the original entity names in the result set.\u00a0<br \/>\nHowever, the returned column will be titled \u201cCentre\u201d rather than \u201ccost_center_name\u201d and\u00a0the rows will be sorted in alphabetical order. Example:<\/p>\n<pre class=\"wrap:true lang:default decode:true \">select ENTITYNAME(cost_center_name,Centre,asc) \r\nfrom P2_PRODUCTION_METRICS \r\nwhere cost_center_name \r\nin (PARAMS(entities,entity))<\/pre>\n<p>&nbsp;<\/p>\n<h2>Limitations of Entity Native Name Mapping<\/h2>\n<p class=\"intro-text\"><strong>General<\/strong><\/p>\n<ul class=\"intro-text\">\n<li>The native name specified for a mapping is case sensitive (while the entity name is not, as per the rest of the system). The query must generate results in the same casing as defined in the native name table for the reverse mapping to work.<\/li>\n<\/ul>\n<p class=\"intro-text\"><strong>Sorting<\/strong><\/p>\n<ul class=\"intro-text\">\n<li>Sorting is performed in memory after the result set is returned and the names remapped.<\/li>\n<li>If a sort is specified in the query (other than on the ENTITYNAME() keyword) the ordering will be based on the pre-remapping values of the column.\u00a0As a result, ENTITYNAME() sorting is likely to be incompatible with queries written to support <a href=\"https:\/\/oihelp.corporate.ifs.com\/help\/p2-server\/connecting-your-data\/creating-a-dataset-datasource\/how-to-write-a-dataset-query\/how-to-write-a-paged-query\/\">paging<\/a>, unless such queries are ordered by a different column to the ENTITYNAME().<\/li>\n<li>If no sort is specified, the rows returned will have the same order they did in the pre-remapping result set returned to the adaptor.<\/li>\n<li>If no alias is specified, reverse mapping should attempt to find a column with the same name as the text of column_expression.<\/li>\n<\/ul>\n<p class=\"intro-text\"><strong>Alias<\/strong><\/p>\n<ul class=\"intro-text\">\n<li>The column_expression or alias must appear as a column heading on the final result set for remapping to work.<\/li>\n<\/ul>\n<p class=\"intro-text\"><strong>Entity Names<\/strong><\/p>\n<ul class=\"intro-text\">\n<li>Only one ENTITYNAME() column in the query can have a sort specified.<\/li>\n<li>Mapping multiple entity names to the same native name may not work.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Datasets are backed by SQL queries that return data when executed against their associated datasource. IFS OI Server provides a number of keywords that allow a degree of customisation of such SQL queries, such as attaching user-supplied parameters to the query at execution time. This article describes how to construct dataset queries.<\/p>\n<p class=\"continue-reading-button\"> <a class=\"continue-reading-link\" href=\"https:\/\/oihelp.corporate.ifs.com\/help\/p2-server\/connecting-your-data\/creating-a-dataset-datasource\/how-to-write-a-dataset-query\/\">Read more<i class=\"crycon-right-dir\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":4677,"parent":3692,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[11],"tags":[674,108,104,219,218,129,220,306],"class_list":["post-3775","page","type-page","status-publish","has-post-thumbnail","hentry","category-tutorial","tag-datatype","tag-entities","tag-keywords","tag-native-names","tag-param","tag-queries","tag-sql","tag-tabular-data","Product-srv"],"_links":{"self":[{"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/pages\/3775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/comments?post=3775"}],"version-history":[{"count":3,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/pages\/3775\/revisions"}],"predecessor-version":[{"id":67729,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/pages\/3775\/revisions\/67729"}],"up":[{"embeddable":true,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/pages\/3692"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/media\/4677"}],"wp:attachment":[{"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/media?parent=3775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/categories?post=3775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oihelp.corporate.ifs.com\/help\/wp-json\/wp\/v2\/tags?post=3775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}