Hi everyone, i worked on Gold Medal Metrics Project:
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