Case statement in sql - sql

I want to create a computed column in SAS enterprise guide where it’ll show car items if it meets certain conditions.
There are 75 car items (T.TS_Items) such as windscreen, tyres, steering wheel etc.
Each item has a unique ID (T.TS_NUM) so T.TS_NUM =1 to T.TS_NUM =75.
And I want to then give each new car item a label such as labelNo_01 to labelNo_75.
So, if the vehicle type is Honda (T.TS_F_NUM = 2), type is hatchback(T.TS_TYPE = I) and T.TS_NUM =1 then the new column name is LabelName_01 with a item such as windscreen.
and if the vehicle type changes for example - the vehicle type is Toyota (T.TS_F_NUM = 1)
but the rest is the same it will give me the 75 car item for Toyota.
My code:
PROC SQL;
CREATE TABLE WORK.MotorVehicle AS
SELECT
T.TS_VEHICLE_RDES,
/* FI_Label_01 */
Case When
T.TS_F_NUM in (1,2) And
T.TS_TYPE = I And
T.TS_NUM =1 Then T.T_Item
else T.T_Item
End
AS FI_Label_01
FROM T.T_ITEM
WHERE T.TS_F_NUM = 41
ORDER BY T.TS_NUM ;
QUIT;
The above is simple and works but I am not sure how to add an Else or Else if statement. Basically when I use the above code and hard code the vehicle type to 1 (toyota) in the where statement its working and its gives me only Toyota but its also giving me all the types instead of just hatchback. I want to add a condition where its only giving the type hatchback but I dont know how or where in the above code it’ll go in.
Also a loop would help so I dont have to repeat the process for each vehicle type. Hope this information helps.
Some Data.
T.TS_F_NUM T.TS_TYPE T.T_Item T.TS_NUM
1 I windscreen 1
2 I side mirror 2
1 C Side mirror 3
2 C passenger door 4
1 I dashboard 5
2 I gear box 6

Yeah you're pretty close, I've got some similar code and mine looks like this and works :)
This obviously has sub sub rules but all the principles are the same for ELSE, I don't think there is an ELSE IF...
CASE WHEN
RULE_1 = TRUE THEN
CASE WHEN RULE_2 = TRUE AND RULE_3 = TRUE THEN
CASE WHEN
RULE_4 = TRUE
THEN
ITEM_1
ELSE
ITEM_2
END
ELSE
CASE WHEN RULE_5 THEN
ITEM_3
ELSE
CASE WHEN RULE_6 = TRUE THEN
ITEM_4
ELSE
ITEM_5
END
END
END
END

Related

Using IF statement to pull boolean column header for an entry

I'm not good at all with IF statements. I currently have a schedule that looks like this:
Lot(Int) PartNum(Varchar50) Amount(Int) IsPainted(Bool) IsInspected(Bool) Finished(Bool)
1 xxx-0191 500 1 1 0
2 xxx-0191 700 1 0 0
What I'm trying to accomplish, and I'm under the thought it'll have to be handled by an IF statement but I'm certainly open for using whatever works best here, is to have a query that will give me the following
Lot PartNum Amount Status
1 xxx-0191 500 Inspected
2 xxx-0191 700 Painted
What I need it to do is just pull the last available column of "True" or "1" in the boolean columns and just display that information in the "Status" column in the query.
use this code
select
Lot,
PartNum,
Aamount,
Case when IsInspected=1 then 'Inspected' else 'Painted' end Status
from
table
Use case. Something like this:
select lot, partnum, amount,
(case when Finished = 1 then 'Finished'
when IsInspected = 1 then 'Inspected'
when IsPainted = 1 then 'Painted'
) as status
This chooses the last boolean as the one chosen for the status.

get percentage of common column between two row in SQL

I have a table like this :
Pseudo |eyes |hair |gender
-------|-------|-------|-------
mat01 |brown |black |male
Alex |blue |black |male
And i need to get the percentage of exactly same field for two given pseudo
in this case, for pseudos mat01 and alex, it should return 0.66 (2/3), because they have the same hair and gender, but not the same eyes.
Does anybody have an idea on how to do this? It should be in one SQL request if possible.
Thank you
No need for case expressions or anything.. you can simply do that:
SELECT t.pseudo,s.pseudo,
((t.eyes = s.eyes) + (t.hair = s.hair) + (t.gender = s.gender))/3 as YourAvg
FROM YourTable t
INNER JOIN YourTable s
ON(t.pseudo <> s.pseudo)
Thats because a condition like t.eyes = s.eyes is a boolean, if its true then it returns 1 and if not it returns 0 .
This will give you each pseudo compared to each pseudo.. you can change it to show only the pseudo you want
This is one method assuming you want to compare every pseudo to the others without duplication and self compare:
Like A-->B
but not like A--> A and not B--> A
SELECT A.Pseudo, B.Pseudo, (case when a.eyes = B.eyes then 1 else 0 end+
case when a.hair = b.hair then 1 else 0 end+
case when a.gender = b.gender then 1 else 0 end)/3 as mAvg
FROM table A
INNER JOIN table B
on A.Pseudo < B.Pseudo
If you need to know how everyone compare to everyone else you should do it in your application rather than in the DB, as you would need an extra column for each user to know the % and the ID of the user you are comparing to.

Transform an Int "Id" to a Specific Text

i have a question, I want to display in a Datagrid in Flex some information of this Combobox: http://puu.sh/bU6ht/ee36ed7fd1.png
N/A = 0
B = 1
M = 2
NT = 3
FS = 4
ER = 5
SSE = 6
And is saved in the Database as int, but I want in the Datagrid display the information as String
http://puu.sh/bU6cR/3c112d85e3.png
I have a solution and is doing a Case in SQL, but i dont know if is the correct solution.
CASE insp._manguera
WHEN 0 THEN 'N/A'
WHEN 1 THEN 'B'
WHEN 2 THEN 'M'
WHEN 3 THEN 'NT'
WHEN 4 THEN 'FS'
WHEN 5 THEN 'ER'
WHEN 6 THEN 'SSE'
END as _manguera,
And do that for every field in the database
You could also have a table in which you could associate the values to letters and do a join on that table.
That way, you wouldn't need to repeat that code anywhere and you could always modify the table. Good for long time use and reuse.
tell me if you need an example :)

case compare to number and give out name

the problem I have is that I have a column called cate with the numbers 1 to 5 but I want alias names in the print out.
For example if the column has the number 1 I want STONE in the result set, if it is 2 I want "TREE".
I should look something like
Select
case when t.cate = 1 then t.cate="STONE"
case when t.cate = 2 then t.cate="TREE"
else null end as test from dbt.tbl t
I do not want to change the value in the table only in the print out.
Any idea how I can that to work?
Thanks for all your help in advance
remove extra case,
SELECT CASE WHEN t.cate = 1 THEN 'STONE'
WHEN t.cate = 2 THEN 'TREE'
ELSE null
END AS test
FROM dbt.tbl t
Alternatively, you can write
SELECT
CASE t.cate
WHEN 1 THEN 'STONE'
WHEN 2 THEN 'TREE'
ELSE NULL
END AS test
FROM dbt.tbl t
If the list is likely to change in the future (either through edits or additions), I'd do it as a separate table:
INSERT INTO Cates (Cate,Description) VALUES
(1,'Stone'),
(2,'Tree') --Etc
And then just do:
SELECT c.Description as Test
FROM dbt.tbl t inner join Cates c on t.Cate = c.Cate

Scoring database entries by multiple columns

i'm faced with a situation, where i have to find the best matches for a users search request. I will provide an example (a little abstract):
We have a table with lawyers:
Name Location Royality Family Law Criminal Law
-------------------------------------------------------------
Lawyer A Berlin 100 €/hour false true
Lawyer B Amsterdam 150 €/hour true true
A user should now be able to search by several features. The weight of each feature should be some kind of parameter. In my case the table contains much more of such features (Location, Royality, 20+ boolean values). Of course the result should provide all "good" results but ordered by some kind of "score", so the best result appears at the top.
I'm not looking for a out of the box solution rather than some introduction to this topic.
Kind regards,
matt
A generic approach is to assign a weight to each item, and add them up when they match. This will cause a full table scan to score every single record.
Assuming inputs of Berlin, >100/hr, Criminal Law=true, family law = null (no criteria)
And Location match carries a weight of 5
select *
from (
select *,
case when location = 'berlin' then 5 else 0 end +
case when royality >= 100 then 1 else 0 end +
case when familylaw = null then 1 else 0 end +
case when criminallaw = true then 1 else 0 end as score
from tbl
) scored
order by score desc
You may be able to make use of SOUNDEX functions in your particular RDBMS. These compare two strings and give a numeric value for "how alike they sound".
You can then weight and/or sum the results for each column, as mentioned by Richard.