用于提供第二个查询的 SQL 查询

发布于 2025-01-10 10:17:39 字数 4591 浏览 0 评论 0原文

我正在尝试弄清楚是否可以将两个(或多个)查询组合在一起,其中第二个查询需要第一个查询的表名和“where”值。

这是第一个查询,它为后续查询返回“where”值和表名:

select table_id, r_table, target FROM S_QOS_DATA
where robot = 'hostname'
AND qos='QOS_CPU_MULTI_USAGE'

它返回下表:

table_idr_tabletarget
1332070RN_QOS_DATA_0073hostname-0
1332071RN_QOS_DATA_0074hostname-1
1332072RN_QOS_DATA_0075hostname-2

然后我已经手动对每个运行以下查询上述值的其中一个:

select sampletime, samplevalue, 'hostname-0' as target
FROM RN_QOS_DATA_0073
where table_id = 1332070

返回以下值(好吧,44,500 中的 4 行):

SampletimeSampleValueTarget
2022-01-26 00:00:42.00045.59hostname-0
2022-01-26 00:01:42.00039.66hostname-0
2022-01-26 00:02:42.00044.54hostname-0
2022-01-26 00:03:42.00042.23hostname-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

等等,依此类推。

在过去的几个小时里,我一直在此处搜索嵌套查询,但我不确定这是否是正确的术语,因为我找到的所有示例都没有显示用作表名称的结果列表。

我的最终目标是运行一个返回如下表的查询:

SampletimeSampleValueTarget
2022-01-26 00:00:42.00045.59hostname-0
2022-01-26 00:01:42.00039.66hostname-0
2022-01 -26 00:02:42.00044.54主机名-0
2022-01-26 00:03:42.00042.23主机名-0
2022-01-27 00:00:42.00045.02主机名-1
2022-01-27 00:01:42.00042.11主机名-1
2022-01-27 00:02:42.00038.12主机名-1
2022-01-27 00:03:42.00040.69主机名-1
2022-01-26 00:00:42.00053.28主机名-2
2022-01-26 00:01:42.00045.10hostname-2
2022-01-26 00:02:42.00045.97hostname-2
2022-01-26 00:03:42.00047.60hostname-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_idr_tabletarget
1332070RN_QOS_DATA_0073hostname-0
1332071RN_QOS_DATA_0074hostname-1
1332072RN_QOS_DATA_0075hostname-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):

sampletimesamplevaluetarget
2022-01-26 00:00:42.00045.59hostname-0
2022-01-26 00:01:42.00039.66hostname-0
2022-01-26 00:02:42.00044.54hostname-0
2022-01-26 00:03:42.00042.23hostname-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:

sampletimesamplevaluetarget
2022-01-26 00:00:42.00045.59hostname-0
2022-01-26 00:01:42.00039.66hostname-0
2022-01-26 00:02:42.00044.54hostname-0
2022-01-26 00:03:42.00042.23hostname-0
2022-01-27 00:00:42.00045.02hostname-1
2022-01-27 00:01:42.00042.11hostname-1
2022-01-27 00:02:42.00038.12hostname-1
2022-01-27 00:03:42.00040.69hostname-1
2022-01-26 00:00:42.00053.28hostname-2
2022-01-26 00:01:42.00045.10hostname-2
2022-01-26 00:02:42.00045.97hostname-2
2022-01-26 00:03:42.00047.60hostname-2

If it's possible, I'd be so grateful if someone could push me in the right direction with the proper terminology.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷路的信 2025-01-17 10:17:39

一个工作学院设法让这个工作正常进行,但由于这是他们第一次尝试动态 SQL - 他们不确定它是否有效或“正确”。

DECLARE @table NVARCHAR(MAX);
DECLARE @sql AS NVARCHAR(MAX);
DECLARE @robot as NVARCHAR(MAX);
set @robot = 'hostname';
select TOP 1 @table=r_table FROM S_QOS_DATA where robot =@robot AND qos='QOS_CPU_MULTI_USAGE';
set @sql = N'select points.sampletime as time, points.samplevalue, hosts.target
FROM S_QOS_DATA AS hosts 
JOIN '+@table+' AS points 
ON hosts.table_id = points.table_id AND hosts.robot='+ CHAR(39)+@robot+ CHAR(39)+' AND qos=''QOS_CPU_MULTI_USAGE'' AND points.sampletime 
ORDER BY points.sampletime';
EXEC sp_executesql @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".

DECLARE @table NVARCHAR(MAX);
DECLARE @sql AS NVARCHAR(MAX);
DECLARE @robot as NVARCHAR(MAX);
set @robot = 'hostname';
select TOP 1 @table=r_table FROM S_QOS_DATA where robot =@robot AND qos='QOS_CPU_MULTI_USAGE';
set @sql = N'select points.sampletime as time, points.samplevalue, hosts.target
FROM S_QOS_DATA AS hosts 
JOIN '+@table+' AS points 
ON hosts.table_id = points.table_id AND hosts.robot='+ CHAR(39)+@robot+ CHAR(39)+' AND qos=''QOS_CPU_MULTI_USAGE'' AND points.sampletime 
ORDER BY points.sampletime';
EXEC sp_executesql @sql;

Any advice or recommendations would be appreciated!

无人问我粥可暖 2025-01-17 10:17:39

您可以借助DynamicQuery 来实现此目的。

这里是。

CREATE TABLE Tbl_master(table_Id INT, r_table VARCHAR(100), target VARCHAR(100))
INSERT INTO Tbl_master VALUES (1332070, 'RN_QOS_DATA_0073','hostname-0')
INSERT INTO Tbl_master VALUES (1332071, 'RN_QOS_DATA_0074','hostname-1')
INSERT INTO Tbl_master VALUES (1332072, 'RN_QOS_DATA_0075','hostname-2')

CREATE TABLE RN_QOS_DATA_0073 (table_Id INT, SampleTime DATETIME, SampleValue NUMERIC (10,2), target VARCHAR(100))
INSERT INTO RN_QOS_DATA_0073 VALUES (1332070, '2022-01-26 00:00:42.000', 1.1,'hostname-0')
INSERT INTO RN_QOS_DATA_0073 VALUES (1332070, '2022-01-26 00:01:42.000', 2.1,'hostname-0')
INSERT INTO RN_QOS_DATA_0073 VALUES (1332070, '2022-01-26 00:02:42.000', 3.1,'hostname-0')

CREATE TABLE RN_QOS_DATA_0074 (table_Id INT, SampleTime DATETIME, SampleValue NUMERIC (10,2), target VARCHAR(100))
INSERT INTO RN_QOS_DATA_0074 VALUES (1332071, '2022-01-27 00:00:42.000', 1.2,'hostname-1')
INSERT INTO RN_QOS_DATA_0074 VALUES (1332071, '2022-01-27 00:01:42.000', 2.2,'hostname-1')
INSERT INTO RN_QOS_DATA_0074 VALUES (1332071, '2022-01-27 00:02:42.000', 3.2,'hostname-1')

CREATE TABLE RN_QOS_DATA_0075 (table_Id INT, SampleTime DATETIME, SampleValue NUMERIC (10,2), target VARCHAR(100))
INSERT INTO RN_QOS_DATA_0075 VALUES (1332072, '2022-01-28 00:00:42.000', 1.3,'hostname-2')
INSERT INTO RN_QOS_DATA_0075 VALUES (1332072, '2022-01-28 00:01:42.000', 2.3,'hostname-2')
INSERT INTO RN_QOS_DATA_0075 VALUES (1332072, '2022-01-28 00:02:42.000', 3.3,'hostname-2')

动态形成执行SQL脚本:

DECLARE @ExecQuery VARCHAR(MAX)

SELECT @ExecQuery = STRING_AGG('SELECT sampletime, samplevalue,'''+target+''' as Target FROM '+r_table+' WHERE table_Id = '+ CAST(table_Id AS VARCHAR),' UNION ALL ')
FROM Tbl_master

EXEC (@ExecQuery)

输出为

sampletime              samplevalue Target
2022-01-26 00:00:42.000 1.10        hostname-0
2022-01-26 00:01:42.000 2.10        hostname-0
2022-01-26 00:02:42.000 3.10        hostname-0
2022-01-27 00:00:42.000 1.20        hostname-1
2022-01-27 00:01:42.000 2.20        hostname-1
2022-01-27 00:02:42.000 3.20        hostname-1
2022-01-28 00:00:42.000 1.30        hostname-2
2022-01-28 00:01:42.000 2.30        hostname-2
2022-01-28 00:02:42.000 3.30        hostname-2

You can achieve this with the help of DynamicQuery.

Here it is.

CREATE TABLE Tbl_master(table_Id INT, r_table VARCHAR(100), target VARCHAR(100))
INSERT INTO Tbl_master VALUES (1332070, 'RN_QOS_DATA_0073','hostname-0')
INSERT INTO Tbl_master VALUES (1332071, 'RN_QOS_DATA_0074','hostname-1')
INSERT INTO Tbl_master VALUES (1332072, 'RN_QOS_DATA_0075','hostname-2')

CREATE TABLE RN_QOS_DATA_0073 (table_Id INT, SampleTime DATETIME, SampleValue NUMERIC (10,2), target VARCHAR(100))
INSERT INTO RN_QOS_DATA_0073 VALUES (1332070, '2022-01-26 00:00:42.000', 1.1,'hostname-0')
INSERT INTO RN_QOS_DATA_0073 VALUES (1332070, '2022-01-26 00:01:42.000', 2.1,'hostname-0')
INSERT INTO RN_QOS_DATA_0073 VALUES (1332070, '2022-01-26 00:02:42.000', 3.1,'hostname-0')

CREATE TABLE RN_QOS_DATA_0074 (table_Id INT, SampleTime DATETIME, SampleValue NUMERIC (10,2), target VARCHAR(100))
INSERT INTO RN_QOS_DATA_0074 VALUES (1332071, '2022-01-27 00:00:42.000', 1.2,'hostname-1')
INSERT INTO RN_QOS_DATA_0074 VALUES (1332071, '2022-01-27 00:01:42.000', 2.2,'hostname-1')
INSERT INTO RN_QOS_DATA_0074 VALUES (1332071, '2022-01-27 00:02:42.000', 3.2,'hostname-1')

CREATE TABLE RN_QOS_DATA_0075 (table_Id INT, SampleTime DATETIME, SampleValue NUMERIC (10,2), target VARCHAR(100))
INSERT INTO RN_QOS_DATA_0075 VALUES (1332072, '2022-01-28 00:00:42.000', 1.3,'hostname-2')
INSERT INTO RN_QOS_DATA_0075 VALUES (1332072, '2022-01-28 00:01:42.000', 2.3,'hostname-2')
INSERT INTO RN_QOS_DATA_0075 VALUES (1332072, '2022-01-28 00:02:42.000', 3.3,'hostname-2')

Dynamically form the execution SQL script:

DECLARE @ExecQuery VARCHAR(MAX)

SELECT @ExecQuery = STRING_AGG('SELECT sampletime, samplevalue,'''+target+''' as Target FROM '+r_table+' WHERE table_Id = '+ CAST(table_Id AS VARCHAR),' UNION ALL ')
FROM Tbl_master

EXEC (@ExecQuery)

The output is

sampletime              samplevalue Target
2022-01-26 00:00:42.000 1.10        hostname-0
2022-01-26 00:01:42.000 2.10        hostname-0
2022-01-26 00:02:42.000 3.10        hostname-0
2022-01-27 00:00:42.000 1.20        hostname-1
2022-01-27 00:01:42.000 2.20        hostname-1
2022-01-27 00:02:42.000 3.20        hostname-1
2022-01-28 00:00:42.000 1.30        hostname-2
2022-01-28 00:01:42.000 2.30        hostname-2
2022-01-28 00:02:42.000 3.30        hostname-2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文