Questions tagged [coalesce]

COALESCE is a SQL function that returns the first non-NULL expression among its arguments. COALESCE() is ANSI standard and may be used instead of vendor-specific alternatives such as ISNULL(), NVL() or IFNULL().

Return the first non-NULL argument. The COALESCE(expression1, expression2... expressionN) function is variety of the CASE expression.

Examples

Consider the expression:

CASE
   WHEN (expression1 IS NOT NULL) THEN expression1
   WHEN (expression2 IS NOT NULL) THEN expression2
   ...
   ELSE expressionN

It is equivalent to

COALESCE(expression1, ... expressionN);

Another usage:

SELECT
  field1,
  COALESCE(SUM(customers), 0), -- if the SUM is NULL, it would be calculated as 0
  field3,
FROM
  ...

References

932 questions
5
votes
3 answers

C# coalesce operator doesn't replace a null method return value?

I have this code: MyClass _localMyClass = MyClassDAO.GetMyClassByID(123) ?? new MyClass(); This is the method: public static MyClass GetMyClassByID(int id) { var query = from m in ctx.MyClass where m.MyClassID ==…
5
votes
1 answer

MySQL group by with rollup, coalesce/ifnull, and date functions

I have run into an issue with MySQL's ROLLUP and dealing with the resulting NULLs. The IFNULL/COALESCE functions work well with plain columns, but seem to break down when used with date functions. Examples follow: SELECT YEAR(date_time) AS…
Dmitriy
  • 51
  • 1
  • 5
5
votes
3 answers

How to use coalesce in ssis

I am using MS SSIS 2008 and I want to perform COALESCE function in DERIVED COLUMN function. Could you help me how to write the expression?
Martin Lučan
  • 51
  • 1
  • 4
5
votes
0 answers

Coalesce with BigDecimal in QueryDsl

I'm trying to do a Coalesce using QueryDSL without any luck. Here is what I want to do: I have some Order objects which have OrderItem objects. Each OrderItem has a price (BigDecimal). For each Order, I want to have the ID of the Order as well as…
Nelson
  • 51
  • 1
  • 4
5
votes
1 answer

Usage of Coalesce or Case statement in JPA

How to use coalesce or case statement in JPA 2 using CriteriaBuilder For many records initiatedBy will be empty and as a result employeeName will be null for those records. I would like to display System Generated if employeeName is null for those…
Jacob
  • 14,463
  • 65
  • 207
  • 320
5
votes
2 answers

Optimize a max() aggregate that works for "no rows", too

I have a query that I am trying to optimize for PostgreSQL 9.2: select coalesce(max(id),0) as m from tbl It takes for forever to run, so I figured I could rewrite it as select id from tbl order by id desc limit 1 except it needs to return 0 if…
5
votes
5 answers

SQL how to find non null column?

I have a table with lots of columns, say I have columns A, B, C, D in each of these columns, only one column in any one record will be filled and the others will always be NULL. I need a select statement that will return the Column of the non…
Darknight
  • 2,460
  • 2
  • 22
  • 26
4
votes
2 answers

Computed Column (COALESCE vs CASE vs ISNULL)

I posted a similar question a while back and now as I need to update this code, I am back to ask a follow-on question. Previous question is here: Computed column based on nullable columns My data (Address1, Address2, City, State, Zip, Country) may…
Ed Sinek
  • 4,829
  • 10
  • 53
  • 81
4
votes
2 answers

How to perform a rolling coalesce of columns in R

I've got a dataframe like this one: stage1 stage2 stage3 stage4 a NA b c NA d NA e NA NA f g NA NA NA h Where each column is a stage from a process. What I want to do is to coalesce each…
Juan C
  • 5,846
  • 2
  • 17
  • 51
4
votes
4 answers

Combine 3 columns into 2 columns and omit NAs

I am trying to take 3 columns and combine them into 2 columns. For each row, only two of the three columns have data (and the other is NA). My first thought was to use coalesce but I can't get it to work for my case. tibble( col1 = c(NA, 1, 0), …
dgetty
  • 43
  • 4
4
votes
1 answer

Saving dataframe to json file with a specific name without creating partition files in Pyspark

I have a dataframe which I want to write it as single json file with a specific name. But it is creating a partitioned file within the stated filename. How do I get this to write data directly to the filename I passed into my parameter? Below is the…
Young
  • 41
  • 2
4
votes
1 answer

MYSQL 5.7.20 - Left join with order by a coalesced column - very strange behavior

I'm facing a very strange issue that I hope you can explain to me. What I'm trying to do is order a resultset based on a coalesced column in a subquery. Let me explain better. I have two tables: CREATE TABLE `user` ( `id` int(11) NOT NULL…
Omagerio
  • 495
  • 2
  • 11
4
votes
1 answer

Arel - How to coalesce a field and a string literal in an Arel query?

I inherited a big, complicated Arel query that pulled from a number of tables. A new requirement says that if one of those tabels doesn't have a value for a particular field ("district"), I should default it to, "Global". Because of high levels of…
David Hempy
  • 5,373
  • 2
  • 40
  • 68
4
votes
1 answer

Strange behavior in a SQL left join

I am posting this question in response to answer I recently wrote on this question. The question involves two tables: Base: ID Field A 1 B 2 D NULL Join: ID A B C D And here is the desired output: Output: ID Newfield A 1 B 2 C …
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
4
votes
5 answers

Coalesce over Rows in MSSQL 2008,

I'm trying to determine the best approach here in MSSQL 2008. Here is my sample data TransDate Id Active ------------------------- 1/18 1pm 5 1 1/18 2pm 5 0 1/18 3pm 5 Null 1/18 4pm 5 1 1/18 5pm 5 …
Bo Flexson
  • 481
  • 1
  • 6
  • 9