我在SQL服务器中有如下表,我需要一个查询来给我提供输出,检查所有EmpID,其中EmpID等于check EmployeeType,其中Empty中的EmployeeType等于给我第一行,该行具有min StartAction 如果某些EmpID中的EmployeeType更改也给我该行
这是我的示例表,我的记录超过200,000
EmpID Serial EmpType SAction
-------------------- ----------- ------------ -----------
10012845 2 5 940705
10012845 3 5 951218
10012845 4 5 960130
10012845 5 5 970419
10012845 6 5 970419
13395353 1 5 921113
13395353 2 5 931116
13395353 3 2 941119
13395353 4 1 951214
13395353 5 1 961713
10013597 1 5 890201
10013597 2 53 921003
10013597 3 53 960320
10013597 4 53 970419
10013597 5 53 970419
我的愿望输出
带有** {description}的行是我想要的行
EmpID Serial EmpType SAction
-------------------- ----------- ------------ -----------
10012845 2 5 940705** same empid and first EmpType and min SAction
10012845 3 5 951218
10012845 4 5 960130
10012845 5 5 970419
10012845 6 5 970419
13395353 1 5 921113** same empid and first EmpType and min SAction
13395353 2 5 931116
13395353 3 2 941119**same empid and different EmpType and min SAction
13395353 4 1 951214**same empid and different EmpType and min SAction
13395353 5 1 961713
10013597 1 5 890201**same empid and first EmpType and min SAction
10013597 2 53 921003**same empid and different EmpType and min SAction
10013597 3 53 960320
10013597 4 53 970419
10013597 5 53 970419
更简单的愿望输出
EmpID Serial EmpType SAction
-------------------- ----------- ------------ -----------
10012845 2 5 940705
13395353 1 5 921113
13395353 3 2 941119
13395353 4 1 951214
10013597 1 5 890201
10013597 2 53 921003
我必须将每个EmpId相互比较以找到第一个EmpId,如果存在Empid等于其他Empid,则获取他们的Empty,并获取该Empid组中彼此相等的所有Empty的离散值,并获得具有min行动。我不知道!!!!
答案 0 :(得分:-1)
我创建了额外的字段来添加您的评论。我已在评论字段中应用了两个UPDATE
。第一个查找第一个EmpId,EmpType,Saction组合。
第二个查询是相同的查询,但是从第一个查询中排除了emptype。
--DROP TABLE #Temp
--GO
CREATE TABLE #Temp
( EmpID INT
,Serial INT
,EmpType INT
,SAction INT
,Comment VARCHAR(MAX)
)
INSERT INTO #Temp(EmpID,Serial,EmpType,SAction)
VALUES
(10012845, 2 , 5 , 940705)
,(10012845, 3 , 5 , 951218)
,(10012845, 4 , 5 , 960130)
,(10012845, 5 , 5 , 970419)
,(10012845, 6 , 5 , 970419)
,(13395353, 1 , 5 , 921113)
,(13395353, 2 , 5 , 931116)
,(13395353, 3 , 2 , 941119)
,(13395353, 4 , 1 , 951214)
,(13395353, 5 , 1 , 961713)
,(10013597, 1 , 5 , 890201)
,(10013597, 2 , 53 , 921003)
,(10013597, 3 , 53 , 960320)
,(10013597, 4 , 53 , 970419)
,(10013597, 5 , 53 , 970419)
UPDATE #Temp SET Comment = '** same empid and first EmpType and min SAction'
FROM (SELECT EmpID
,MIN(Serial) Serial
,MIN(SAction) Saction
FROM #Temp
GROUP BY EmpID) S
WHERE #Temp.EmpID = S.EmpID
AND #Temp.Serial = S.Serial
AND #Temp.SAction = S.Saction
UPDATE #Temp SET Comment = '**same empid and different EmpType and min SAction'
FROM (SELECT T1.EmpID
,T1.EmpType
,MIN(T1.Serial) Serial
,MIN(T1.SAction) Saction
FROM #Temp T1
GROUP BY T1.EmpID,T1.EmpType) S
WHERE #Temp.EmpID = S.EmpID
AND #Temp.Serial = S.Serial
AND #Temp.SAction = S.Saction
AND #Temp.Comment IS NULL
--AND NOT EXISTS (SELECT 1
-- FROM #Temp T2
-- where #Temp.EmpID = t2.EmpID
-- and T2.EmpType=#Temp.EmpType
-- )
select * from #Temp