Need to list countries by top 25 in a category with SQL





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to list countries by top 25 in a specific category and year in sql but I am having trouble figuring out how to do that.



For example I have the categories Country and economic freedom. I want to list the 25 countries in economic freedom in the year 2016. How would I do that?



Countries is a varchar and Economic freedom is also varchar ex.(3.60)
This is for Microsoft Sql Server.










share|improve this question




















  • 5





    Give sample data and your expected result..

    – dwir182
    Nov 23 '18 at 19:14











  • You can use TOP(@count) -When TOP is used in conjunction with the ORDER BY clause, the result set is limited to the first N number of ordered rows;

    – Indrakumara
    Nov 23 '18 at 19:36











  • Shouldn't economic freedom be numeric for 1-25 ranking?

    – Parfait
    Nov 23 '18 at 22:00




















0















I am trying to list countries by top 25 in a specific category and year in sql but I am having trouble figuring out how to do that.



For example I have the categories Country and economic freedom. I want to list the 25 countries in economic freedom in the year 2016. How would I do that?



Countries is a varchar and Economic freedom is also varchar ex.(3.60)
This is for Microsoft Sql Server.










share|improve this question




















  • 5





    Give sample data and your expected result..

    – dwir182
    Nov 23 '18 at 19:14











  • You can use TOP(@count) -When TOP is used in conjunction with the ORDER BY clause, the result set is limited to the first N number of ordered rows;

    – Indrakumara
    Nov 23 '18 at 19:36











  • Shouldn't economic freedom be numeric for 1-25 ranking?

    – Parfait
    Nov 23 '18 at 22:00
















0












0








0








I am trying to list countries by top 25 in a specific category and year in sql but I am having trouble figuring out how to do that.



For example I have the categories Country and economic freedom. I want to list the 25 countries in economic freedom in the year 2016. How would I do that?



Countries is a varchar and Economic freedom is also varchar ex.(3.60)
This is for Microsoft Sql Server.










share|improve this question
















I am trying to list countries by top 25 in a specific category and year in sql but I am having trouble figuring out how to do that.



For example I have the categories Country and economic freedom. I want to list the 25 countries in economic freedom in the year 2016. How would I do that?



Countries is a varchar and Economic freedom is also varchar ex.(3.60)
This is for Microsoft Sql Server.







sql sql-server list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 21:06









marc_s

585k13011251272




585k13011251272










asked Nov 23 '18 at 19:11









JoSJoS

32




32








  • 5





    Give sample data and your expected result..

    – dwir182
    Nov 23 '18 at 19:14











  • You can use TOP(@count) -When TOP is used in conjunction with the ORDER BY clause, the result set is limited to the first N number of ordered rows;

    – Indrakumara
    Nov 23 '18 at 19:36











  • Shouldn't economic freedom be numeric for 1-25 ranking?

    – Parfait
    Nov 23 '18 at 22:00
















  • 5





    Give sample data and your expected result..

    – dwir182
    Nov 23 '18 at 19:14











  • You can use TOP(@count) -When TOP is used in conjunction with the ORDER BY clause, the result set is limited to the first N number of ordered rows;

    – Indrakumara
    Nov 23 '18 at 19:36











  • Shouldn't economic freedom be numeric for 1-25 ranking?

    – Parfait
    Nov 23 '18 at 22:00










5




5





Give sample data and your expected result..

– dwir182
Nov 23 '18 at 19:14





Give sample data and your expected result..

– dwir182
Nov 23 '18 at 19:14













You can use TOP(@count) -When TOP is used in conjunction with the ORDER BY clause, the result set is limited to the first N number of ordered rows;

– Indrakumara
Nov 23 '18 at 19:36





You can use TOP(@count) -When TOP is used in conjunction with the ORDER BY clause, the result set is limited to the first N number of ordered rows;

– Indrakumara
Nov 23 '18 at 19:36













Shouldn't economic freedom be numeric for 1-25 ranking?

– Parfait
Nov 23 '18 at 22:00







Shouldn't economic freedom be numeric for 1-25 ranking?

– Parfait
Nov 23 '18 at 22:00














2 Answers
2






active

oldest

votes


















0














Sounds like you need a regular SELECT TOP with an ORDER BY clause. Please beware that you need to convert Economic freedom to a numeric data type, otherwise 4 > 10, because 4 > 1. Given provided information, this should be your query, if Year is a column as well.



SELECT TOP 25 [Country], [Economic freedom]
FROM Table
WHERE [Year] = 2016
ORDER BY CAST([Economic freedom] as float) DESC





share|improve this answer
























  • Why use a float?

    – NicVerAZ
    Nov 23 '18 at 22:17











  • It's just my goto numeric data type when not knowing the number of decimals etc. We just need [Economic freedom] to be numeric for correct sorting.

    – Erik Blomgren
    Nov 24 '18 at 18:42



















0














Select EconomicFreedom,count(*) from table group by EconomicFreedom having count(*) <=25 order by Countries desc;




Group by is the major step







share|improve this answer


























  • Might be I missed it thanks for reminding

    – Himanshu Ahuja
    Nov 23 '18 at 22:00












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451896%2fneed-to-list-countries-by-top-25-in-a-category-with-sql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Sounds like you need a regular SELECT TOP with an ORDER BY clause. Please beware that you need to convert Economic freedom to a numeric data type, otherwise 4 > 10, because 4 > 1. Given provided information, this should be your query, if Year is a column as well.



SELECT TOP 25 [Country], [Economic freedom]
FROM Table
WHERE [Year] = 2016
ORDER BY CAST([Economic freedom] as float) DESC





share|improve this answer
























  • Why use a float?

    – NicVerAZ
    Nov 23 '18 at 22:17











  • It's just my goto numeric data type when not knowing the number of decimals etc. We just need [Economic freedom] to be numeric for correct sorting.

    – Erik Blomgren
    Nov 24 '18 at 18:42
















0














Sounds like you need a regular SELECT TOP with an ORDER BY clause. Please beware that you need to convert Economic freedom to a numeric data type, otherwise 4 > 10, because 4 > 1. Given provided information, this should be your query, if Year is a column as well.



SELECT TOP 25 [Country], [Economic freedom]
FROM Table
WHERE [Year] = 2016
ORDER BY CAST([Economic freedom] as float) DESC





share|improve this answer
























  • Why use a float?

    – NicVerAZ
    Nov 23 '18 at 22:17











  • It's just my goto numeric data type when not knowing the number of decimals etc. We just need [Economic freedom] to be numeric for correct sorting.

    – Erik Blomgren
    Nov 24 '18 at 18:42














0












0








0







Sounds like you need a regular SELECT TOP with an ORDER BY clause. Please beware that you need to convert Economic freedom to a numeric data type, otherwise 4 > 10, because 4 > 1. Given provided information, this should be your query, if Year is a column as well.



SELECT TOP 25 [Country], [Economic freedom]
FROM Table
WHERE [Year] = 2016
ORDER BY CAST([Economic freedom] as float) DESC





share|improve this answer













Sounds like you need a regular SELECT TOP with an ORDER BY clause. Please beware that you need to convert Economic freedom to a numeric data type, otherwise 4 > 10, because 4 > 1. Given provided information, this should be your query, if Year is a column as well.



SELECT TOP 25 [Country], [Economic freedom]
FROM Table
WHERE [Year] = 2016
ORDER BY CAST([Economic freedom] as float) DESC






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 21:26









Erik BlomgrenErik Blomgren

53226




53226













  • Why use a float?

    – NicVerAZ
    Nov 23 '18 at 22:17











  • It's just my goto numeric data type when not knowing the number of decimals etc. We just need [Economic freedom] to be numeric for correct sorting.

    – Erik Blomgren
    Nov 24 '18 at 18:42



















  • Why use a float?

    – NicVerAZ
    Nov 23 '18 at 22:17











  • It's just my goto numeric data type when not knowing the number of decimals etc. We just need [Economic freedom] to be numeric for correct sorting.

    – Erik Blomgren
    Nov 24 '18 at 18:42

















Why use a float?

– NicVerAZ
Nov 23 '18 at 22:17





Why use a float?

– NicVerAZ
Nov 23 '18 at 22:17













It's just my goto numeric data type when not knowing the number of decimals etc. We just need [Economic freedom] to be numeric for correct sorting.

– Erik Blomgren
Nov 24 '18 at 18:42





It's just my goto numeric data type when not knowing the number of decimals etc. We just need [Economic freedom] to be numeric for correct sorting.

– Erik Blomgren
Nov 24 '18 at 18:42













0














Select EconomicFreedom,count(*) from table group by EconomicFreedom having count(*) <=25 order by Countries desc;




Group by is the major step







share|improve this answer


























  • Might be I missed it thanks for reminding

    – Himanshu Ahuja
    Nov 23 '18 at 22:00
















0














Select EconomicFreedom,count(*) from table group by EconomicFreedom having count(*) <=25 order by Countries desc;




Group by is the major step







share|improve this answer


























  • Might be I missed it thanks for reminding

    – Himanshu Ahuja
    Nov 23 '18 at 22:00














0












0








0







Select EconomicFreedom,count(*) from table group by EconomicFreedom having count(*) <=25 order by Countries desc;




Group by is the major step







share|improve this answer















Select EconomicFreedom,count(*) from table group by EconomicFreedom having count(*) <=25 order by Countries desc;




Group by is the major step








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 21:59

























answered Nov 23 '18 at 19:41









Himanshu AhujaHimanshu Ahuja

1,0102219




1,0102219













  • Might be I missed it thanks for reminding

    – Himanshu Ahuja
    Nov 23 '18 at 22:00



















  • Might be I missed it thanks for reminding

    – Himanshu Ahuja
    Nov 23 '18 at 22:00

















Might be I missed it thanks for reminding

– Himanshu Ahuja
Nov 23 '18 at 22:00





Might be I missed it thanks for reminding

– Himanshu Ahuja
Nov 23 '18 at 22:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451896%2fneed-to-list-countries-by-top-25-in-a-category-with-sql%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()