SQL C # Sorting by Maximum Count

Im using 2 tables, Employer and Jobs tables.

The employer table has values ​​such as:

E_ID    e_name
1       john
2       rick
3       mike

      

The table of jobs has such meanings as:

J_ID        FK_eID     J_Title
1           1          Job1
2           1          Job2
3           3          Job3
4           2          Job4
5           3          Job5
6           1          Job6

      

Thus, jobs are created by employers,

What I want is to filter which employer has posted the most jobs and display the e_name in the highest to lowest order in the list ...

But I have no idea how to do this, any help would be appreciated ...

Code im using ... There is no clue about the SQL part.

        SqlConnection myConn2;
        SqlCommand myCommand2;
        SqlDataReader myReader2;
        String SQL2,SQL, divjobs;
        myConn2 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
        divjobs = "<ul>";
        myConn2.Open();
        SQL2 = "";
        myCommand2 = new SqlCommand(SQL2, myConn2);
        myReader2 = myCommand2.ExecuteReader();

        while (myReader2.Read())
        {
            divjobs = divjobs + "<li>" + "<a href='employers/viewemployer.aspx?EID=" + myReader2["e_id"] + "'>" + myReader2["e_name"] + "</a>" + "</li>";
        }
        divjobs = divjobs + "</ul>";
        topemp.InnerHtml = divjobs;
        myConn2.Close();

      

+3


source to share


1 answer


Group by employer and order according to the number of tasks for each of them



select e.e_name, count(j.j_id) as jobs
from employer e
left join jobs j on j.fk_eid = e.e_id
group by e_id, e.e_name
order by count(j.j_id) desc

      

+4


source







All Articles