3

我想根据分组值从一个表 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
4

2 回答 2

1

当我在 SQLFiddle 中构建您的架构时,这似乎得到了您的答案。

SELECT COUNT(*) AS Device_Count, Criticality
FROM TPM_scan
GROUP BY criticality

http://www.sqlfiddle.com/#!6/c5287/6

于 2013-04-26T13:05:33.423 回答
1

您应该只从派生表中的 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
于 2013-04-26T13:02:24.180 回答