case compare to number and give out name - sql

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

Related

Using case statement to compare columns

I'd like to use a case statement to compare multiple rows on 2 columns. For example, if row 1 column 1 and row 2 column 1 match but row 1 column 2 and row 2 column 2 don't, then xx. I have
CASE
WHEN (pprof.description = pprof.description and PCtrl.[sequence] <> PCtrl.[sequence])
THEN xx
but that doesn't return any values, which I know to be incorrect. I'm new to SQL so apologies if I've got this all wrong.
Edit:
Here's some sample data:
Column 1
Column 2
Column 3
123
-A
-No
123
-B
-Yes
Can't figure out the formatting here but there are 3 columns of data above. I'd like the case statement to evaluate whether column 1 match in 2 different rows (i.e., 123 = 123) and also whether column 2 doesn't (A <> B) and if both those conditions are true, return a value in column 3 (in my case, make the No a Yes, since 123-B is Yes). It might be worth noting that the "Yes" and "No" themselves are built into the larger case statement here:
(CASE WHEN tenure.description not in ('casual','co-op','fswep') THEN CASE WHEN (pprof.description = pprof.description and PCtrl.[sequence] <> PCtrl.[sequence])
THEN CASE WHEN (PEmp.Employee_Number = PEmp3.Supervisor_Number) THEN 'Yes' ELSE 'No' END END END) as 'People Manager'
You will want to do a self join here. Without seeing your data, I can't really give you an answer, but you want something like this.
Update A
Set column_3 = 'YES' /* or put CASE statement here /*
FROM pprof A
INNER JOIN pprof B
ON a.description = b.description
AND a.sequence != b.sequence
You may need more join conditions depending on the form of your data and what you want.

replace value with 1 and apply SUM method in one query SQL

I need to replace the value in column imp from 1-0 to 1 and sum it up. Column imp is a STRING, so it also needs to be converted to INT. Each line represents the record so I need to sum up imp and group by another column (in order to get the number of records for a specific Advertiser)
My data looks like this:
advertiser advertiser_id imp
Frank 123 1-0
Frank 123 1-0
Mike 124 1-0
My query:
SELECT
a.AdvertiserID as AdvertiserID,
SUM(
CASE
WHEN a.imp = '1-0' THEN "1"
END
),
b.advertiser_name as AdvertiserName,
FROM
(
`bigquery_table_a` a
INNER JOIN `another_bigquery_table` b ON (a.AdvertiserID = b.advertiser_id)
)
GROUP BY
AdvertiserID,
AdvertiserName
Error message: No matching signature for aggregate function SUM for argument types: STRING
Desired output:
advertiser advertiser_id imp
Frank 123 2
Mike 124 1
Are you just looking for aggregation and count()?
SELECT AdvertiserID, AdvertiserName, COUNT(*)
FROM `bigquery_table_a` a JOIN
`another_bigquery_table` b
ON (a.AdvertiserID = b.advertiser_id
GROUP BY AdvertiserID, AdvertiserName;
Or, if you specifically want to count values of '1-0', use COUNTIF():
SELECT AdvertiserID, AdvertiserName, COUNTIF(imp = '1-0')
FROM `bigquery_table_a` a JOIN
`another_bigquery_table` b
ON (a.AdvertiserID = b.advertiser_id
GROUP BY AdvertiserID, AdvertiserName;
The immediate problem with your code is that as you point out you want an INTEGER type so you can sum it;. Yet your CASE goes out of its way to return a STRING type instead. So you could change
CASE
WHEN a.imp = '1-0' THEN "1"
END
to
CASE
WHEN a.imp = '1-0' THEN 1
END
You might also want to add an ELSE condition, but honestly that isn't likely to matter for your purpose; if you did, it would look like
CASE
WHEN a.imp = '1-0' THEN 1
ELSE 0
END
Now there also may be simpler ways to get the count you want; what else you might do depends what DBMS you're using. But from where you are, the above seems like the simplest fix.

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.

Can a CASE expression have 2 resultant values

I have to write a case expression in SQL which goes like this,
case condition
if (T_CD = 'Y')
Case C_CD = 'H3'
set R_ID = 3 and RS_ID = 25
CASE A_FLG = 'N' and Mod = 'D'
set R_ID = 3 and RS_ID = 31
Both R_ID and RS_ID populate columns in a different table and have to be derived as per condition above.
My question is - Since I want 2 separate fields out of my case expression, will a single Case give out 2 resultant field values for me. Or Do I have to write 2 different case expressions for it.
If your dbms supports row types, maybe this works for you:
select case when a = 1 then (1,2) else (3,4) end from testtable;
The SQL Validator says:
The following feature outside Core SQL-2003 is used:
T051, "Row types"

MSSQL - No Result or NULL

I have a little problem.
I'm SELECTING From a table. I want to see a value what is just used only in one point of my workflow. Before that no value here.
But, if no result, then my "result" is NULL. And thats not good for me.
I mean, if I write
SELECT myValue FROM myTable WHERE asd = 'thisIs'
when myValue is NULL in the table, then my result is NULL. Thats okay.
when no results found, then my result is NULL too. Thats not okay.
If no result I have do different function.
How can I separate these ?
|MyValue|
---|-------|
1 |NULL |
---|-------|
|MyValue|
---|-------|
|MyValue|
---|-------|
1 |Hello |
---|-------|
In the first and third case I have to call an Update function, in the second case I have to call an Insert function.
But the result what my SP is sending back in the first and second case is NULL too.
Thank you for your help in advance.
As I understand you want to do something like this:
IF EXISTS (
SELECT myValue FROM myTable WHERE asd = 'thisIs'
)
BEGIN
SELECT myValue FROM myTable WHERE asd = 'thisIs'
END
ELSE
BEGIN
EXEC SomeOtherCode
END
You can count number of rows found after select records query. if count is greater or equal to one you have to run update query else insert query as selected records are empty.