How can I order manipulated data that I store in a variable in SQL?

Jtara

I have a variable @columns that outputs a set of decimals. For example, [0.00000], [1.00000], [4.00000], [3.00000]. These later get used as columns in one of my queries. However the decimals don't get put in any particular order. I would like to order them from least to greatest in SQL. I don't see a function that does this.

How can I manipulate the data to do that?

SELECT
    @COLUMNS += QUOTENAME(ct.rate) + ','
FROM
(
SELECT DISTINCT ct.rate FROM
DB.table ct
WHERE ct.ID = @ID
) ct

SET @COLUMNS = LEFT(@COLUMNS, LEN(@COLUMNS) - 1);

PRINT @COLUMNS```

Leandro Bardelli

If you order the data before the array, you will get the output ordered from least to greatest. As you can't use order inside subquery, you can select a TOP MAX(INT) to subquery it.

SELECT
    @COLUMNS += QUOTENAME(ct1.rate) + ','
FROM
(
SELECT DISTINCT  TOP 2147483647 ct.rate FROM
DB.table ct
WHERE ct.ID = @ID
ORDER BY ct.rate
) ct1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I store a sql query into a variable, not the result?

How can I add a sequence / order a data in SQL

How can I store the result of a function into a variable?

How can I store dataset() in Application variable?

How can I store a variable in a list number?

How can I store a pattern in a variable in Rust?

How can I store my variable in a file?

How Can I Store a Wildcard in a Variable

How can I store in a variable the Firestore document data what I read from the database?

How can i store encrypted data

How can I store data locally with angularjs?

RealmSwift - How can I store data in list

How much data can I store in NSUserDefaults?

How can I store data into array of AngularJS

How can I store the users' data properly?

How can I store data inside python?

How can I store MQTT data into QuestDB?

How can I filter and order Json data

How can I store a gameobject in a variable, destroy it in the scene, but not in the variable?

How can I store the count of a for loop with promises that return in an arbitrary order?

Sympy: How can I store an equation in order to solve it later numerically?

How can I calculate avg of data from django form and store in variable for later use?

How can I permanently store a sorted version of a data frame in a new variable using pandas

how can i order the sql "SHOW DATABASES" statement in alphabetic order

Why is my data object variable being altered when I'm not reassigning manipulated values to it?

How can I extend an SQL query in a variable?

How can I store long text data in embedded SQL Server CE database of C# application?

How can i assign data as variable in node?

How can I count data groups sequential order in the same column in SQL Server 2014?