![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
Hello, Jet sql (Access Jet) does not have the capability to perform such a query in one shot -- that would be transact sql (for sql server). However, there is a workaround. Quick note: Access is a file based miniature RDBMS (as opposed to sql server which is server based). As such - it can only perform 10-30% of the operations of a server based RDBMS right out of the box. The power of Access lay in the workarounds. The workarounds could be a little bit tedious but at least they are available. So here is your workaround: Sub getTopTwoEmpsFromEachDept() Dim DB As DAO.Database, RS As DAO.Recordset Dim str1 As String, str2 As String DoCmd.SetWarnings False Set DB = CurrentDB Set RS = DB.OpenRecordset("Select DeptName From Emp_Master Group By DeptName Order By DeptName") Do While Not RS.EOF str1 = RS(0) DoCmd.RunSql "Insert Into tmp1 Select Top 2 Emp_Code, DeptName From Emp_Master Where DeptName = '" & str1 & "' Order By Emp_Code" RS.MoveNext Loop DoCmd.SetWarnings True End Sub This solution would require that you create a temporary table which I called tmp1 which will contain the Emp_Code field and the DeptName field. You loop through your master table and pick up each distinct DeptName and then select the top two Emp_Codes from each Dept and store these values in tmp1. Here is the Tsql (transact Sql) version of this workaround select * from Emp_Master t1 Where Exists (Select * from (Select Top 2 Emp_Code, DeptName From Emp_Master t2 Where t2.DeptName = t1.DeptName Order By DeptName) t3 Where t3.Emp_Code = t1.Emp_Code This query assumes that Emp_Code is a unique field. This query is less busy than the workaround above, but I have not been able to run this type of query in Access. Thus, the workaround. HTH Rich *** Sent via Developersdex http://www.developersdex.com *** |
#4
| |||
| |||
|
|
Hello Sir, I am having *problem in formulating a SQL query . Please help in finding out the solution: I need a SQL query in whichi want to select N number *of employee from table Training_emp *such that ----> There are at max 2 emp from any department MyTableStructure is: Training_emp: * * Emp_code * * Topic * * Session * * Month * * Inn_add * * Attend * * Date Emp_master: * * * Emp_code * * *DeptName now what i want is..to select say 20 *employees from Training_emp table.... suct that no more that two employeees from same departnment are selected My *Back end is MS access |
![]() |
| Thread Tools | |
| Display Modes | |
| |