如何计算表中的特定值

时间:2017-12-14 21:42:42

标签: sql sql-server count

我在下面的查询中给出了我的下一个结果(下面)。

我想知道如何计算仅在查询结果中出现多次的“MIR_ID”(如果可能的话)。

谢谢!

我的查询:

with Source as (
select 
request_id
,description
,substring(VwDurationIndicator.RequestDetails, 5, 6) as Mir_ID
, request_type

from vortex_hvc.dbo.VwDurationIndicator 
 left join 
 vw_public_request_material_location_mir 
 on substring(VwDurationIndicator.RequestDetails, 5, 6) like vw_public_request_material_location_mir.material_request_id
where DateDiff(hour, CreatedTime, getdate()) < 9 and
 request_type = 'MIR' and SITE = 'HVC' 

        )
 select Source.*

 ,count(Mir_ID) as total
 from Source

 group by Mir_ID,request_id,description,request_type

 order by request_id

查询返回:

request_id  description Mir_ID  request_type    total
152788  VPO: Y7363040RS, Step: PPV-M, Bin: BIN 01   335403  MIR 2
152788  VPO: Y7242580, Step: PPV-S FUSE, Bin: BIN 01    335403  MIR 1
152788  VPO: Y7243570, Step: PPV-S, Bin: BIN 01 335403  MIR 1
152788  VPO: Y7243580, Step: PPV-S, Bin: BIN 01 335403  MIR 1
152788  VPO: Y7322650, Step: PPV, Bin: BIN 01   335403  MIR 5
152788  VPO: Y7363020, Step: PPV-M, Bin: BIN 01 335403  MIR 2
152791  VPO: Y7324440, Step: OLB, Bin: BIN 2571 335412  MIR 1
152791  VPO: Y7324550, Step: OLB, Bin: BIN 2571 335412  MIR 1
152791  VPO: Y7324480, Step: OLB, Bin: BIN 2571 335412  MIR 2
152791  VPO: Y7324590, Step: OLB, Bin: BIN 2571 335412  MIR 1
152791  VPO: Y7324540, Step: OLB, Bin: BIN 2571 335412  MIR 1
152791  VPO: Y7324580, Step: OLB, Bin: BIN 2571 335412  MIR 3
152791  VPO: Y7324430, Step: OLB, Bin: BIN 2571 335412  MIR 1
152791  VPO: Y7324660, Step: OLB, Bin: BIN 2571 335412  MIR 1
152791  VPO: Y7324470, Step: OLB, Bin: BIN 2571 335412  MIR 1
152792  VPO: Y7342740, Step: PRODUCTION BURN-IN, Bin: BIN 01    335419  MIR 1
152792  VPO: Y7342730, Step: PRODUCTION BURN-IN, Bin: BIN 01    335419  MIR 1
152794  VPO: Y7324480, Step: OLB, Bin: BIN 2571 335420  MIR 1
152794  VPO: Y7324540, Step: OLB, Bin: BIN 2571 335420  MIR 1
152794  VPO: Y7324420, Step: OLB, Bin: BIN 2571 335420  MIR 2

1 个答案:

答案 0 :(得分:1)

这应该做的工作:

SELECT MIR_ID, COUNT(*) AS COUNT_MIR_ID
FROM YOUR_RESULT
GROUP BY MIR_ID
HAVING COUNT(*) > 1;

假设YOUR_RESULT包含您在帖子中提供的结果。您可以将结果存储在表中,也可以在原始查询之外使用此查询。