我想根据分组值从一个表 dbo.VTM_duedate 和另一个表 dbo.TPM_scan 中获取分组值(列:'Critality')和下一个计数值(列:'DEVICE_NAME')。
表格1
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