Click here to Skip to main content
15,912,457 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following table in SQLserver database
I want to get number of time RAM has changed
SQL
-----------------------
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 
----------------------

I want following output
SQL
-----------------------------------------
sr  Machinename   No.of time RAM changed
-----------------------------------------
1.  Comp-1        3
2.  Comp-2        2 
-----------------------------------------

NOTE:Comp-2 has 3 rows but 1 row having same RAM value so its not counted as changed value

What I have tried:

WITH CTE AS(
  SELECT ROW_NUMBER() OVER(PARTITION BY Compname ORDER BY RAM desc) as rowid,Compname, RAM
	FROM tblA) 
	select * from cte
Posted
Updated 24-May-19 3:36am

1 solution

Depending on what you mean by "to get number of time RAM has changed"...

Try this:
SQL
SELECT Machinename,  COUNT(RAM) AS CountOfRAM
FROM YourTable
GROUP BY Machinename


or:
SQL
SELECT Machinename, COUNT(DISTINCT RAM) AS CountOfRAM
FROM YourTable
GROUP BY Machinename
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900