logo
down
shadow

SQL Order By: Specific Value First, then Ordering?


SQL Order By: Specific Value First, then Ordering?

By : Fluch
Date : October 20 2020, 08:10 AM
like below fixes the issue Use a case expression to put Needs Response rows first. Then order by date descending:
code :
order by case when status = 'Needs Response' then 0 else 1 end, date desc


Share : facebook icon twitter icon
ordering array in specific order

ordering array in specific order


By : user3311892
Date : March 29 2020, 07:55 AM
around this issue I have following array , try this
code :
$myArray="your array";

<?php

foreach($myArray as $c=>$key) {
    $sort_conv[$c] = $key['conversions'];
    $sort_lable[$c] = $key['label'];
   }

array_multisort($sort_conv, SORT_ASC, $sort_lable, SORT_STRING, $myArray);
print_r($myArray);
?>
Ordering resultset in specific order

Ordering resultset in specific order


By : Naveen Naidu
Date : March 29 2020, 07:55 AM
around this issue I need to group my resultset with "NY" entries first then any other state entries which might be returned. , Try this at the end of your query:
code :
order by (case when state = 'NY' then 0 else 1 end),
         state
Ordering list by a specific order

Ordering list by a specific order


By : Gene Lawrence
Date : March 29 2020, 07:55 AM
I wish this helpful for you You need to think about performance when running a sort like this.
If you expect only a handful of elements, then Pedro's solution is OK.
code :
List<Test> tests = new List<Test>() { 
    new Test(){ No = 101 },
    new Test(){ No = 201 },
    new Test(){ No = 300 },
    new Test(){ No = 401 },
    new Test(){ No = 500 },
    new Test(){ No = 601 }
};


int[] newOrder = new int[6] { 201, 401, 300, 101, 601, 500 };

// Create a Dictionary/hashtable so we don't have to search in newOrder repeatedly
// It will look like this: { {201,0}, {401,1}, {300,2}, {101,3}, {601,4}, {500,5} }
Dictionary<int, int> newOrderIndexedMap = Enumerable.Range(0, newOrder.Length - 1).ToDictionary(r => newOrder[r], r => r);

// Order using 1 CPU
var orderedTests = tests.OrderBy(test => newOrderIndexedMap[test.No]);
// Order using multi-threading
var orderedInParallelTests = tests.AsParallel().OrderBy(test => newOrderIndexedMap[test.No]);

// Order using 1 CPU, when it's possible that a match will not be found in newOrder
var orderedTestsSafe = tests.OrderBy(test => 
    {
        int index;
        bool foundIndex = newOrderIndexedMap.TryGetValue(test.No, out index);
        return foundIndex ? index : Int32.MaxValue;
    });
Ordering column in specific order

Ordering column in specific order


By : Peterson R Manuel
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have a select statement: , I would suggest using GROUP BY and a simpler ORDER BY:
code :
SELECT LOCATIONTYPE
FROM DLOCATION
WHERE BinCheckedOut = 'N' and submitted = 'Y' and
      BINNUM > 0 and DISPOSED = 'N'
GROUP BY LOCATIONTYPE
ORDER BY MIN(BINNUM);
Ordering data in specific order in R

Ordering data in specific order in R


By : user988799
Date : March 29 2020, 07:55 AM
around this issue We can using row_number with dplyr group_by create a help key to achieve the certain order
code :
df %>% 
  group_by(name,status) %>%
  mutate(Helpkey=row_number()) %>%
  arrange(name,Helpkey) %>%
  select(-Helpkey)

# A tibble: 10 x 3
# Groups:   name, status [6]
     NUM status  name
   <int>  <chr> <chr>
 1     1      +     A
 2     4      -     A
 3     2      +     A
 4     5      -     A
 5     8      +     A
 6    10      -     A
 7     3      +     B
 8     9      -     B
 9     6      +     C
10     7      -     C
Related Posts Related Posts :
  • SQL Query - Group consecutive items based on condition
  • Users who work in same department
  • Syntax error near column value Vb
  • Oracle Trigger BEFORE INSERT has No data found
  • What kind of join to use on SQL tables
  • Is there a way to add a constant value dynamically to all records returned in Hive?
  • SQL optimization (inner join or selects)
  • EF 6.x, LINQ-to-SQL and raw SQL clauses
  • Simple SQL Variable Assignment Only Returns One Letter: Why?
  • Converting a custom timestamp to date
  • SQL Server : inserting Player vs Player names in to new table from tblEntrants
  • invalid identifier in sql
  • PL/SQL - I keep getting this error when concatenating: PLS-00306: wrong number or types of arguments in call to '||'
  • Count records only from left side of a LEFT JOIN
  • get everything before a string including itself oracle
  • Format Data from Word Doc to SQL using RegEX
  • Conditional formatting on MAX value row
  • MS-Access : selecting data from two tables and only returning you need
  • SQL Server: optimal indexing strategies for many-to-many join
  • DBgrid column very wide
  • PostgreSQL Group values by category, count and calculate percentage
  • MS Access SQL - Most Recent Record for Each Consultant ID
  • Update table: Summary of previous rows without using cursor or while loop
  • PostgreSQL: built-in function to remove substring starting with certain pattern
  • ORA-00909: invalid number of arguments
  • How to summarize all possible combinations of variables?
  • Select Column within a Column SQL
  • PostgreSQL Inserting 2 relationships at once
  • T sql - How to store results from a dynamic query using EXEC or EXECUTE sp_executesql
  • How do I parse my json into CSV using regex?
  • Reverse foreign key cascading (or how to collect database garbage)
  • SQL Pivot Questions
  • Insert records into a table with a condition in SQL Server 2016
  • display null value using rank functions in oracle sql
  • SQL - Get count of group by column but also select top item of group
  • How to add an array of datarows into an exisitng table inside my database
  • There is no unique constraint matching given keys for referenced table "employee" 1
  • SQL: Unable to SELECT joined column
  • How to find out how much space a SQL Server table uses?
  • Window function to remove specific records from SQL Server dataset
  • How to add a column for each day in sql?
  • Create group column based on the specific rows
  • Not sure if this consistitues a transitive dependency
  • How to compare the values in a column to a long list in SQL Server
  • Preserving data format Decimal(6,5) from vba to sql
  • Oracle Query to rollup QTY by Year- only last 3 years
  • SQL - Calculate 2 columns and view result to another to column
  • Divide or Multiply according to a condition (Improving query)
  • PostgreSQL unnest() with consecutive integers grouped by number
  • SQL to limit output to certain months and years
  • VARCHAR TIME TO GET THE DIFFERENCE
  • SQL conditional constraint on multiple columns being unique
  • Optimize a SQL select query in a loop
  • BTEQ Teradata Import Multiple files into one table
  • Update SQL datetime column with oldest values of another table column?
  • Is INSERT ... SELECT an atomic transaction?
  • SQL query completed successfully but not results
  • SQL sub select returning multiple values
  • Verify condition on two columns
  • SQL conditional field, first match JOIN
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org