我想利用分組值(柱:「臨界」)合併從一個表dbo.VTM_duedate和下一計數值(柱:「DEVICE_NAME」)在另一個表DBO .TPM_scan基於分組值。SQL agregate值在一個表中,並與數從另一個表
表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
提供了樣本數據這樣做回答這個問題對我們就輕鬆多了。感謝您的徹底和完整。 – 2013-04-26 13:03:39