SQL ORDER BY (CASE), NEWID() - sql

I have a table like the image below. I already have a code that selects all rows that has no RQID and follows by those rows that has RQID.
SELECT * FROM table1
WHERE moduleexam = 20
ORDER BY CASE
WHEN RQID <> ''
THEN 1
ELSE 0
END, NEWID()
The query works by selecting all rows randomly except those rows that has RQID.
The problem is in the rows that has RQID. How can I make it that both rows with the same RQID should be sequentially ordered?
P.S. I am using MSSQL Server 2005.

Try
ORDER BY CASE
WHEN RQID <> ''
THEN 1
ELSE 0
END, RQID, NEWID()
After sorting by empty/not empty it will sort by actual RQID and after that randomly.

ORDER BY CASE ... END, RQID, NEWID();

Related

Remove the duplicate rows based on Presence of Number of NULL values in a row

I was able to remove the duplicate rows, but I would like to remove the duplicate rows based on one more constraint. I want to keep only a row with a smaller number of NULL values.
Original Table
Ran the SQL Server Query
WITH CTE AS(
SELECT *,
RN = ROW_NUMBER()OVER(PARTITION BY Premise_ID ORDER BY Premise_ID)
FROM sde.Premise_Test
)
DELETE FROM CTE WHERE RN > 1
Result:
But I want to get this result
I have modified the SQL script as per the comment from Aaron. but the result is still the same. DB fiddle is showing NULL from IS NULL getting highlighted.
Update the ROW_NUMBER() function like this (no, there is no shorter way):
RN = ROW_NUMBER() OVER (
PARTITION BY Premise_ID
ORDER BY Premise_ID,
CASE WHEN Division IS NULL THEN 1 ELSE 0 END
+ CASE WHEN InstallationType IS NULL THEN 1 ELSE 0 END
+ CASE WHEN OtherColumn IS NULL THEN 1 ELSE 0 END
...
)

SQL Server Sorting Int Column bug

I have this SQL statement that sorts the rows depending on their change_order and change_id.
My statement looks like this:
select *
from change_dtl_from2
where chnfr_hdrno = 'CH000009'
order by
case
when chnorder is null
then 1
else 0
end asc, change_id asc
Where chnfr_hdrno is the document number, chnorder is change_order, change_id is unique key per row.
If I execute that statement, the result will be like this:
As you can see, the row with chnorder value of 5 is on the top most where the chnorder is set its order by ascending. I don't know where or what I'm doing wrong.
The default value of chnorder is null and is an int column. That's why I'm confused because whenever I add a new row, the new one will be on top most. I hope you can all help me. :)
SQL does everything correct depending on your query. With your CASE-Statement you "replace" all non-NULL-values to 1 and sort them only by change_id. If you only want to replace NULL by 1 you should use
order by (case when chnorder is null then 1 else chnordner end) asc
or you should use
order by (case when chnorder is null then 1 else 0 end) asc, chnordner asc, change_id asc

SQL: Order by statement with start value

I have a field like this:
1月~3月
12月~1月
3月~12月
4月~12月
9月~8月
6月~7月
How can i sort that column following:
4月~12月
6月~7月
9月~8月
12月~1月
1月~3月
3月~12月
It start by 4 and end by 3 (4-5-6-7-8-9-10-11-12-1-2-3)(month)
This will do it, you need to separate out the numeric portion of your field, and also use a CASE statement:
SELECT *
FROM Table1
ORDER BY CASE WHEN CAST(LEFT(Col1, CHARINDEX('月',Col1)-1)AS INT) >= 4 THEN 1 END DESC
,CAST(LEFT(Col1, CHARINDEX('月',Col1)-1)AS INT)
Demo: SQL Fiddle
SQL Server syntax above, might vary depending on database.
order by case when col < 4 then 1 else 0 end, col
or if it's really a varchar
order by case when convert(int,substring(col,1,1)) < 4 then 1 else 0 end, col

SQL - Order By Different case on one column

I have data like this:
I wanna order the data, and the result will be like this:
CHECKING_ACCT_MONTHS--------------------11-201110-201109-2011AVERAGE
in another words, the data will ordered descending, but the AVERAGE data will be at the bottom. How can I do that,.?
Query should be ...
SELECT *
FROM TableName
ORDER BY
CASE
WHEN CHECKING_ACCT_MONTHS = 'AVERAGE'
THEN 1 ELSE 0
END,
CHECKING_ACCT_MONTHS DESC

Order by Maximum condition match

Please help me to create a select query which contains 10 'where' clause and the order should be like that:
the results should be displayed in order of most keywords(where conditions) matched down to least matched.
NOTE: all 10 condition are with "OR".
Please help me to create this query.
i am using ms-sql server 2005
Like:
Select *
from employee
where empid in (1,2,4,332,434)
or empname like 'raj%'
or city = 'jodhpur'
or salary >5000
In above query all those record which matches maximum conditions should be on top and less matching condition record should be at bottom.
SELECT *
FROM (SELECT (CASE WHEN cond1 THEN 1 ELSE 0 END +
CASE WHEN cond2 THEN 1 ELSE 0 END +
CASE WHEN cond2 THEN 1 ELSE 0 END +
...
CASE WHEN cond10 THEN 1 ELSE 0 END
) AS numMatches,
other_columns...
FROM mytable
) xxx
WHERE numMatches > 0
ORDER BY numMatches DESC
EDIT: This answer was posted before the question was modified with a concrete example. Marcelo's solution addresses the actual problem. On the other hand, my answer was giving priority to matches of specific fields.
You may want to try something like the following, using the same expressions in the ORDER BY clause as in your WHERE clause:
SELECT *
FROM your_table
WHERE field_1 = 100 OR
field_2 = 200 OR
field_3 = 300
ORDER BY field_1 = 100 DESC,
field_2 = 200 DESC,
field_3 = 300 DESC;
I've recently answered a similar question on Stack Overflow which you might be interested in checking out:
Is there a SQL technique for ordering by matching multiple criteria?
There are many options/answers possible. Best answer depends on size of the data, non-functional requirements, etc.
That said, what I would do is something like this (easy to read / debug):
Select * from
(Select *, iif(condition1 = bla, 1, 0) as match1, ..... , match1+match2...+match10 as totalmatchscore from sourcetable
where
condition1 = bla or
condition2 = bla2
....) as helperquery
order by helperquery.totalmatchscore desc
I could not get this to work for me on Oracle.
If using oracle, then this Order by Maximum condition match is a good solution.
Utilizes the case when language feature