Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL ORDER BY according to a condition

I am writing some sort of resources management system.

A resource is an instance of a definition. A definition is the metadata, basically it contains the properties.

This is in general my DB:

TypeDefinition
id    name
===============
1     CPU


PropertyDefinition
id    name       typeDefinitionId   valueType
================================================
1     frequency  1                  int
2     status     1                  string


TypeInstance
id    name     typeDefinitionId
=================================
1     CPU#1    1  
2     CPU#2    1


PropertyInstanceValue
id   propertyDefinitionId  typeInstanceId   valueType   intValue  StringValue FloatValue
========================================================================================
1    1                     1                int         10
2    2                     1                string                Pending
3    1                     2                int         20
4    2                     2                string                Approved

REQUIREMENT:

order all resources according to a specific property value.

For example: order all resources according to their status --> Meaning CPU#2 will appear before CPU#1 because “Approved” is before “Pending”.

If we were to order according to frequency, CPU#1 will appear before CPU#2 because 10 is before 20.

So I need to sort each time according to a different column (intValue / stringValue/ FloatValue / etc), depending on the property's valueType.

Any suggestion?

LIMITATION:

PIVOT is currently the only option we've thought of, but it's not really possible since the DB is huge and I need the query to be as fast as possible.

Thanks a lot in advance,

Michal.

like image 919
Michal Avatar asked Oct 18 '12 15:10

Michal


People also ask

How do you put condition in order in SQL?

Syntax. SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2, .. columnN] [ASC | DESC]; You can use more than one column in the ORDER BY clause.

Can I use ORDER BY before the WHERE condition?

ORDER BY Characteristics: The ORDER BY clause is used to get the sorted records on one or more columns in ascending or descending order. The ORDER BY clause must come after the WHERE, GROUP BY, and HAVING clause if present in the query.

How do you use ORDER BY clause with SQL statement?

The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

Can we use ORDER BY in function?

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.


1 Answers

If the problem is that you don't want to dynamically build the query then use this order by structure:

order by case @orderby
    when 'status' then status
    when 'frequency' then frequency
    end
option (recompile)

You will pass the @orderby parameter. The final recompile option is to force the engine to build a new plan according to the passed parameters, that is, assuming you are using a stored procedure.

like image 72
Clodoaldo Neto Avatar answered Sep 28 '22 16:09

Clodoaldo Neto