Can you use an 'In' operator in a column expression? - sql

Using Oracle 11. Trying to write some simple code for a conceptual demo. To illustrate what I'm trying to achieve, imagine I have SOMETABLE that has two columns: ID and NAME, like so:
ID NAME
---------
1 Tom
2 Larry
3 David
4 Steve
I'm trying to compute a third column that is true if the second column matches one of two hard-coded values. Something like this (which of course doesn't work.)
Select ID,
NAME,
(NAME in ('Larry', 'David')) as IS_FAVORITE
from SOMETABLE
and hoping to get this output...
ID NAME IS_FAVORITE
----------------------
1 Tom FALSE
2 Larry True
3 David True
4 Steve FALSE
Much to my surprise, I'm being told Oracle doesn't have the concept of booleans and I should be using 'numeric strings' or something like that, so this too is fine...
ID NAME IS_FAVORITE
----------------------
1 Tom 'N'
2 Larry 'Y'
3 David 'Y'
4 Steve 'N'
So can you use the IN operator in a column expression like this? If not, how would one compute the column that I am after?

You can achieve your expected output using case expression.
Select ID,
NAME,
CASE
WHEN
NAME in ('Larry', 'David')
THEN
'TRUE'
ELSE
'FALSE'
END as IS_FAVORITE
from SOMETABLE

Related

merging multiple rows into one based on id

i have the data in this format in an amazon redshift database:
id
answer
1
house
1
apple
1
moon
1
money
2
123
2
xyz
2
abc
and what i am looking for would be:
id
answer
1
house, apple, moon, money
2
123, xyz, abc
any idea? the thing is that i cannot hard code the answers as they will be variable, so preferably a solution that would simply scoop the answers for each id's row and put them together separated by a delimiter.
you can use aggregate function listagg:
select id , listagg(answer,',')
from table
group by id
You can use string_agg(concat(answer,''),',') with group by so it will be like that:
select id , string_agg(concat(answer,''),',') as answer
from table
group by id
tested here
Edit:
you don't need concatenate, you can just use string_agg(answer,',')

Postgres Group by like fuzzy logic

I want to group by data present table. but the problem in all names is similar not the same.
id name subject_id
---------------------------------
1 Ganeash 1
2 Ganesha P 2
3 Shree Ganesh Pai 1
4 Gaanesh shree G 1
5 Ramesh shri 2
In this data everywhere Ganesh is common so the output should contain.
name count
-------------
Ganesh 4
Ramesh 1
If I use soundex function.
postgres=# SELECT soundex('hello world!');
ERROR: function soundex(unknown) does not exist
LINE 1: SELECT soundex('hello world!');
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
You can also use CASE
select name,count(*) from (
select case when name like '%Ganesh%' THEN 'Ganesh'
when name like '%Ramesh%' THEN 'Ramesh' end as name
from test) a
group by name
Check Demo Here
Output
It is pretty simple, you just need to split the text field into an array of words and then unnest the arrays into rows. Then you manipulate the rows using standard SQL (count(), GROUP BY, etc.):
SELECT count(*),
unnest(regexp_split_to_array(name, E'\\s+')) AS name2
FROM names
GROUP BY name2
ORDER BY 1 DESC

Getting a union query to create single records

So I've put together a union query in access, but it displays in this fashion.
Name Column1 Column2
John 1 0
Jim 2 0
Mike 3 0
John 0 2
Jim 0 1
Mike 0 3
I would like for it to display like this:
Name Column1 Column2
John 1 2
Jim 2 1
Mike 3 3
In my select statement I'm setting column2 to 0 in the first portion of the union statement, and setting column1 to 0 in the second part. Now I realize that's why I'm getting the 0s in the end result, but is there a way to achieve my desired display with a union or do I need something more complex.
EDIT: The reason I set those columns to 0, was to avoid it asking me to enter a value when it could not find a value for column1 or 2.
PS. this is not my actual query, but rather just a quick example I was able to throw on here for question purposes.
select [Name], max([Column1]) as col1, max([Column2]) as col2
from ( put union query here ) as x
group by [Name]
edit -- didn't notice that your data was a query result and not a table. Put your current query where indicated in the from clause (as an inline view)

To select a unique value from a column having multiple values MS Access

Table Structure is like this
Table Name:Employee
**Employee_Id APPRS_TY_CD**
540589 2
540589 UNK
1952938 2
1952938 UNK
2488178 1
2488178 UNK
3818934 1
3818934 UNK
5402944 1
If a Employee ID has APPRS_TY_CD as (UNK AND a value) then APPRS_TY_CD should be a value and not UNK. If APPRS_TY_CD is not UNK for an Employee ID then that value should be populated as it is.
My final output should look like this.
**Employee_Id APPRS_TY_CD**
540589 2
1952938 2
2488178 1
3818934 1
5402944 1
I'm using MS Access.
This should be fairly simple as numbers are considered "lower" than strings you can use an aggregate function, I've created an SQL Fiddle here (note this is Sql Server but the code should be the same as it's not using proprietary features). Given your data you could use the MIN function from SQL to get the APPRS_TY_CD for each user. Here is my suggested code:
SELECT
Employee_Id
, MIN(APPRS_TY_CD) APPRS_TY_CD
FROM
Employee
GROUP BY
Employee_Id
The results returned are (you should be able toe execute the fiddle yourself to prove this):
EMPLOYEE_ID APPRS_TY_CD
540589 2
1952938 2
2488178 1
3818934 1
5402944 1

Conditioning on multiple rows in a column in Teradata

Suppose I have a table that looks like this:
id attribute
1 football
1 NFL
1 ball
2 football
2 autograph
2 nfl
2 blah
2 NFL
I would like to get a list of distinct ids where the attribute column contains the terms "football", "NFL", and "ball". So 1 would be included, but 2 would not. What's the most elegant/efficient way to do this in Terdata?
The number of attributes can vary for each id, and terms can repeat. For example, NFL appears twice for id 2.
You can use the following:
select id
from yourtable
where attribute in ('football', 'NFL', 'ball')
group by id
having count(distinct attribute) = 3
See SQL Fiddle with Demo (fiddle is showing MySQL, but this should work in TeraData)