How to combine IF and REGEXP_CONTAINS in bigquery - google-bigquery

I am fairly new to BigQuery and SQL. I am trying to figure out if it is possible to combine IF and REGEXP_Contains.
I want to apply different calculations based on a campaign name.
So If Campaign Name contains 'ABC' THEN, (A + C) As Cost, if Campaign Name is Not 'ABC' Then, (A + B + C) As Cost.
I tried this:
CASE
IF REGEXP_CONTAINS(lower(CampaignName),'ABC'),(A + C), (A+B+C) END AS Cost,
But got this error message:
Syntax error: Expected "(" but got identifier "REGEXP_CONTAINS"
What have I done wrong here?
Thank you for your help!

It could be done two way using If() or case as below:
IF (REGEXP_CONTAINS(CampaignName,'ABC'),(A + C), (A+B+C) ) AS Cost
or
case when REGEXP_CONTAINS(CampaignName,'ABC') then (A+C) ELSE
(A+B+C) END AS Cost

Related

How to use sum, replace, and case in the same setting?

I have a table where numbers are stored as strings and some bigger numbers look like 1,634.5 so I have to replace the "," with blanks to get accurate data.
I have been using this which works, but I can't figure out how to do that with sum and case as I need to sum up the revenue together when it equals xyz
code that works
(replace(revenue, ',', '')
code that doesn't work
sum(replace(revenue, ',', '') case WHEN food in ('burgers', 'fries') then revenue else 0 end)) as foodsum
group by foodcategory
i also tried using a nested query but didn't work, does anyone have any recommendations?
thanks!
Here's the correct way on aggregating your revenue based on foodcategory.
select case when food in ('burgers', 'fries') then sum(replace(revenue, ',', '')) else 0 end as foodsum
from table1
group by foodcategory

How to filter out a column value when another column contains a value in SQl

How can I filter out a value in one column if a value in another column is present? I'm trying to remove all C's in the CODE column if it is accompanied by a '-' in the Description column.
CODE | Description
A -
A -
B -
B -
C stuff
C -
In the 'CODE' column, I want to remove the values 'C' if the Description = '-'.
The end result would look like the table below with the C & - removed.
CODE | Description
A -
A -
B -
B -
C stuff
Of note, the Description column is a custom field using CASE WHEN.
In excel, I would just create a dummy conditional column and then filter 'C' &'-' out. I'm not sure how to go about doing this in SQL.
One way to express this uses not:
where not (code = 'C' and Description = '-')
This seems quite close to the logic as your express it.
This is equivalent to:
where code <> 'C' or Description <> '-')
Note that both these solutions assume that code and description are not NULL. They can be modified to handle that as well, but that does not seem necessary based on the data in the question.
Try:
SELECT CODE, Description
FROM your_table_name
WHERE (CODE Not Like 'C' And Description Not Like '-');
Also, I would advise keeping the field/column names of the same case such as 'Code' and 'Description' or 'CODE' and DESCRIPTION'. It is a much better practice.
select
t1.Code,
t1.Description
from MyTable t1
where t1.code not in (select code from MyTable where Description not like '-')
union All
select * from MyTable
where Description not like '-'
order by code asc
primary:
result:

how to replace T-SQL CASE statement with efficient code

how can i replace the following case statement with efficient code
SELECT
CASE WHEN LEN(Code.Description)>=30
THEN left(Code.Description, 30) + '...'
ELSE NominalCode.Description END AS ShortDescription
FROM Code
Because the data set it returns is going to be 30-50,000 records and according to lead dev this code is ridiculous. I need help
Presumably, you intend:
SELECT (CASE WHEN LEN(c.Description) > 30
THEN left(c.Description, 30) + '...'
ELSE c.Description
END) AS ShortDescription
FROM Code c;
That is, the Description column references all come from the same table. Your code is fine.
However, I would adjust the semantics so the resulting string always has the same length:
SELECT (CASE WHEN LEN(c.Description) > 30
THEN LEFT(c.Description, 27) + '...'
ELSE c.Description
END) AS ShortDescription
FROM Code c;
Other than adding a ShortDesc field to your Product Table, perhaps I would try the following
Concat(left(Code.Description,30),IIF(Len(Code.Description)>30,'...',''))
or even
left(Code.Description,30)+IIF(Len(Code.Description)>30,'...','')
EDIT
As you can see the same execution plan, however, the performance of my approach was 18% better. This test was done on an isolated machine with a sample size of 30,000 records.

Can somebody explain this tricky sql?

what would the below sql return and why?
SELECT 1 + '+' + 2
Same reason SELECT 1 + 'whocares' + 2 results in 3. In some database systems, 'strings' evaluate to 0 if they can't be interpreted as a number. Try SELECT 1 + '2' + 3 to see an example of where it CAN be interpreted as a number.
And yes, the "in some database systems" applies to this whole answer, where I'm going to guess you're using something like MySQL.
One way of thinking about this type of mathematical logic is that SQL manages your math functions in a given order of operation.
SELECT 1 + 'whocares' + 2
-- Results in a number as the first item processed, 1, tells SQL it is about to deal with integers
SELECT 'whocares' + 1 + '...this guy'
-- Results in a string of 'whocares...this guy' as the first item is a string
Actually, the second line may fail depending on which SQL syntax/system you're using... but you get the point.

Is it possible to search for multiple terms in a column by using a LIKE statement?

I'm trying to understand if the above question is possible. I've been conceptually thinking about it, and basically what I'm looking to do is:
Specify keywords that may appear in a title. Lets use the two terms "Portfolio" and "Mike"
I'm hoping to generate a query that will allow for me to search for when Portfolio is contained within a title, or Mike. These two titles need not to be together.
For instance, if I have a title dubbed: "Portfolio A" and another title "Mike's favorite" I'd like both of these titles to be returned.
The issue I've encountered with using a LIKE statement is the following:
WHERE 1=1
and rpt_title LIKE ''%'+#report_title+'%'''
If I were to input: 'Portfolio,Mike' it would search for the occurrence of just that within a title.
EDIT: I should have been a bit more clear. I believe it's necessary for me to input my variable as 'Portfolio, Mike' in order for it to find the multiple values. Is this possible?
I'm assuming you could maybe use a charindex with a substring and a replace?
Yep, multiple Like statements with OR will work just fine -- just make sure you use the correct parentheses:
SELECT ...
FROM ...
WHERE 1=1
and (rpt_title LIKE '%Portfolio%'
or rpt_title LIKE '%Mike%')
However, I might suggest you look into using a full-text search.
http://msdn.microsoft.com/en-us/library/ms142571.aspx
I can propose a solution where you could specify any number of masks, without using multiple LIKE -
DECLARE #temp TABLE (st VARCHAR(100))
INSERT INTO #temp (st)
VALUES ('Portfolio photo'),('- Mike'),('blank'),('else'),('est')
DECLARE #delims VARCHAR(30)
SELECT #delims = '|Portfolio|Mike|' -- %Portfolio% OR %Mike% OR etc.
SELECT t.st
FROM #temp t
CROSS JOIN (
SELECT substr =
SUBSTRING(
#delims,
number + 1,
CHARINDEX('|', #delims, number + 1) - number - 1)
FROM [master].dbo.spt_values n
WHERE [type] = N'P'
AND number <= LEN(#delims) - 1
AND SUBSTRING(#delims, number, 1) = '|'
) s
WHERE t.st LIKE '%' + s.substr + '%'