本文介绍了Sql查询获取每行更改行值的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在SQLserver数据库中有以下表格 我想获得RAM更改的时间 - --------------------- sr Machinename RAM - ------------------ - 1 。 Comp-1 20 2 。 Comp-2 40 3 。 Comp-1 15 4 。 Comp-1 10 5 。 Comp-2 40 6 。 Comp-2 30 - - ------------------ 我想要关注输出 - ----------------- ---------------------- sr span class =code-keyword> time RAM已更改 - ---- ----------------------------------- 1 。 Comp-1 3 2 。 Comp-2 2 - - ------------------------------------- 注意:Comp-2有3行,但1行具有相同的RAM值,因此不计入更改值 我有什么试过: WITH CTE AS( SELECT ROW_NUMBER()OVER(按名称分区ORDER BY RAM desc) as rowid,Compname,RAM FROM tblA) select * from cte 解决方案 取决于你的意思通过获取RAM已更改的时间... 试试这个: SELECT Machinename,COUNT(RAM) AS CountOfRAM FROM YourTable GROUP BY 计算机名 或: SELECT Machinename,COUNT( DISTINCT RAM) AS CountOfRAM FROM YourTable GROUP BY 计算机名 I have following table in SQLserver databaseI want to get number of time RAM has changed -----------------------sr Machinename RAM----------------------1. Comp-1 202. Comp-2 403. Comp-1 154. Comp-1 105. Comp-2 406. Comp-2 30 ----------------------I want following output-----------------------------------------sr Machinename No.of time RAM changed-----------------------------------------1. Comp-1 32. Comp-2 2 -----------------------------------------NOTE:Comp-2 has 3 rows but 1 row having same RAM value so its not counted as changed valueWhat I have tried:WITH CTE AS( SELECT ROW_NUMBER() OVER(PARTITION BY Compname ORDER BY RAM desc) as rowid,Compname, RAMFROM tblA) select * from cte 解决方案 Depending on what you mean by "to get number of time RAM has changed"...Try this:SELECT Machinename, COUNT(RAM) AS CountOfRAMFROM YourTableGROUP BY Machinenameor:SELECT Machinename, COUNT(DISTINCT RAM) AS CountOfRAMFROM YourTableGROUP BY Machinename 这篇关于Sql查询获取每行更改行值的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 15:38