I have a raw table in bigquery, and I want to add a "conditional"/"calculated" column based on a given value, saying something like:
CASE
WHEN `columnA`="valueX" THEN `columnB`
ELSE NULL
AS `valueX`
The problem is that I can't just say SELECT *, CASE..., since then I must do GROUP BY over all of the columns.
Is there a simple way to create a "calculated" column like that ? Maybe using some UDF and views "tricks" ?
Input example:
Output example:
Thanks In advance,
Shushu
SELECT *,
CASE
WHEN columnA='valueX' THEN columnB
ELSE NULL
END AS valueX
FROM
Related
How can I (or is it possible?) use something like CASE statement in T-SQL inside QUERY in Google spreadsheet?
I want to do something like this:
=QUERY(A6:AI,"select A,(CASE WHEN D>2.5 THEN 'Yes' ELSE 'No' END),E,C,J")
I want basically a custom column with Yes/No values based on other column. Is it possible?
EDIT: To better explain, I have data in table Existing table and I would like to transform it to the Transformed table using QUERY statement:
So I need something to say: if column D is empty, print No, otherwise print Yes. This has to be in the QUERY because it's not the last column, there will be more data after column Finished. So I have this:
=QUERY(A4:D,"Select A, B, (CASE WHEN D='' THEN 'No' ELSE 'Yes' END)") - But that doesn't work
Thank you for help,
CASE & THEN are not supported in google's query language
try:
=INDEX({A6:A, IF(D6:D>2.5, "yes", "no"), E6:E, C6:C, J6:J})
Input table looks as below which has various conversions, but reported only on one conversion (defined in report_field)
Output table has reported_conversion which is derived based on report_field
What's the best way of coding to achieve this?
CASE expression could be used:
SELECT *
,CASE report_field
WHEN 'conversion1' THEN conversion1
WHEN 'conversion2' THEN conversion2
WHEN 'conversion3' THEN conversion3
END AS reported_conversion
FROM tab;
or DECODE
SELECT *, DECODE(report_field,
'conversion1', conversion1
'conversion2', conversion2
'conversion3', conversion3)
FROM tab
How do I get the column "count(division)" instead of getting the actual number of counts?
select * from num_taught;
gets me this
select count(division) from num_taught;
gets me this, but I actually want the third column "count(division)" from the previous image
I want to know this because I'm doing this right now:
sql> select * from num_taught as a, num_taught as b
...> where a.count(division) = b.count(division);
Error: near "(": syntax error
but as you can see, there's a syntax error and I think it's because the code is not referencing the "count(division)" columns but actually finding the count instead.
My end goal is to output the "Titles" that have the same "Division" and have the same count(division).
So for example, the end table would have the rows "Chief Accountant", "Programmer Trainee", "Scrivener", "Technician", "Wizard". Since these are the rows that have a match in division and count(division)
Thanks!
What does DESC num_taught return? I am curious how the third column is populated - is it some kind of pseudo-column? You may want try wrapping the column name with [], see: How to deal with SQL column names that look like SQL keywords?
i.e. try:
select [count(division)] from num_taught;
You need to escape your column name using quotes (in case it's Sqlite like you mentioned in the comments).
select "count(division)" from num_taught;
or:
select * from num_taught as a, num_taught as b
where a."count(division)" = b."count(division)";
If you don't you are using the count-function provided by your Database-system.
It's very unusual to name a column like this, it might be either a trap by your tutor or an error while initializing the table in your case.
I think you just want a count(distinct):
select count(distinct division)
from num_taught;
I have one column that called 'message' and includes several data such as fund_no, detail, keywords. This column is in table called 'trackemails'.
I have another table, called 'sendemails' that has a column called 'Fund_no'.
I want to retrieve all data from 'trackemail' table that the column 'message' contains characters same as 'Fund_no' in 'trackemails' Table.
I think If I want to check the equality, I would write this code:
select
case when t.message=ts.fund_no then 1 else 0 end
from trackemails t, sendemails s
But, I do want something like below code:
select
case when t.message LIKE ts.fund_no then 1 else 0 end
from trackemails t, sendemails s
I would be appreciate any advice to how to do this:
SELECT *
FROM trackemails tr
INNER JOIN sendemail se on tr.Message like '%' + se.Fund_No + '%'
Dear Check SQL CHARINDEX() Function. This function finds a string in another string and returns int for the position they match. Like
SELECT CHARINDEX('ha','Elham')
-- Returns: 3
And as you need:
SELECT *
,(SELECT *
FROM sendemail
WHERE CHARINDEX(trackemails.Message,sendemail.Fund_No)>0 )
FROM trackemails
For more information, If you want something much better for greater purposes, you can use Fuzzy Lookup Component in SSDT SSIS. This Component gives you a new column in the output which shows the Percentages of similarity of two values in two columns.
I have a column with many nulls in table 1, but now and then there is a value. If there is a value, I want to go to table 2 and take the the corresponding value from there. This should create an extra column called user_original:
CASE
WHEN table1.user_changed IS NOT NULL
THEN table2.user_original
ELSE -- do nothing
END as user_original
I basically want to replace
is nothing
Is this correct ? How can this be done? Is there a better way?
do you mean you need below
CASE
WHEN table1.user_changed IS NOT NULL
THEN table2.user_original
END as user_original
Do you want a correlated subquery?
(CASE WHEN table1.user_changed IS NOT NULL
THEN (SELECT table2.user_original FROM table2 WHERE table2.? = table1.?)
END) as user_original
The ? is for the columns that identifying "corresponding" values.
If this is the case, you can probably simplify this to:
(SELECT table2.user_original FROM table2 WHERE table2.? = table1.?) as user_original
My guess is there will be no matching value if the original value is NULL.