Sql tag expression

Would I be able to do this within a sql expression for example:
select * from(select top 1 pname FROM sqlth_partitions order by end_time desc)
in order to make searching through a history tag dynamic

It sounds possible. What’s stopping you?

[quote=“Julio.Bautista, post:1, topic:16876”]
select * from(select top 1 pname FROM sqlth_partitions order by end_time desc)
[/quote

well when I try to run it on the sql query browser on ignition I get this error

You will need to alias the subquery.


select * from (select top 1 pname FROM sqlth_partitions order by end_time desc) subquery1

https://www.techonthenet.com/sql_server/subqueries.php

1 Like

While you do need to alias a sub-query to make it work, this query isn’t going to return what you’re looking for. The top level query only returns a subset of the sub-query (if that makes sense).

What it seems like you’re trying to do is select all the history data for the last partition that was created. For that you’d need to do a dynamic sql statements.

declare @stmt nvarchar(250)
select top 1 @stmt = 'select * from ’ + pname FROM sqlth_partitions order by end_time desc

exec sp_executesql @stmt