Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to write s sql query to get the records like

SQL
Page      Count
1           3
2           6


from
SQL
Page    Count
1	  3
2	  3
2	  6

i.e. max count for page no

SQL
select PageNo,PCount from table where IKey= 12732 AND Pcount in (3,6) group by Pcount,PageNo


What I have tried:

SQL
select PageNo,MAX(PCount) from table where IKey= 12732 AND Pcount in (3,6) group by Pcount,PageNo
Posted
Updated 19-Aug-19 20:22pm
v2
Comments
Maciej Los 20-Aug-19 2:18am    
And what's wrong with your code?
kedar001 20-Aug-19 2:31am    
above query giving result as different count for same page no i.e
Page Count
1 3
2 3
2 6

1 solution

Please, read my comment to the question first.

I think you need something like this:
SQL
SELECT PageNo, MAX(PCount) As PCount
FROM [table]
WHERE IKey= 12732 
GROUP BY PageNo

or
SQL
SELECT PageNo, MAX(PCount) OVER(PARTITION BY PageNo ORDER BY PCount DESC) As PCount
FROM [table]
WHERE IKey= 12732


For further details, please see:
MAX (Transact-SQL) - SQL Server | Microsoft Docs[^]
OVER Clause (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 
Comments
kedar001 20-Aug-19 2:58am    
thanks
Maciej Los 20-Aug-19 3:34am    
You're very welcome.
MadMyche 20-Aug-19 10:19am    
+5
Maciej Los 20-Aug-19 14:00pm    
Thank you.

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