SQL server use case in order by - sql

One of the column in my table like this:
Symbol
586fast
urgent
243late
296fast
122late
155fast
I need urgent in first then records with fast then records with late like this:
urgent
586fast
296fast
155fast
243late
122late
I am getting urgent in first row by:
ORDER BY CASE WHEN substring(Symbol, len(Symbol) - 2, 3) = 'ent'THEN 0 ELSE 1 END
After that it is order by number in records.

Since you said you are on 2005 you could do something like this.
order by case
when Symbol = 'urgent' then 1
when Symbol like '%fast' then 2
when Symbol like '%late' then 3
else 4
end

ORDER BY CASE WHEN CHARINDEX('urgent', Symbol) > 0 THEN 1
WHEN CHARINDEX('fast', Symbol) > 0 THEN 2
WHEN CHARINDEX('late', Symbol) > 0 THEN 3
ELSE 4
END

Related

Count each column if the value is met for a row

I've been searching far and wide (pun intended) but haven't found a solution yet.
So I got a table with 19 columns.
pokemon name
against_bug
against_dark
against_dragon
against_electric
against_fairy
against_fight
against_fire
against_flying
against_ghost
against_grass
against_ground
against_ice
against_normal
against_poison
against_psychic
against_rock
against_steel
against_water
Each of the "against_xxx" columns can have a value of 0,5, 1, 2 or 4.
I want each row to count how many of these columns have a value of 2 or higher in order to determine which pokemon has the most vulnerabilities.
I have no idea how to approach this.
Please look at Stu's suggestion in the comments on your question. Normalizing your table would be a great help.
Now you need to do something like this:
SELECT
pokemon,
CASE WHEN against_bug >= 2 THEN 1 ELSE 0 END +
CASE WHEN against_dark >= 2 THEN 1 ELSE 0 END +
CASE WHEN against_dragon >= 2 THEN 1 ELSE 0 END +
CASE WHEN against_electric >= 2 THEN 1 ELSE 0 END +
--[....repeat this for all your columns....]
FROM your_table
A normalized table would look like this:
pokemon
against_type
against_value
Pickachu
against_bug
1
Pickachu
against_dark
2
Pickachu
against_dragon
0.5
Pickachu
against_electric
1
Pickachu
(etc)
(etc)
Blastoid
against_bug
1
Blastoid
against_dark
2
Blastoid
against_dragon
2
Blastoid
against_electric
4
Blastoid
(etc)
(etc)
In this case you could write a much simpler query:
SELECT
pokemon,
count(*) AS number_of_vulnerabilities
FROM your_table
WHERE against_value >= 2
GROUP BY pokemon

Aggregation using conditions in CASE WHEN in SQL Teradata? [duplicate]

I work in SQL Teradata.
I would liek to count how many rows (clients) have rounded value in column "amount" (means for example 140.00 not 157.76 and so on). I use code like below:
select
client_id,
count(amount mod 1 = 0)
from table
group by client_id
Nevertheless, I have an error like: SELECT Failed. 3706: Syntax error: expected something between an integer and '='.
What can I do ?
Use a case expression:
sum(case when amount mod 1 = 0 then 1 else 0 end)
I'm not 100% sure if Teradata supports mod 1. I would normally write this as:
sum(case when amount = floor(amount) then 1 else 0 end)

Give first duplicate a 1 and the rest 0

I have data which contains 1000+ lines and in this it contains errors people make. I have added a extra column and would like to find all duplicate Rev Names and give the first one a 1 and all remaining duplicates a 0. When there is no duplicate, it should be a 1. The outcome should look like this:
RevName ErrorCount Duplicate
Rev5588 23 1
Rev5588 67 0
Rev5588 7 0
Rev5588 45 0
Rev7895 6 1
Rev9065 4 1
Rev5588 1 1
I have tried CASE WHEN but its not giving the first one a 1, its giving them all zero's.
Thanks guys, I am pulling out my hair here trying to get this done.
You could use a case expression over the row_number window function:
SELECT RevName,
Duplicate,
CASE ROW_NUMER() OVER (PARTITION BY RevName
ORDER BY (SELECT 1)) WHEN 1 THEN 1 ELSE 0 END AS Duplicate
FROM mytable
SQL tables represent unordered sets. There is no "first" of anything, unless a column specifies the ordering.
Your logic suggests lag():
select t.*,
(case when lag(revname) over (order by ??) = revname then 0
else 1
end) as is_duplicate
from t;
The ?? is for the column that specifies the ordering.

Sqlite Get counts of all distinct values across a row

For a personal end of the year project I've scraped my attendance off the school website hoping to do some form of visualization of the data. I've now gotten stuck transforming that data into the form I need it in.
Currently my database looks like this
Date,One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Dee
2014-09-03,P,P,P,P,AU,AU,P,T*,AU,P
2014-09-04,P,P,P,P,N/A,AU,P,T*,N/A,P
2014-09-05,P,P,P,P,AU,AU,P,P,P,P
2014-09-09,P,P,P,P,AU,AU,P,P,AU,P
2014-09-11,AU,AU,P,AU,AU,P,AU,AU,AU,P
2014-09-15,P,P,P,P,AU,P,P,P,AU,P
2014-09-17,P,P,P,P,AU,AU,P,P,AU,P
The columns are each period,and each one has an indicator of my presence. My question is, is it possible to turn that into something like this using only sqlite?
Date,P,AU,T*,N/A
2014-09-03,6,3,1,0
2014-09-04,6,1,1,2
2014-09-05,8,2,0,0
2014-09-09,7,3,0,0
2014-09-11,3,7,0,0
2014-09-15,8,2,0,0
2014-09-17,7,3,0,0
2014-09-19,9,1,0,0
Counting each occurence of a value across the row.
Something like this:
select date,
case when one = 'p' then 1 else 0 end +
case when two = 'p' then 1 else 0 end +
...
case when dee = 'p' then 1 else 0 end as p,
case when one = 'au' then 1 else 0 end +
case when two = 'au' then 1 else 0 end +
...
case when dee = 'au' then 1 else 0 end as au,
...
from table

SQL: Sort by priority, but put 0 last

I have a (int) column called "priority". When I select my items I want the highest priority (lowest number) to be first, and the lowest priority (highest number) to be the last.
However, the items without a priority (currently priority 0) should be listed by some other column after the ones with a priority.
In other words. If I have these priorities:
1 2 0 0 5 0 8 9
How do I sort them like this:
1 2 5 8 9 0 0 0
I guess I could use Int.max instead of 0, but 0 makes up such a nice default value which I would try to keep.
I don't think it can get cleaner than this:
ORDER BY priority=0, priority
SQLFiddle Demo
Note that unlike any other solutions, this one will take advantage of index on priority and will be fast if number of records is large.
Try:
order by case priority when 0 then 2 else 1 end, priority
A very simple solution could be to use a composite value/ "prefix" for sorting like this:
SELECT ...
FROM ...
ORDER By CASE WHEN priority = 0 THEN 9999 ELSE 0 END + priority, secondSortCriteriaCol
This will do the trick. You will need to replace testtable with your table name.
SELECT t.priority
FROM dbo.testtable t
ORDER BY (CASE WHEN t.priority = 0 THEN 2147483647 ELSE t.priority END)
In case it's not clear I've picked 2147483647 because this is the max value of the priority column so it will be last.
Mark's answer is better and defo one to go with.
order by case(priority) when 0 then 10 else priority end