BigQuery equivalent of COALESCE()? - google-bigquery

I'm in the process of converting some aggregate queries from Postgres to our new architecture in BigQuery. Is there an equivalent of COALESCE() in BigQuery?
Currently, I am converting a Postgres query statement like
coalesce(column1,'DEFAULT')
to
CASE
WHEN column1 IS NOT NULL
THEN column1
ELSE 'DEFAULT'
END AS column1
which seems easy enough.
However converting a Postgres query statement with nested coalesce statements like
count(distinct coalesce(
coalesce(
coalesce(column1,column2),
column3),
column4)))
would get much more messy if I used CASE statements all over the place, and also seems like the wrong thing to do.
Does BigQuery have a method equivilent to COALESCE(), or am I stuck writing the whole CASE statement equivalent?

You can use IFNULL function in BigQuery, which can be nested like that:
select ifnull(column1,
ifnull(column2,'DEFAULT'))
from
(select string(NULL) as column1, 'y' as column2)
P.S. But omission of COALESCE in BigQuery is an oversight, I will fix it.
Update: As of 4/16/2015, COALESCE is available in BigQuery.
https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#conditional_expressions

Related

Equivalent to minus in netezza

I want to compare data between two different db tables in netezza. In oracle we can do that by minus operator. How can the same operation be done in netezza.
SELECT CUSTOMER_SRC_ID,CUSTOMER_SRC_DESC FROM CIDB_SIT..CUSTOMER_SRC
MINUS
SELECT CUSTOMER_SRC_ID,CUSTOMER_SRC_DESC FROM EDW_SIT..CUSTOMER_SRC
Seems like it doesn't work in netezza. Can any one help me find the equivalent query in netezza?
The ANSI-SQL standard calls this operators except. Netezza implements it, as do PostgreSQL and MS SQL Server:
SELECT CUSTOMER_SRC_ID,CUSTOMER_SRC_DESC FROM CIDB_SIT..CUSTOMER_SRC
EXCEPT -- Here
SELECT CUSTOMER_SRC_ID,CUSTOMER_SRC_DESC FROM EDW_SIT..CUSTOMER_SRC
You could use the EXCEPT
or
--if customer_src_id is unique--
SELECT CUSTOMER_SRC_ID,CUSTOMER_SRC_DESC
FROM CIDB_SIT..CUSTOMER_SRC
WHERE CUSTOMER_SRC_ID NOT IN (SELECT CUSTOMER_SRC_ID FROM EDW_SIT..CUSTOMER_SRC);

Multiple NVL() alternative - first not null parameter

Currently I have something like that:
NVL(COL1, NVL(COL2, NVL(COL3, NVL(COL4, NVL(COL5, COL6)))))
Is in Oracle 11gR2 any function that returns first NOT NULL parameter ?
Use COALESCE() function , it returns first non null value in expression list.
SELECT COALESCE(col1,col2,col3,col4,col5,col6)
FrOM tableName
Perhaps you are looking for COALESCE()?
Note that COALESCE() is supported on almost all khown databases: Oracle, PostgreSQL, MySQL, MSSQL, SQLite.

SQL Server: Best way to concatenate multiple columns?

I am trying to concatenate multiple columns in a query in SQL Server 11.00.3393.
I tried the new function CONCAT() but it's not working when I use more than two columns.
So I wonder if that's the best way to solve the problem:
SELECT CONCAT(CONCAT(CONCAT(COLUMN1,COLUMN2),COLUMN3),COLUMN4) FROM myTable
I can't use COLUMN1 + COLUMN2 because of NULL values.
EDIT
If I try SELECT CONCAT('1','2','3') AS RESULT I get an error
The CONCAT function requires 2 argument(s)
Through discourse it's clear that the problem lies in using VS2010 to write the query, as it uses the canonical CONCAT() function which is limited to 2 parameters. There's probably a way to change that, but I'm not aware of it.
An alternative:
SELECT '1'+'2'+'3'
This approach requires non-string values to be cast/converted to strings, as well as NULL handling via ISNULL() or COALESCE():
SELECT ISNULL(CAST(Col1 AS VARCHAR(50)),'')
+ COALESCE(CONVERT(VARCHAR(50),Col2),'')
SELECT CONCAT(LOWER(LAST_NAME), UPPER(LAST_NAME)
INITCAP(LAST_NAME), HIRE DATE AS ‘up_low_init_hdate’)
FROM EMPLOYEES
WHERE HIRE DATE = 1995
Try using below:
SELECT
(RTRIM(LTRIM(col_1))) + (RTRIM(LTRIM(col_2))) AS Col_newname,
col_1,
col_2
FROM
s_cols
WHERE
col_any_condition = ''
;
Blockquote
Using concatenation in Oracle SQL is very easy and interesting. But don't know much about MS-SQL.
Blockquote
Here we go for Oracle :
Syntax:
SQL> select First_name||Last_Name as Employee
from employees;
Result: EMPLOYEE
EllenAbel
SundarAnde
MozheAtkinson
Here AS: keyword used as alias.
We can concatenate with NULL values.
e.g. : columnm1||Null
Suppose any of your columns contains a NULL value then the result will show only the value of that column which has value.
You can also use literal character string in concatenation.
e.g.
select column1||' is a '||column2
from tableName;
Result: column1 is a column2.
in between literal should be encolsed in single quotation. you cna exclude numbers.
NOTE: This is only for oracle server//SQL.
for anyone dealing with Snowflake
Try using CONCAT with multiple columns like so:
SELECT
CONCAT(col1, col2, col3) AS all_string_columns_together
, CONCAT(CAST(col4 AS VARCHAR(50), col1) AS string_and_int_column
FROM table
If the fields are nullable, then you'll have to handle those nulls. Remember that null is contagious, and concat('foo', null) simply results in NULL as well:
SELECT CONCAT(ISNULL(column1, ''),ISNULL(column2,'')) etc...
Basically test each field for nullness, and replace with an empty string if so.

Can SQL SUM() function take an expression as argument?

I'm using SQLite database and I'm wondering whether I'm allowed to write queries as follows:
SELECT SUM(column1 * column2)
FROM my_table;
I googled but references say that SUM function is has the following format:
SUM([DISTINCT|ALL] column)
And my question is: does column mean actually column or does it allow expressions (like above) too?
You can always use a table expression:
SELECT SUM(Calc)
FROM (
SELECT Column1 * Column2 AS 'Calc'
FROM My_Table) t
I don't have SQLite but checking the docs indicates this should work fine.
Yes, you can use an expression like the one you mentioned, if the datatype of both columns allow it.

SQL query where clause is too long

I've written an SQL query with a lot of 'or's in the 'where' clause:
i.e.
"SELECT * FROM myTable WHERE col1='a' or col1='b' or col1='c'...etc"
I'm trying to run a query in access via vb.net, but I keep getting "Query is too complex" error message.
I'm guessing I've hit some maximum limit. Anyone know a way around this, other than just to break it down into multiple queries?
How about using the IN operator instead?
SELECT Field1, Field2
FROM Table1
WHERE Field1 IN('Val1','Val2', ....)
If you query is that simple would you not be better using
SELECT * FROM myTable WHERE col1 in ('a','b','c')
but it would help to post the actual query so we can give a accurate answer
You could use SQL IN operator instead having multiple OR conditions.
You can use IN as in -
SELECT * FROM myTable WHERE col1 IN ('a','b','c','d');
Many have stated that you should use the IN-operator instead, but when that is used together with constants I believe the optimiser simply converts that to an OR-operator.
Instead you could load a temporary table with your constants and then use the IN-operator with that table.