.Net Questions and Ans.. Q. 1 Querying a Data Table Using Select Method and Lambda Expressions in Dot Net Suppose we have a DataTable object having four fields: SSN, NAME, ADDRESS and AGE. We could create a data table and add columns in the following way: DataTable dt = new DataTable (); dt.Columns.Add( "SSN" , typeof ( string )); dt.Columns.Add( "NAME" , typeof ( string )); dt.Columns.Add( "ADDR" , typeof ( string )); dt.Columns.Add( "AGE" , typeof ( int )); // Showing how to set Primary Key(s) in a Data table (Although it's not compulsory to have one) DataColumn [] keys = new DataColumn [1]; keys[0] = dt.Columns[0]; dt.PrimaryKey = keys; Now we store some data in our data table "dt" to show how we can perform several queries on the DataTable object like filtering some data, finding a person's record...
---------Using Sub Query 3rd Highest Salary All Employees--------- SELECT * FROM Emp WHERE Salary IN( SELECT TOP 1 salary FROM ( SELECT TOP 3 salary FROM Emp e ORDER BY Salary DESC ) a ORDER BY a.Salary ) ------------------- 3rd highest salary----------- -----3rd Hightest salary---------- DECLARE @n INT=3 SELECT DISTINCT(Salary) from emp e1 where @n=( SELECT COUNT(DISTINCT(salary)) from emp e2 where e2.salary>=e1.salary ) --------------3rd Highest Salary----------- ;WITH cte AS ( SELECT Id,Name,e.Salary, DENSE_RANK() OVER (PARTITION BY e.Salary ORDER BY e.Salary) AS sal FROM Emp e ) SELECT * FROM cte WHERE sal=2 ----------------------- DECLARE @N int SET @N = 3 -- Change the value here to pick a different salary rank SELECT Salary FROM ( SELECT row_number() OVER (ORDER BY Salary DESC) as SalaryRank, Salary FROM Emp e ) as SalaryCTE WHERE...
good
ReplyDelete