我有 2 张桌子。
以下是我需要遵循的步骤才能获得所需的输出。我可以按照步骤 3 进行操作。请帮助我,因为它有点复杂,我无法理解如何进一步进行。
表格1
Site Code FailFlag Comments ModifiedDate ModifiedBy
ABT A01 F Dfasdf 10/11/2011 Anna
ABT A01 F dsfsdf 15/12/2012 Mand
ABT A01 Rds 30/03/2011 Tim
ABT A01 GHDs 02/12/2012 Andy
ABT A02 F dfd 09/05/2012 Anna
ABT A02 sdada 11/02/2013 Kathy
ABT A02 Dfg 15/05/2011 Rob
AFL A02 F asda 13/02/2011 Dan
AFL A02 dsaa 24/12/2010 Ryan
TRG A01 sdasd 16/04/2010 Richard
TRG K05 jksdh 23/04/2012 Mark
KLD K05 F sd 18/05/2013 Jim
KLD K05 dsfsd 10/03/2012 James
KLD K05 sdsd 12/05/2011 Luther
KTY K05 F saq 09/09/2012 Ryan
KTY K05 asd 04/04/2010 Kathy
KMD C02 F nas 29/02/2012 Rob
KMD C02 asda 11/11/2011 Andy
表 2:
Site Code Freq StartDate EndDate
ABT A01 43 01/01/2011 01/02/2012
ABT A02 254 01/01/2011 19/02/2011
ABT A02 109 20/02/2011 01/01/2012
ABT A02 12 02/01/2012 01/01/2013
AFL A02 13 01/01/2011 01/02/2012
TRG A01 122 01/01/2011 01/02/2012
TRG K05 61 01/01/2011 01/02/2012
KLD KO5 33 01/01/2011 15/05/2012
KLD K05 79 16/05/2012 01/01/2013
KTY K05 52 01/01/2011 01/02/2012
KMD C02 78 01/01/2011 01/02/2012
ZYT G01 11 01/01/2011 01/02/2012
PYN A01 15 01/01/2011 01/02/2012
DYN F08 122 01/01/2011 01/02/2012
脚步 :
表 1 中的“站点”和“代码”两列的组合是在表 2 中的“站点”和“代码”列的组合中查看的。
在“失败”列中过滤相同的内容并找出失败的次数
以下是查询和输出:
SELECT Site,Code,COUNT(*) as [Count], FailFlagCount= SUM(CASE WHEN F = 'F' THEN 1 ELSE 0 END) FROM Table1 GROUP BY Site,Det Site Code Count FailFlagCount ABT A01 4 2 ABT A02 3 1 AFL A02 2 1 TRG A01 1 0 TRG K05 1 0 KLD KO5 3 1 KTY K05 2 1 KMD C02 2 1
我们在表 2 中检查相同的组合。即,在表 2 中查看步骤 2 输出的站点和代码以获得其频率
计算:
一个。CC % = [1-(FailCount / Count)]*100 = [1-(2/4)]*100
湾。BPO % = [1-(FailCount / Freq)]*100 = [1-(2/43)]*100
c.Forecast% =
让我们假设当前月份是 3 月。
计算过去 3 年的失败次数并找到平均值,假设为“X”。
计算剩余月份的故障,'Y' = (X * 剩余月份/12)
12 个月内的总故障 = 当前故障 + Y因此 Forecast% = [1-(12 个月内的总故障/频率)]*100。
对于我们的示例,让我们假设 X = 2 的值因此 Y = (2 * 9/12)
预测百分比 = [1-(1+1.5)]*100
将对站点和代码的所有可能组合进行上述计算。
Site Code CC B.P.O Forecast StartDate EndDate ABT A01 01/01/2011 01/02/2012 ABT A02 01/01/2011 19/02/2011 ABT A02 20/02/2011 01/01/2012 ABT A02 02/01/2012 01/01/2013 AFL A02 01/01/2011 01/02/2012 TRG A01 01/01/2011 01/02/2012 TRG K05 01/01/2011 01/02/2012 KLD K05 01/01/2011 15/05/2012 KLD K05 16/05/2012 01/01/2013 KTY K05 01/01/2011 01/02/2012 KMD C02 01/01/2011 01/02/2012
上表的分组是在站点类型上完成的,即站点的第一个字母。然后在分组后完成所有计算(cc,BPO,Forecast)的平均值例如: - 'A' 代表'ABT','T' 代表'TRG'。(我假设我们为多种类型创建多个表,然后进行联合以获取以下查询)
示例:-Site Code CC B.P.O Forecast A A01 A A02 [i.e.,Avg value of (ABT and A02) and (AFL and A02)] T A01 T K05 avg [i.e., Avg value of (KLD and K05) and (KTY and K05)] K K05 K C02
这应该是我的最终输出。请帮忙。
为表格添加脚本以便更容易提供帮助:
表 1 脚本:
CREATE TABLE Table1
(
ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
Site VARCHAR(5),
Code VARCHAR(5),
FailFlag CHAR(1),
Comments VARCHAR(100),
ModifiedDate DATETIME,
ModifiedBy VARCHAR(50)
);
INSERT INTO Table1
(Site, Code, FailFlag, Comments, ModifiedDate, ModifiedBy)
SELECT 'ABT', 'A01', 'F', 'Dfasdf', '10/11/2011', 'Anna' UNION ALL
SELECT 'ABT', 'A01', 'F', 'dsfsdf', '15/12/2012', 'Mand' UNION ALL
SELECT 'ABT', 'A01', NULL, 'Rds', '30/03/2011', 'Tim' UNION ALL
SELECT 'ABT', 'A01', NULL, 'GHDs', '02/12/2012', 'Andy' UNION ALL
SELECT 'ABT', 'A02', 'F', 'dfd', '09/05/2012', 'Anna' UNION ALL
SELECT 'ABT', 'A02', NULL, 'sdada', '11/02/2013', 'Kathy' UNION ALL
SELECT 'ABT', 'A02', NULL, 'Dfg', '15/05/2011', 'Rob' UNION ALL
SELECT 'AFL', 'A02', 'F', 'asda', '13/02/2011', 'Dan' UNION ALL
SELECT 'AFL', 'A02', NULL, 'dsaa', '24/12/2010', 'Ryan' UNION ALL
SELECT 'TRG', 'A01', NULL, 'sdasd', '16/04/2010', 'Richard' UNION ALL
SELECT 'TRG', 'K05', NULL, 'jksdh', '23/04/2012', 'Mark' UNION ALL
SELECT 'KLD', 'K05', 'F', 'sd', '18/05/2013', 'Jim' UNION ALL
SELECT 'KLD', 'K05', NULL, 'dsfsd', '10/03/2012', 'James' UNION ALL
SELECT 'KLD', 'K05', NULL, 'sdsd', '12/05/2011', 'Luther' UNION ALL
SELECT 'KTY', 'K05', 'F', 'saq', '09/09/2012', 'Ryan' UNION ALL
SELECT 'KTY', 'K05', NULL, 'asd', '04/04/2010', 'Kathy' UNION ALL
SELECT 'KMD', 'C02', 'F', 'nas', '29/02/2012', 'Rob' UNION ALL
SELECT 'KMD', 'C02', NULL, 'asda', '11/11/2011', 'Andy';
表 2 脚本:
CREATE TABLE Table2
(
ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
Site VARCHAR(5),
Code VARCHAR(5),
Freq int,
StartDate DATETIME,
EndDate DATETIME
);
INSERT INTO Table2 (Site, Code, Freq, StartDate, EndDate)
SELECT 'ABT', 'A01', 43, '01/01/2011', '01/02/2012' UNION ALL
SELECT 'ABT', 'A02', 254, '01/01/2011', '19/02/2011' UNION ALL
SELECT 'ABT', 'A02', 109, '20/02/2011', '01/01/2012' UNION ALL
SELECT 'ABT', 'A02', 12, '02/01/2012', '01/01/2013' UNION ALL
SELECT 'AFL', 'A02', 13, '01/01/2011', '01/02/2012' UNION ALL
SELECT 'TRG', 'A01', 122, '01/01/2011', '01/02/2012' UNION ALL
SELECT 'TRG', 'K05', 61, '01/01/2011', '01/02/2012' UNION ALL
SELECT 'KLD', 'KO5', 33, '01/01/2011', '15/05/2012' UNION ALL
SELECT 'KLD', 'K05', 79, '16/05/2012', '01/01/2013' UNION ALL
SELECT 'KTY', 'K05', 52, '01/01/2011', '01/02/2012' UNION ALL
SELECT 'KMD', 'C02', 78, '01/01/2011', '01/02/2012' UNION ALL
SELECT 'ZYT', 'G01', 11, '01/01/2011', '01/02/2012' UNION ALL
SELECT 'PYN', 'A01', 15, '01/01/2011', '01/02/2012' UNION ALL
SELECT 'DYN', 'F08', 122, '01/01/2011', '01/02/2012';