Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6951467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:13:24+00:00 2026-05-27T14:13:24+00:00

I have a stored procedure to return the top 50 rows from a table,

  • 0

I have a stored procedure to return the top 50 rows from a table, like this:

select top 50 
    Puzzle, 
    min(DateSolved) as CreationDate, 
    avg(SecondsToComplete) as AverageTime, 
    count(*) as TimesSolved 
  from CustomSolves
  group by Puzzle

But I want the stored procedure to accept two parameters, so that I can order by a custom column (the creation date, the average time, or the times sovled) and only select records newer than a certain date. Something like this pseudo-SQL:

procedure SelectCustomPuzzles
  @CutoffDate datetime,
  @SortColumn column
as
  select top 50 
      Puzzle, 
      min(DateSolved) as CreationDate, 
      avg(SecondsToComplete) as AverageTime, 
      count(*) as TimesSolved 
    from CustomSolves
    where CreationDate > @CutOffDate
    group by Puzzle
    order by @SortColumn

How could I go about doing something like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:13:25+00:00Added an answer on May 27, 2026 at 2:13 pm
    order by 
       CASE @SortColumn WHEN 'foo ASC' THEN foo ELSE NULL END,
       CASE @SortColumn WHEN 'foo DESC' THEN foo ELSE NULL END DESC,
       CASE @SortColumn WHEN 'bar ASC' THEN bar ELSE NULL END,
       CASE @SortColumn WHEN 'bar DESC' THEN bar ELSE NULL END DESC,
    

    Edit, for a default sort add one of these

    ...
    CASE  WHEN @SortColumn IS NULL THEN default ELSE NULL END DESC
    
    ...
    defautcolumn
    

    Notes:

    • ASC or DESC can’t be in the CASE: has to be outside
    • A single CASE with multiple WHEN..THEN column requires datatypes to be implicitly CASTable. I prefer separate CASE expressions to avoid implicit conversions See https://dba.stackexchange.com/a/4166/630 for a worked example
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Stored procedure which return some fields from a temporary table
I have a stored procedure that returns values from a temp table. In my
Say I have a stored procedure that returns data from a SELECT query. I
I have a stored procedure that returns rows: CREATE PROCEDURE MyProc AS BEGIN SELECT
I have some complex stored procedures that may return many thousands of rows, and
I have a SQL Server 2008 stored procedure that updates values in a table.
I have a simple stored procedure like so: ALTER PROCEDURE [dbo].[spList_Report] @id INT AS
I have a stored procedure that will return a list of ids. I want
I have a stored procedure that return a varbinary(max) type data. I want to
i have this stored procedure to look up uk postcodes.. SET ANSI_NULLS ON GO

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.