New in the Community? Get started here

Schneider Electric Exchange Community

Discuss and solve problems in energy management and automation. Join conversations and share insights on products and solutions. Co-innovate and collaborate with a global network of peers.

Register Now
Geo SCADA Expert Forum
Showing results for 
Search instead for 
Did you mean: 
Lt. Commander

[import] SQL Query, need guidance

>>Message imported from previous forum - Category:ClearSCADA Software<<
User: hardin4019, originally posted: 2019-02-04 22:37:59 Id:359
I have a template for multiple sites with up to 5 wireless sensors reporting back to the master radio at each site (acting as the Modbus RTU) we are recording 3 Modbus Advacned Analog variables per pressure sensor for, so as many as 15 points at a site.

Sensor 1 Pressure
Sensor 1 Battery Voltage
Sensor 1 RSSI

Sensor 2 Pressure
Sensor 2 Battery Voltage
Sensor 2 RSSI

Sensor 3, etc...

Each Pressure Sensor Analog point has specific information I want to show in the first 3 columns of a table, "Station Name", "Pressure Source", and "Line Name". Pressure Source and Line name are unique to each pressure sensor, and I have some Metadata fields setup called ObjProperty01, ObjProperty02, ObjProperty03 that contains this info for each active Pressure Sensor. Then after those 3 columns I want to show the value of Pressure Sensor 1, Pressure Sensor 1 Battery Voltage, Pressure Sensor 1 RSSI, then repeat for each pressure sensor.

Where I am having issues is probably just lack of experience with SQL Queries, but it is very simple to get a Query like:

ID, FullName AS "_FullName", ObjProperty01 AS "Station Name", ObjProterty02 AS "Pressure Source", ObjProperty03 AS "Line Name", CurrentValueFormated AS "Pressure"
From CDBPoint

But is there a way to specify that I want the value of the battery voltage for the same sensor in the next column, and RSSI in the last column?

I have also tried mapping out all 15 points to the Group Points and have successfully gotten the first Pressure Sensor and related values to show up in a single row, but need help with how to get the query to repeat for each Pressure Sensor after the first. That Query looks something like:

ID, FullName AS "_FullName", GPoint01-ObjProperty01 AS "Station Name", GPoint01-ObjProterty02 AS "Pressure Source", GPoint03-ObjProperty03 AS "Line Name", GPoint01-CurrentValueFormated AS "Pressure", GPoint01 AS "_ID01", GPoint02-CurrentValueFormated AS "Battery Voltage", GPoint03-CurrentValueFormated AS "RSSI"

But I want this query to continue on with the next line as GPoint04 (the next pressure sensor), GPoint05 (the next sensor battery voltage), and GPoint06 (the next sensor RSSI) using the same columns and names as the query above, and then repeat for sensors 3-5.

I'm hoping someone has some suggestions and is better at SQL Queries than I am!



Reply User: sbeadle, posted: 2019-02-04 23:13:19
I'd strongly consider a Data Set for this. It is possible with SQL, but could be less efficient. You can have multiple rows of a Data Set within a single instance

Reply User: dmercer, posted: 2019-02-05 05:12:08
I find that these work best when you set up the database folder structure in a way that makes the divisions between the sites and the sensors clear. eg.
---Battery Voltage
---Battery Voltage
---Battery Voltage

With that group structure, something like the below query should work (I haven't tested it)

SELECT TVal.SensorId-FullName AS "Name", TVal.CurrentValue, TRSSI.CurrentValue, TBatt.CurrentValue FROM

(SELECT ParentGroupId AS "SensorId", ParentGroupId-ParentGroupId AS SiteId, ParentGroupName, CurrentValue
FROM CAdvModbusAnalogIn
WHERE NOT "Name" = 'RSSI' AND NOT "Name' = 'Battery Voltage') AS TVal


(SELECT ParentGroupId AS "SensorId", ParentGroupId-ParentGroupId AS SiteId, ParentGroupName, CurrentValue
FROM CAdvModbusAnalogIn

ON TVal.SensorId= TRSSI.SensorId

(SELECT ParentGroupId AS "SensorId", ParentGroupId-ParentGroupId AS SiteId, ParentGroupName, CurrentValue
FROM CAdvModbusAnalogIn
WHERE "Name" = 'Battery Voltage' ) AS TBatt

ON TVal.SensorId= TBatt.SensorId

As sbeadle said, a dataset will make it more efficient. The difference may or may not matter in your system.

Reply User: tfranklin, posted: 2019-02-05 14:24:37
That metadata looks eerily familiar! If the sites are structured like [at]dmercer suggested, then you can easily use the query that you already wrote and just tack on a where clause for WHERE objType = 'Sensor' or something to that degree.

Reply User: hardin4019, posted: 2019-02-05 16:47:15
I tried both ways. The method [at]dmercer suggested is providing the most favorable results for now as it lets me use a custom pick action script to trend the points and access the point menu.

Now I'm down the rabbit hole of getting my pick action script to give me a ".~Detail Faceplate" from the Parent group holding all of the sub groups with sensor values.... So much fun!

"Set obj = Server.LookupObject(rowID)
ShowOnDetailHead "SCX:////CMimic/" & obj.FullName & ".Detail Faceplate" " is giving me the full name of the sensor folder. Changine obj.FullName to obj.Parent is giving me the ID of the parent folder and not the name. Any thoughts?

Reply User: hardin4019, posted: 2019-02-05 17:46:46
Fixed my issue with getting the Parent Group path instead of getting the ID number and now have a workable solution. Thanks all!

Reply User: sbeadle, posted: 2019-02-05 17:47:09
Should you use .ParentGroupName?

Reply User: hardin4019, posted: 2019-02-05 17:53:13
[at]sbeadle said:
Should you use .ParentGroupName?

Tried that, its not one of the supported "ServerObject Properties". The .Parent is supported, but returns the ID instead of the name. So I ended up with something like below.

dim ojb, ojb2
set obj = Server.LookupObject(rowID)

set rowID = ojb.Parent
set obj2 = Server.LookupObject(rowID)

ShowPopUp "SCX:////CMimic/" & obj2.FullName & ".Detail Faceplate" ,10,10,25,40