在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
这应该有效。如果要搜索描述列表,则添加带有描述的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
[我认为您想做的是使每个description
具有相同的Equip
。您可以使用group by
和having
子句满足此要求。
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