计数类似描述列和基于设备的总和

问题描述 投票:0回答:2

在SQL Server中,我试图对设备进行分组,然后计算“喜欢”描述以获取出现次数。有没有人做过类似的事情?

Select 
    Equip, Count like Description, 
from
    WorkOrder (nolock) 
where 
    DateTm Between DATEADD(month, DATEDIFF(month, 0, getDate()), 0) and DATEADD(month, DATEDIFF(month, -1, getDate()), -1)
Group by 
    Equip, Description 
order by 
    Equip Asc
sql-server count sql-like
2个回答
0
投票

这应该有效。如果要搜索描述列表,则添加带有描述的where类。

    Select Equip,Description, count(*) as Count, 
                from WorkOrder (nolock) 
    where DateTm Between DATEADD(month, DATEDIFF(month, 0, getDate()), 0) and 
DATEADD(month, DATEDIFF(month, -1, getDate()), -1)
                Group by Equip,Description order by Equip Asc

0
投票

[我认为您想做的是使每个description具有相同的Equip。您可以使用group byhaving子句满足此要求。

select description
    , Equip
    , count(Equip) 
from WorkOrder (nolock) 
where DateTm Between dateadd(month, DATEDIFF(month, 0, getDate()), 0) 
    and dateadd(month, DATEDIFF(month, -1, getDate()), -1)
group by description, Equip 
having count(description) > 1
© www.soinside.com 2019 - 2024. All rights reserved.