remove everything between parentheses bigquery - google-bigquery

Stringfield1 has the following
Sales Support (130)
Product Support (110)
IT
needs to be
Sales Support
Product Support
IT
trying
regexp_substr(Stringfield1 ,'\((.*?)\)',1,1,null,1)
but I can't seem to get it to work, what am I doing wrong?
using big query

Use regexp_replace(Stringfield1, r'\(\d+\)$', '')
if applied to sample data in your question - output is

Related

Amazon Marketing Cloud - CAST Function and SQL Documentation

I am using Amazon Marketing Cloud (AMC) for work and I am having trouble applying a WHERE [column] <> ''.
The CSV file that is output is large and it contains many records with nothing in the main ID column. I am able to filter out the nulls, but not the ''.
This is the error message I get when I CAST: "No match found for function signature type(<RecordType(BIGINT order, VARCHAR campaign)>)"
The field is compiled in a CTE using NAMED_ROW('order', ROW_NUMBER() OVER(PARTITION BY imp_user_id ORDER BY impression_timestamp),'campaign', campaign) AS campaign_order.
Then, the next CTE turns it into an array using ARRAY_SORT(COLLECT(distinct a.campaign_order)) AS path.
An example of the output is [[1, <Name of Campaign 1>],[2, <Name of Campaign 2>],...[N, <Name of Campaign N>]]
I know that AMC is based on Presto Database Engine, but when looking for documentation I am not sure whether to look at Presto, Hive, or Apache. Whenever I search for something one of those 3 sources usually comes up. I have luck sometimes and other times I do not. It would help if I knew exactly what form of SQL AMC was using so I can narrow down the documentation, syntax, etc.
This platform is still in beta I believe and is relatively exclusive in terms of access. So, I am not sure if many people will be able to help.
In short, I want to filter out records = '', but due to the data type of the field it won't let me.
How do I cast "type(<RecordType(BIGINT order, VARCHAR campaign)" so that I can filter out ''? Also, what documentation should I be using for AMC?
I am considering using LEN() so that I can filter out any records with length = 0.
Anyways, any and all help is appreciated!
If you need more information, then please let me know.
Thank you!
AMC documentation is available at https://advertising.amazon.com/marketing-cloud/documentation (Amazon Advertising account with AMC access is required).
In general AMC SQL is closer to PostgreSQL rather than Presto syntax.
It's a little hard to see why you are trying to cast a record to a string. I think it might be easier to filter the records before the window function is applied. I might be able to help more if you share the relevant part of your query. Alternatively, feel free to contact AMC support by email or via your sales rep.

Which function can I use to find AVERAGE of a set of numbers in SQL?

I am very new to SQL and I am still trying to understand it. I was trying to find the average of a set of numbers but I don't know which function to use. In the SELECT statement , I wrote SELECT AVG as was written in the example I was working on and it returned error message 'Table Not Found'.
From the error message you gave, I think you have misunderstood how the avg keyword works.
Select avg(<the value you want to check the average of>)
FROM <table name here>;
Example
SELECT AVG(Price)
FROM Products;
Please note
As the comments on your threat states, please provide us with what SQL database you are using. Are you using PostgresSQL, mysql etc. Different SQL databases have different features and I can be helpful for us to know what you are using 😊
Also, if you can provide us with the information on the table (which fields the table contains) we can provide you with a more specific SQL query 😊
Here is a great tutorial that might help you further!
https://www.w3schools.com/sql/sql_count_avg_sum.asp
Hope this helps 😊

Bigquery CASE SENSITIVE query with LIMIT clause is not working?

When making a Bigquery query like for example:
Select Campaign FROM TABLE WHERE Campaign CONTAINS 'buy' GROUP BY Campaign IGNORE CASE LIMIT 100
The IGNORE CASE clause is not working when used with LIMIT clause.
Some time ago it did work.
Is this a Bigquery fault or something changed?
Thanks a lot
Ramiro
A couple of things here:
Legacy SQL expects IGNORE CASE to appear at the end of the query, so you need to use LIMIT 100 IGNORE CASE instead of IGNORE CASE LIMIT 100
The BigQuery team advises using standard SQL instead of legacy SQL if you're working on new queries, since it tends to have better error messages, better performance, etc. and it's where we're focusing our efforts going forward. You may also be interested in the migration guide.
If you want to use standard SQL for your query, you could do:
Select LOWER(Campaign) AS Campaign
FROM TABLE
WHERE LOWER(Campaign) LIKE '%buy%'
GROUP BY LOWER(Campaign)
LIMIT 100

Using sql subquery in Tableau

I trying to calculate a new field in Tableau similar to a sub query in SQL. However, my numbers not matching up when I try to do this. I am stuck at this point and I am trying to see what others have done.
To provide reference, below is the subquery that I am trying to duplicate in Tableau.
select
((sum(n.non_influenced_sales*n.calls)/sum(n.calls))-(sum(n.average_sales*calls)/sum(calls))))/
(sum(n.non_influenced_sales*n.calls)/sum(n.calls)) as impact
from (select
count(d.id) as calls
,avg(d.sale) as average_sales
,avg(case when non_influenced=1 then d.sale else null end) as non_influenced_sales
from data d
group by skill) n
When I try to build the same calculation in Tableau, I am able to get the same results as long as I comment out the group by skill. However, when I try to group by skill, my attempts to match the number have not been working.
The closest I have come is when I try to fix the level of detail expression by using include. Tableau code:
(include [skill]:([non_influenced_sales]-[average_sales])/[non_influenced_sales]}
However, doing this or using fixed has not worked and I can't match the numbers I am getting from SQL.
FYI, Impact is an aggregated measure. I built the sub-query part in tableau by just creating separate fields for the calculation I needed. So for example
Non Influenced Sales calculated in Tableau:
avg(if [non_influenced]=1 then [non_influenced_sales] end)
However, I am not sure if this matters or not.
I have also tried creating custom sql. I am able to get a rolled up version using all of the dates correct. But when I want to get down to different dates/use other filters, things get messy real quick. I am trying to build relationships on a date level, but that hasn't worked either.
Is there an easier way to do this?

BigQuery calculate grand totals row

Is there any way to add row with grand totals under my result set, like ms-sql ROLLUP operator? I want to avoid using extra query for this totals.
Edited: BigQuery now supports the ROLLUP operator. See the query reference docs for more information.
Update: Since this question was posted, BigQuery did implement support GROUP BY ROLLUP clause compatible with SQL Standard. It is documented here: https://cloud.google.com/bigquery/query-reference#groupby