Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 1 character in body
Source Link

I'm experiencing some issues with the result set I'm getting from my laravelLaravel query

The Query

 $collegecounts = DB::table('colleges as cs') ->select(DB::raw('count(college_id) as graduates'),'cs.id','cs.collegename', DB::raw('round(AVG(st.status_id),0) as average'), DB::raw("(CASE WHEN (st.status_id = 4) THEN count(st.college_id) END) as placements") ) ->join('students as st', 'st.college_id', '=', 'cs.id') ->groupBy('st.college_id', 'cs.id', 'cs.collegename') ->get(); 

Expected results are similar to the table below: (Placements column on the table below is a status_id count)

enter image description here

SQL Table:

The Student Table | Column | | ----------| | id | | Name | | College_id| | Status_id |

Column
id
Name
College_id
Status_id

The College Table

Column
id
Name
location

The status is found in the status table. If I don't add the status_id in the groupby query, I get an error that requires me to add status_id to groupby. Upon adding it to Groupby, then I get more columns, for each student, instead of the grouping by college. I also tried using having instead of the case condition I get the same result

The error:

SQLSTATE[42803]: Grouping error: 7 ERROR: column "st.status_id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...ound(AVG(st.status_id),0) as average, (CASE WHEN (st.status_... ^

I can easily add st.status_id to the group by  , however that doesn't get me the correct result

I'm experiencing some issues with the result set I'm getting from my laravel query

The Query

 $collegecounts = DB::table('colleges as cs') ->select(DB::raw('count(college_id) as graduates'),'cs.id','cs.collegename', DB::raw('round(AVG(st.status_id),0) as average'), DB::raw("(CASE WHEN (st.status_id = 4) THEN count(st.college_id) END) as placements") ) ->join('students as st', 'st.college_id', '=', 'cs.id') ->groupBy('st.college_id', 'cs.id', 'cs.collegename') ->get(); 

Expected results are similar to the table below: (Placements column on the table below is a status_id count)

enter image description here

SQL Table:

The Student Table | Column | | ----------| | id | | Name | | College_id| | Status_id |

The College Table

Column
id
Name
location

The status is found in the status table. If I don't add the status_id in the groupby query, I get an error that requires me to add status_id to groupby. Upon adding it to Groupby, then I get more columns, for each student, instead of the grouping by college. I also tried using having instead of the case condition I get the same result

The error:

SQLSTATE[42803]: Grouping error: 7 ERROR: column "st.status_id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...ound(AVG(st.status_id),0) as average, (CASE WHEN (st.status_... ^

I can easily add st.status_id to the group by  , however that doesn't get me the correct result

I'm experiencing some issues with the result set I'm getting from my Laravel query

The Query

 $collegecounts = DB::table('colleges as cs') ->select(DB::raw('count(college_id) as graduates'),'cs.id','cs.collegename', DB::raw('round(AVG(st.status_id),0) as average'), DB::raw("(CASE WHEN (st.status_id = 4) THEN count(st.college_id) END) as placements") ) ->join('students as st', 'st.college_id', '=', 'cs.id') ->groupBy('st.college_id', 'cs.id', 'cs.collegename') ->get(); 

Expected results are similar to the table below: (Placements column on the table below is a status_id count)

enter image description here

SQL Table:

The Student Table

Column
id
Name
College_id
Status_id

The College Table

Column
id
Name
location

The status is found in the status table. If I don't add the status_id in the groupby query, I get an error that requires me to add status_id to groupby. Upon adding it to Groupby, then I get more columns, for each student, instead of the grouping by college. I also tried using having instead of the case condition I get the same result

The error:

SQLSTATE[42803]: Grouping error: 7 ERROR: column "st.status_id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...ound(AVG(st.status_id),0) as average, (CASE WHEN (st.status_... ^

I can easily add st.status_id to the group by, however that doesn't get me the correct result

added 2 characters in body
Source Link
kya
  • 1.8k
  • 9
  • 38
  • 65

I'm experiencing some issues with the result set I'm getting from my laravel query

The Query

 $collegecounts = DB::table('colleges as cs') ->select(DB::raw('count(college_id) as graduates'),'cs.id','cs.collegename', DB::raw('round(AVG(st.status_id),0) as average'), DB::raw("(CASE WHEN (st.status_id = 4) THEN count(st.college_id) END) as placements") ) ->join('students as st', 'st.college_id', '=', 'cs.id') ->groupBy('st.college_id', 'cs.id', 'cs.collegename') ->get(); 

Expected results are similar to the table below: (Placements column on the table below is a status_id count)

enter image description here

SQL Table:

The Student Table | Column | | ----------| | id | | Name | | College_id| | Status_id |

The College Table

Column
id
Name
location

The status is found in the status table. If I don't add the status_id in the groupby query, I get an error that requires me to add status_id to groupby. Upon adding it to Groupby, then I get more columns, for each student, instead of the grouping by college. I also tried using having instead of the case condition I get the same result

The error:

SQLSTATE[42803]: Grouping error: 7 ERROR: column "st.status_id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...ound(AVG(st.status_id),0) as average, (CASE WHEN (st.status_... ^

I can easily add st.status_id to the group by , however that doesn't get me the correct result

I'm experiencing some issues with the result set I'm getting from my laravel query

The Query

 $collegecounts = DB::table('colleges as cs') ->select(DB::raw('count(college_id) as graduates'),'cs.id','cs.collegename', DB::raw('round(AVG(st.status_id),0) as average'), DB::raw("(CASE WHEN (st.status_id = 4) THEN count(st.college_id) END) as placements") ) ->join('students as st', 'st.college_id', '=', 'cs.id') ->groupBy('st.college_id', 'cs.id', 'cs.collegename') ->get(); 

Expected results are similar to the table below: (Placements column on the table below is a status_id count)

enter image description here

SQL Table:

The Student Table | Column | | ----------| | id | | Name | | College_id| | Status_id |

The College Table

Column
id
Name
location

The status is found in the status table. If I don't add the status_id in the groupby query, I get an error that requires me to add status_id to groupby. Upon adding it to Groupby, then I get more columns, for each student, instead of the grouping by college. I also tried using having instead of the case condition I get the same result

The error:

SQLSTATE[42803]: Grouping error: 7 ERROR: column "st.status_id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...ound(AVG(st.status_id),0) as average, (CASE WHEN (st.status_... ^

I can easily add st.status_id to the group by , however that doesn't get me the correct result

I'm experiencing some issues with the result set I'm getting from my laravel query

The Query

 $collegecounts = DB::table('colleges as cs') ->select(DB::raw('count(college_id) as graduates'),'cs.id','cs.collegename', DB::raw('round(AVG(st.status_id),0) as average'), DB::raw("(CASE WHEN (st.status_id = 4) THEN count(st.college_id) END) as placements") ) ->join('students as st', 'st.college_id', '=', 'cs.id') ->groupBy('st.college_id', 'cs.id', 'cs.collegename') ->get(); 

Expected results are similar to the table below: (Placements column on the table below is a status_id count)

enter image description here

SQL Table:

The Student Table | Column | | ----------| | id | | Name | | College_id| | Status_id |

The College Table

Column
id
Name
location

The status is found in the status table. If I don't add the status_id in the groupby query, I get an error that requires me to add status_id to groupby. Upon adding it to Groupby, then I get more columns, for each student, instead of the grouping by college. I also tried using having instead of the case condition I get the same result

The error:

SQLSTATE[42803]: Grouping error: 7 ERROR: column "st.status_id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...ound(AVG(st.status_id),0) as average, (CASE WHEN (st.status_... ^

I can easily add st.status_id to the group by , however that doesn't get me the correct result

Source Link
kya
  • 1.8k
  • 9
  • 38
  • 65

Count based on condition using query builder not working

I'm experiencing some issues with the result set I'm getting from my laravel query

The Query

 $collegecounts = DB::table('colleges as cs') ->select(DB::raw('count(college_id) as graduates'),'cs.id','cs.collegename', DB::raw('round(AVG(st.status_id),0) as average'), DB::raw("(CASE WHEN (st.status_id = 4) THEN count(st.college_id) END) as placements") ) ->join('students as st', 'st.college_id', '=', 'cs.id') ->groupBy('st.college_id', 'cs.id', 'cs.collegename') ->get(); 

Expected results are similar to the table below: (Placements column on the table below is a status_id count)

enter image description here

SQL Table:

The Student Table | Column | | ----------| | id | | Name | | College_id| | Status_id |

The College Table

Column
id
Name
location

The status is found in the status table. If I don't add the status_id in the groupby query, I get an error that requires me to add status_id to groupby. Upon adding it to Groupby, then I get more columns, for each student, instead of the grouping by college. I also tried using having instead of the case condition I get the same result

The error:

SQLSTATE[42803]: Grouping error: 7 ERROR: column "st.status_id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...ound(AVG(st.status_id),0) as average, (CASE WHEN (st.status_... ^

I can easily add st.status_id to the group by , however that doesn't get me the correct result