Show values in another value in sql - sql

I want show value 1 by 'this i number one' in result of query sql. I'm looking for a statement like ALIAS but using for values, not column.
Ex:
id|
0|
1|
1|
When excute query results sholud be:
id|
this is number zero|
this is number one|
this is number one|
Thanks!

Ex: i have table id that have 2 value 0,1. Can i using SELECT id FROM table WITH 0 AS zero AND 1 AS one;
I hope my result should be: id|zero|zero|one|...
If I correct understood, you need something like this:
with t(id) as (
select 0
union all
select 1
-- another values
)
select id as original_value,
CASE
WHEN id = 0 THEN 'zero'
WHEN id = 1 THEN 'one'
-- another values
END as changed_value
from t

Related

How to make a sql view where each row is the result of a query?

I am currently working on PostgreSQL version 14.
I got two tables, one is a list of emails, the other one describe if those emails are invalid or was marked as unsubscribe (black_list). I want to make the percentage of how many addresses are invalid and unsubscribed in two different rows in the same table using a view.
My email table
| email_id | email|
|:---- |:------:|
| 1| bob.smith#yahoo.com|
| 2| ronald.gregor#gmail.com|
| 3| jo123456#gmail.com|
My table black_list looks like that.
email_id
unsubscribe
invalid
1
True
False
3
False
True
The result I expect.
categories
value
unsubscribe
33
invalid
33
I tried to make a view with this query :
CREATE OR REPLACE VIEW percentage_unsubscribe (value) AS SELECT (SELECT COUNT(*)
FROM black_list WHERE unsubscribe = True)/(SELECT COUNT(*) FROM email_table
But i would like to know how to pass the categorical column and the second row.
Use union to generate two rows and the with statement to optimize the query a bit and make it more readable, e.g.:
create or replace view percentage_unsubscribe (category, value) as
with totals as (
select
count(*) filter (where unsubscribe) as unsubscribe,
count(*) filter (where invalid) as invalid,
(select count(*) from email_table) as total
from black_list
)
select 'unsubscribe', unsubscribe* 100/ total
from totals
union
select 'invalid', invalid* 100/ total
from totals;

How to combine two queries where one of them results in an array and the second is the element place in the array?

I have the following two queries:
Query #1
(SELECT ARRAY (SELECT (journeys.id)
FROM JOURNEYS
JOIN RESPONSES ON scenarios[1] = responses.id) AS arry);
This one returns an array.
Query #2:
SELECT (journeys_index.j_index)
FROM journeys_index
WHERE environment = 'env1'
AND for_channel = 'ch1'
AND first_name = 'name1';
This second query returns the element index in the former array.
How do I combine the two to get only the element value?
I recreated a simpler example with a table containing an array column (the result of your first query)
create table my_array_test (id int, tst_array varchar[]);
insert into my_array_test values (1,'{cat, mouse, frog}');
insert into my_array_test values (2,'{horse, crocodile, rabbit}');
And another table containing the element position for each row I want to extract.
create table my_array_pos_test (id int, pos int);
insert into my_array_pos_test values (1,1);
insert into my_array_pos_test values (2,3);
e.g. from the row in my_array_test with id=1 I want to extract the 1st item (pos=1) and from the row in my_array_test with id=2 I want to extract the 3rd item (pos=3)
defaultdb=> select * from my_array_pos_test;
id | pos
----+-----
1 | 1
2 | 3
(2 rows)
Now the resulting statement is
select *,
tst_array[my_array_pos_test.pos]
from
my_array_test join
my_array_pos_test on my_array_test.id = my_array_pos_test.id
with the expected result
id | tst_array | id | pos | tst_array
----+--------------------------+----+-----+-----------
1 | {cat,mouse,frog} | 1 | 1 | cat
2 | {horse,crocodile,rabbit} | 2 | 3 | rabbit
(2 rows)
Now, in your case I would probably do something similar to the below, assuming your 1st select statement returns one row only.
with array_sel as
(SELECT ARRAY (SELECT (journeys.id)
FROM JOURNEYS
JOIN RESPONSES ON scenarios[1] = responses.id) AS arry)
SELECT arry[journeys_index.j_index]
FROM journeys_index cross join array_sel
WHERE environment = 'env1'
AND for_channel = 'ch1'
AND first_name = 'name1';
I can't validate fully the above sql statement since we can't replicate your tables, but should give you a hint on where to start from

Updating one column based on the value of another column

I have a table named Vendor, within this table I have a column called AccountTerms which is shows only a value (i.e. 0, 1, 2, 3) and so on. I also have a column that I want to use (ulARAgeing) in order to reflect the meaning of that value, such as:
0: Current
1: 30 Days
2: 60 Days
and so on...
What I need is a script that will look at the value in AccountTerms and will then update ulARAgeing to show the word value shown above. How do I do this?
I am going to try to explain this in a simple manner as much as possible so it's easy to understand :
Let's assume, you have a table Vendor setup something like this:
create table Vendor (AccountTerms int, ulARAgeing varchar(50));
And, then we will insert some sample values for both columns in Vendor table:
insert into Vendor values
(0,'Test'),
(1,'Test1'),
(2,'Test2');
Next, we will write an update statement to update your ulARAgeing column based on the values in AccountTerms column in the same table:
update vendor
set ulARAgeing = (CASE
WHEN AccountTerms = 0
THEN 'Current'
WHEN AccountTerms = 1
THEN '30 Days'
WHEN AccountTerms = 2
THEN '60 Days'
END);
CASE WHEN is similar to using IF..ELSE statement in most other programming languages. So, here we will be updating the existing ulARAgeing value to different string value based on the condition in the case when statement. So, for e.g. if the AccountTerms = 0 then we will update the value for ulARAgeing to `Current' and so forth.
To check if the above statement worked correctly, you just need to run the update statement above and then select from the table again:
select * from Vendor;
Result:
+--------------+-----------------+
| AccountTerms | ulARAgeing |
+--------------+-----------------+
| 0 | Current |
| 1 | 30 Days |
| 2 | 60 Days |
+--------------+-----------------+
SQL Fiddle Demo
Assuming you want a simple script to update, then it would be like this:
update
Vendor
set ulARAgeing = 'Current'
where AccountTerms = 0;
Assuming you want a script where it automatically update the column from a logic of numeric progression. Then it would be like this:
;WITH CTE
AS (select
AccountTerms
,ulARAgeing
,CONCAT((AccountTerms * 30), ' Days') as _ulARAgeing
from
Vendor)
UPDATE CTE
SET ulARAgeing = _ulARAgeing;
If by chance the value of "ulARAgeing" come from another table, then the script using "; WITH", you must use a join to get the correct value, instead of using a logic of progression.

How to select multiple rows based on AND condition on a column?

I am trying something like:
select name from myTable where id in (1056,1066,1069,1080,1404,1406,1407,3018)
But this is acting like OR condition and if I use AND between these values it does not give any result as it is trying to match in one cell.
Sample Data:
Name ID : abc 1 |abc 2| abc 3| def 2| fgh 3
My expected output is:
Name: abc
I want to find all the distinct names
having all corresponding ID's
Any pointer on this?
It seems you are confused on Logic, if you use AND then condition becomes false because there will not be any single row with ID having 1056 AND 1066.
for eg. suppose you have this table
ID name level
----------------
1056 abc bce
1066 bcd def
select name from myTable where id =1056 AND id=1066
now evaluate this with first row replacing id
1056 =1056 AND 1056=1066 (True AND False) is False
1066 =1056 AND 1066=1066 (False AND True) is False
So condition never becomes true so no rows are selected
Update
As per as your updated question, write like this
select distinct name from myTable where id in (1056,1066,1069,1080,1404,1406,1407,3018)
Ew. Multiple values in the same field?
select name from myTable
where id like '%1056%'
or id like '%1066%'
or id like '%1069%'
etc
Logically there is no output of your query, but if you want to use multiple ANDs once without writing writing AND multiple times, you can use Row Subqueries:
select name
from myTable
where (id,id,id,id,id,id,id,id)=(1056,1066,1069,1080,1404,1406,1407,3018)
is equal to:
select name
from myTable
where id =1056
AND id =1066
AND id =1069
AND id =1080
AND id =1404
AND id =1406
AND id =1407
AND id =3018
For the problem of this type, the best solution I could is use divide feature of Relational Algebra.
It will give the expected result.
Sample Example:-
http://coronet.iicm.tugraz.at/Dbase1/scripts/rdbh06.htm

SQL query for two values of one row based off same table column

I have two columns of one row of a report that I would like to be based off the same one column in a SQL table.
For example, in the report it should be something like:
ID | Reason | SubReason
1 | Did not like | Appearance
In the SQL table it is something like:
ID | ReturnReason
1 | Did not like
1 | XX*SR*Appearance
1 | XX - TestData
1 | XX - TestData2
The SubReason column is being newly added and the current SQL query is something like:
SELECT ID, ReturnReason AS 'Reason'
FROM table
WHERE LEFT(ReturnReason,2) NOT IN ('XX')
And now I'd like to add a column in the SELECT statement for SubReason, which should be the value if *SR* is in the value. This however won't work because it also has 'XX' in the value, which is omitted by the current WHERE clause.
SELECT t.ID, t.ReturnReason AS 'Reason',
SUBSTRING(t1.ReturnReason,7,10000) as 'SubReason '
FROM t
LEFT JOIN t as t1 on t.id=t1.id and t1.ReturnReason LIKE 'XX*SR*%'
WHERE t.ReturnReason NOT LIKE 'XX%'
SQLFiddle demo