Gold Medal Metrics Project: orderedSports query not passing test even when displays the right output

Hi everyone, i worked on Gold Medal Metrics Project:

https://www.codecademy.com/paths/web-development/tracks/sql-for-web-development/modules/project-gold-medal-metrics/informationals/bapi-p5-gold-medal-metrics

and for the bonus function query, i made my own query and it displays the right results comparing it with the query on the resolved project, yet… i was not able to pass the tests

My code

const orderedSports = (country, field, sortAscending) => {
  if (field){
    if(sortAscending){
      const query = `WITH Total AS (
        SELECT COUNT (*) AS totalMedals, country
        FROM GoldMedal
        Where country = ${country}
        )
        SELECT GoldMedal.sport, COUNT (*) AS count , ((COUNT (*) * 100)/ Total.totalMedals) AS percent
        FROM GoldMedal
        JOIN Total
          ON GoldMedal.country = Total.country
          WHERE GoldMedal.country = ${country}
        GROUP BY 1
        ORDER BY GoldMedal.${field} ASC;`
      return query;
    }else{
      const query = `WITH Total AS (
        SELECT COUNT (*) AS totalMedals, country
        FROM GoldMedal
        Where country = ${country}
        )
        SELECT GoldMedal.sport, COUNT (*) AS count , ((COUNT (*) * 100)/ Total.totalMedals) AS percent
        FROM GoldMedal
        JOIN Total
          ON GoldMedal.country = Total.country
          WHERE GoldMedal.country = ${country}
        GROUP BY 1
        ORDER BY GoldMedal.${field} DESC;`
      return query;
    }
  }else{
    if(sortAscending){
      const query = `WITH Total AS (
        SELECT COUNT (*) AS totalMedals, country
        FROM GoldMedal
        Where country = '${country}'
        )
        SELECT GoldMedal.sport, COUNT (*) AS count , ((COUNT (*) * 100)/ Total.totalMedals) AS percent
        FROM GoldMedal
        JOIN Total
          ON GoldMedal.country = Total.country
          WHERE GoldMedal.country = '${country}'
        GROUP BY 1;`
      return query;
    }else{
      const query = `WITH Total AS (
        SELECT COUNT (*) AS totalMedals, country
        FROM GoldMedal
        Where country = '${country}'
        )
        SELECT GoldMedal.sport, COUNT (*) AS count , ((COUNT (*) * 100)/ Total.totalMedals) AS percent
        FROM GoldMedal
        JOIN Total
          ON GoldMedal.country = Total.country
          WHERE GoldMedal.country = '${country}'
        GROUP BY 1;`
      return query;
    }
  } 

Project resolved code

const orderedSports = (country, field, isAscending) => {
  let orderingString = '';
  if (field) {
    if (isAscending) {
      orderingString = `ORDER BY ${field} ASC`;
    } else {
      orderingString = `ORDER BY ${field} DESC`;
    }
  }
  return `SELECT sport, COUNT(sport) AS count, (COUNT(sport) * 100 / (select COUNT(*) FROM GoldMedal WHERE country = '${country}')) AS percent FROM GoldMedal WHERE country = '${country}' GROUP BY sport ${orderingString};`;
};

DB browser (i replaced the variables with values to make it work)
My results

Project resolved code

even on the Index.html it is being displayed correctly:

Failed tests

from my point of view my query is right… but there must be reason why it is not being accepted… and i would like to know it, can someone help me? please!

thanks

I have the same issue with mostSummerWins and mostWinterWins, which is as far as I have come before searching for answers…I am getting the right output and failing the tests.

There must be a problem with your query, try adding logs to those 3 tests in test.js file to see how your queries looks like in action.


It helped me to solve this and previous, orderedMedals, function.
Here’s my solution, it passes the tests:

const orderedSports = (country, field, sortAscending) => {
	let orderQuery;
	if (sortAscending) {
		orderQuery = `ORDER BY ${field} ASC`;
	} else if (sortAscending === undefined) {
		orderQuery = '';
	} else {
		orderQuery = `ORDER BY ${field} DESC`;
	}
	return `WITH sum_query AS (SELECT Country, count(*) as suma FROM goldmedal GROUP by 1)
  SELECT goldmedal.Sport, count(*) as count, 
   round(count(*)*100/ CAST(sum_query.suma as REAL),1) as percent
   FROM goldmedal  
  JOIN sum_query ON sum_query.Country = goldmedal.Country
  WHERE goldmedal.country='${country}'
  GROUP BY 1
  ${orderQuery};`;
};