Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi All, Need som help in subqueries in sql. i have a table of data where we have 3 fields empno name status. in status we have 5 different status and each empno will have all the 5 status. i need a query which will count the unique numbers in the table and if the status is in the last stage it should not show the count in previous stage.in the report so need help in this queryRegards,Lokesh Sundar
sec
Starting Member
4 Posts
Posted - 2010-07-21 : 14:21:42
1> Use a subselect to return a table of empnos with the maximum status (the status name will have to be hierachical so that the 'maximum' can be identified; maybe add statlevel)2> Get a count of these recordsNote: An alias for the subselected table and columns are requiredSELECT tbl.maxstat, COUNT(*) FROM ( SELECT emp.empno, MAX(emp.status) AS maxstat FROM tblEmpInfo emp GROUP BY emp.empno) tblGROUP BY tbl.maxstatHope this helps give you a directionsecLet God be true and every man a liar