gpt4 book ai didi

SQL 聚合一个表中的值并与另一个表中的计数合并

转载 作者:行者123 更新时间:2023-12-02 02:03:08 25 4
gpt4 key购买 nike

我想从一个表 dbo.VTM_duedate 中获取分组值(列:'Criticality'),并根据分组值在另一个表 dbo.TPM_scan 中获取下一个计数值(列:'DEVICE_NAME')。

表一

USE tempdb;
GO

IF OBJECT_ID('dbo.VTM_duedate') IS NOT NULL
DROP TABLE dbo.VTM_duedate;
GO

CREATE TABLE dbo.VTM_duedate
(
Criticality varchar(30) NOT NULL,
KB varchar(10) NOT NULL
);

INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('Medium', 157848);
INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('High', 155439);
INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('High', 635533);
INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('Critical', 189164);
INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('High', 188641);
INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('Critical', 537990);
INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('Critical', 1349605);
INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('Critical', 5646789);
INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('Medium', 6545789);
INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('High', 5637965);
INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('Medium', 6464367) ;
INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('Medium', 1323123) ;
INSERT INTO dbo.VTM_duedate(Criticality, KB)
VALUES('Medium', 1004326) ;
GO

表2

USE tempdb;
GO

IF OBJECT_ID('dbo.TPM_scan') IS NOT NULL
DROP TABLE dbo.TPM_scan;
GO

CREATE TABLE dbo.TPM_scan
(
DEVICE_NAME varchar(30) NOT NULL,
APP_ID varchar(10) NOT NULL,
Criticality varchar(10) NOT NULL
);

INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('LDNSQLF700', 157848, 'Medium');
INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('LDNSQLF700', 155439, 'High');
INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('LDNSQLF700', 635533, 'High');
INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('NYSQL502', 189164, 'Critical');
INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('NYSQL502', 188641, 'High');
INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('AUSSQL140', 537990, 'High');
INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('AUSSQL140', 1349605, 'Critical');
INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('JAP543X2', 5646789, 'Medium');
INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('EU456CLX', 6545789, 'Critical');
INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('EUCTX654', 5637965, 'High');
INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('EUCTX654', 6464367, 'Medium') ;
INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('EUCTX654', 1323123, 'High') ;
INSERT INTO dbo.TPM_scan(DEVICE_NAME, APP_ID, Criticality)
VALUES('EUCTX654', 1004326, 'Medium') ;
GO

预期结果:

Criticality   Device_count
Critical 3
High 6
Medium 4

最佳答案

您应该只从派生表中的 dbo.VTM_duedate 选择不同的关键性列表,并将其加入 dbo.TPM_scan 并获得您的计数!

select
s.Criticality,
count(*) as 'Device_Count'
from
(select distinct Criticality from dbo.VTM_duedate) d
inner join dbo.TPM_scan s
on d.Criticality = s.Criticality
group by
s.Criticality

关于SQL 聚合一个表中的值并与另一个表中的计数合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16237082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com