用于提供第二个查询的 SQL 查询
我正在尝试弄清楚是否可以将两个(或多个)查询组合在一起,其中第二个查询需要第一个查询的表名和“where”值。
这是第一个查询,它为后续查询返回“where”值和表名:
select table_id, r_table, target FROM S_QOS_DATA
where robot = 'hostname'
AND qos='QOS_CPU_MULTI_USAGE'
它返回下表:
table_id | r_table | target |
---|---|---|
1332070 | RN_QOS_DATA_0073 | hostname-0 |
1332071 | RN_QOS_DATA_0074 | hostname-1 |
1332072 | RN_QOS_DATA_0075 | hostname-2 |
然后我已经手动对每个运行以下查询上述值的其中一个:
select sampletime, samplevalue, 'hostname-0' as target
FROM RN_QOS_DATA_0073
where table_id = 1332070
返回以下值(好吧,44,500 中的 4 行):
Sampletime | SampleValue | Target |
---|---|---|
2022-01-26 00:00:42.000 | 45.59 | hostname-0 |
2022-01-26 00:01:42.000 | 39.66 | hostname-0 |
2022-01-26 00:02:42.000 | 44.54 | hostname-0 |
2022-01-26 00:03:42.000 | 42.23 | hostname-0 |
下一个查询将如下所示:
select sampletime, samplevalue, 'hostname-1' as target
FROM RN_QOS_DATA_0074
where table_id = 1332071
then
select sampletime, samplevalue, 'hostname-2' as target
FROM RN_QOS_DATA_0075
where table_id = 1332072
等等,依此类推。
在过去的几个小时里,我一直在此处搜索嵌套查询,但我不确定这是否是正确的术语,因为我找到的所有示例都没有显示用作表名称的结果列表。
我的最终目标是运行一个返回如下表的查询:
Sampletime | SampleValue | Target |
---|---|---|
2022-01-26 00:00:42.000 | 45.59 | hostname-0 |
2022-01-26 00:01:42.000 | 39.66 | hostname-0 |
2022-01 -26 00:02:42.000 | 44.54 | 主机名-0 |
2022-01-26 00:03:42.000 | 42.23 | 主机名-0 |
2022-01-27 00:00:42.000 | 45.02 | 主机名-1 |
2022-01-27 00:01:42.000 | 42.11 | 主机名-1 |
2022-01-27 00:02:42.000 | 38.12 | 主机名-1 |
2022-01-27 00:03:42.000 | 40.69 | 主机名-1 |
2022-01-26 00:00:42.000 | 53.28 | 主机名-2 |
2022-01-26 00:01:42.000 | 45.10 | hostname-2 |
2022-01-26 00:02:42.000 | 45.97 | hostname-2 |
2022-01-26 00:03:42.000 | 47.60 | hostname-2 |
如果可能的话,如果有人可以的话,我将非常感激用正确的方式推动我走向正确的方向术语。
I'm trying to work out if it's possible to combine two (or more) queries together into one, where the second one requires a table name and "where" value from the first.
This is the first query which returns the "where" value and table name for the subsequent queries:
select table_id, r_table, target FROM S_QOS_DATA
where robot = 'hostname'
AND qos='QOS_CPU_MULTI_USAGE'
which returns the following table:
table_id | r_table | target |
---|---|---|
1332070 | RN_QOS_DATA_0073 | hostname-0 |
1332071 | RN_QOS_DATA_0074 | hostname-1 |
1332072 | RN_QOS_DATA_0075 | hostname-2 |
I've then been manually running the following query with each of the above values:
select sampletime, samplevalue, 'hostname-0' as target
FROM RN_QOS_DATA_0073
where table_id = 1332070
which returns the following (well, 4 rows of the 44,500):
sampletime | samplevalue | target |
---|---|---|
2022-01-26 00:00:42.000 | 45.59 | hostname-0 |
2022-01-26 00:01:42.000 | 39.66 | hostname-0 |
2022-01-26 00:02:42.000 | 44.54 | hostname-0 |
2022-01-26 00:03:42.000 | 42.23 | hostname-0 |
The next query will look like this:
select sampletime, samplevalue, 'hostname-1' as target
FROM RN_QOS_DATA_0074
where table_id = 1332071
then
select sampletime, samplevalue, 'hostname-2' as target
FROM RN_QOS_DATA_0075
where table_id = 1332072
and so on, and so on.
I've been searching through here for nested queries for the past couple of hours, but I'm not sure if that's the correct terminology as none of the examples I can find show a list of results being used as the table name.
My ultimate goal is to run a single query that returns a table like this:
sampletime | samplevalue | target |
---|---|---|
2022-01-26 00:00:42.000 | 45.59 | hostname-0 |
2022-01-26 00:01:42.000 | 39.66 | hostname-0 |
2022-01-26 00:02:42.000 | 44.54 | hostname-0 |
2022-01-26 00:03:42.000 | 42.23 | hostname-0 |
2022-01-27 00:00:42.000 | 45.02 | hostname-1 |
2022-01-27 00:01:42.000 | 42.11 | hostname-1 |
2022-01-27 00:02:42.000 | 38.12 | hostname-1 |
2022-01-27 00:03:42.000 | 40.69 | hostname-1 |
2022-01-26 00:00:42.000 | 53.28 | hostname-2 |
2022-01-26 00:01:42.000 | 45.10 | hostname-2 |
2022-01-26 00:02:42.000 | 45.97 | hostname-2 |
2022-01-26 00:03:42.000 | 47.60 | hostname-2 |
If it's possible, I'd be so grateful if someone could push me in the right direction with the proper terminology.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个工作学院设法让这个工作正常进行,但由于这是他们第一次尝试动态 SQL - 他们不确定它是否有效或“正确”。
任何意见或建议将不胜感激!
A work colleage managed to get this working, but as it was their first attempt at Dynamic SQL - they're not sure if its efficient or "correct".
Any advice or recommendations would be appreciated!
您可以借助
DynamicQuery
来实现此目的。这里是。
动态形成执行SQL脚本:
输出为
You can achieve this with the help of
DynamicQuery
.Here it is.
Dynamically form the execution SQL script:
The output is