# Get Dataset From DB

**URL:** <https://forum.inductiveautomation.com/t/get-dataset-from-db/9488>\
**Category:** Ignition\
**Created:** [December 29, 2014, 12:49pm UTC](https://forum.inductiveautomation.com/t/get-dataset-from-db/9488 "2014-12-29T12:49:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Automator59](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@Automator59](https://forum.inductiveautomation.com/u/Automator59)\
**Post date:** [December 29, 2014, 12:49pm UTC](https://forum.inductiveautomation.com/t/get-dataset-from-db/9488/1 "2014-12-29T12:49:27Z")

</div>

I need to get the top 5 items in a MS SQL database table (descending order) and looking for the best way to accomplish this in Ignition. I have a table that is collecting data for a production line and need to get the top 5 names and times for those stations. It is very likely to have less than 5 with greater than zero, so I will ignore zero values.

table:  
index, int  
station, nvarchar(50)  
time, int

What would the best method of getting this data be, transaction group or gateway script, or something else?

Thanks!

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [December 29, 2014, 3:45pm UTC](https://forum.inductiveautomation.com/t/get-dataset-from-db/9488/2 "2014-12-29T15:45:52Z")

</div>

It depends on what you want to do with this data.

Do you need the data in a client or gateway? What do you want to do with it?

You probably just need a database query in Python using [system.db.runQuery](http://localhost:8088/main/system/help/index.html?system_db_runquery.htm).

```auto
result = system.db.runQuery("SELECT TOP 5 station,time from table ORDER BY time DESC")
```

You will need to have a database connection setup in Ignition and have the project using that connection as its default database connection.

---

<div class="post-metadata">

**Author:** ![Automator59](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@Automator59](https://forum.inductiveautomation.com/u/Automator59)\
**Post date:** [December 29, 2014, 4:08pm UTC](https://forum.inductiveautomation.com/t/get-dataset-from-db/9488/3 "2014-12-29T16:08:35Z")

</div>

This would be a gateway script. I have a table that is tracking current downtime and I need to show a chart with the top 5 downtime locations and the total time they were down, all that is included in a current status table.

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [December 29, 2014, 4:31pm UTC](https://forum.inductiveautomation.com/t/get-dataset-from-db/9488/4 "2014-12-29T16:31:31Z")

</div>

If you need the data in a chart then you do not need the data in the gateway, you need it in a runtime client because charts are in clients. More specifically you need the data in a client window, in a chart.

Perhaps what you should do is write a database query for a SQL Query binding on the “DB Pens” property on an [Easy Chart](http://localhost:8088/main/system/help/index.html?charts_pmieasychart.htm) in an Ignition window. Or do the same thing on the “Data” property of a Chart or Bar Chart component.

---

<div class="post-metadata">

**Author:** ![Automator59](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@Automator59](https://forum.inductiveautomation.com/u/Automator59)\
**Post date:** [December 29, 2014, 5:30pm UTC](https://forum.inductiveautomation.com/t/get-dataset-from-db/9488/5 "2014-12-29T17:30:58Z")

</div>

I used a chart and did a query with selecting TOP into a chart

SELECT TOP(5) Station, ShiftDownTime  
FROM tblStatCurState  
WHERE (ShiftDownTime \> 0)  
ORDER BY ShiftDownTime DESC

I will modify this some more based on getting a specific area for the given screen which is assigned individually by location already.

Thanks for the help
