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
4
votes
2 answers

R: coalescing a large data frame

Say I create a data frame, foo: foo <- data.frame(A=rep(NA,10),B=rep(NA,10)) foo$A[1:3] <- "A" foo$B[6:10] <- "B" which looks like, A B 1 A 2 A 3 A 4 5 6 B 7 B 8
Dan
  • 11,370
  • 4
  • 43
  • 68
4
votes
2 answers

Coalescing aggregate of fields in postgres

I'm trying to group by a name field, but with a 'coalescing aggregate' of the other values a row might have. Here's the data: name | jurisdiction | db_from ------------|-----------------|---------------- my station NULL …
J-DawG
  • 1,035
  • 9
  • 24
4
votes
2 answers

Using COALESCE with different data types?

I have a query using COALESCE(timestamp_type::date,charachter_varying) which fails due to the mismatched data types: ERROR: COALESCE types date and character varying cannot be matched It works if i cast the timestamp as…
Don
  • 3,876
  • 10
  • 47
  • 76
4
votes
1 answer

Coalesce and Concat in the same statement, Postgres

I am trying to concat an individual's first and last name together but coalesce a team name when there is a null value. Unfortunately my syntax is returning a SPACE, so coalesce does not recognize it as a null value.. What can I do to correct this?…
Lewisj
  • 41
  • 1
  • 2
4
votes
1 answer

Postgres: COALESCE all columns

I'm dealing with a table that has way too many columns for good design. (Close to 100). I can't change the schema. I need to COALESCE each column to be 0 on NULL. Is there a way to do this without typing out all 100 columns by hand? (Perhaps…
SRobertJames
  • 8,210
  • 14
  • 60
  • 107
4
votes
2 answers

Why use COALESCE() in sql statement?

I'm refactoring some old code and stumbled upon this named query (It's using hibernate on top of mysql): delete F from foo F inner join user DU on F.user_id = DU.id where (COALESCE(:userAlternateId,null) is null or DU.alternate_id like…
3vlM33pl3
  • 537
  • 5
  • 14
4
votes
1 answer

combine CONCAT() and COALESCE() to generate JSON in MySQL

I'm building small JSON blocks from functions and I need to filter null values while quoting non-null values like this (MySQL 5.0, so no built-in JSON functions): COALESCE(CONCAT('[', group_concat( CONCAT('{ "key": "', REPLACE(a.val,…
Preuk
  • 632
  • 7
  • 18
4
votes
1 answer

Using coalesce in joins

I'm writing a query that lists all the challenges (+ score) a user has taken today, or in case he hasn't taken any challenge on this day that outputs the last challenge he has ever taken. I've written the following query, that seems to give me what…
Clémentine
  • 468
  • 1
  • 5
  • 16
4
votes
1 answer

How to use coalesce, or a variation, to pull more than 1 value?

This is a question about COALESCE in PostgreSQL. In a current view I am using COALESCE to grab the first NOT NULL value in a list: COALESCE(vw_header_to_node_13.subsetname, vw_header_to_node_12.subsetname,…
user3329160
  • 165
  • 2
  • 13
4
votes
1 answer

Why does COALESCE return multiple Invalid Column Name errors for a single column?

When writing a SQL query using COALESCE, I've noticed that it will, under specific circumstances, throw multiple errors for a single invalid column name. If I write the following query and try to execute it, I receive a single error message…
AHiggins
  • 7,029
  • 6
  • 36
  • 54
4
votes
2 answers

MySQL return default value if dataset is empty

I am trying to query a database using: SELECT coalesce(value, "NA") as value FROM mytable FORCE INDEX (chrs) FORCE INDEX (sites) WHERE chrom = 1 AND site = 120 This query works fine if I have an entry in the database. However, if the entry is not…
user2165857
  • 2,530
  • 7
  • 27
  • 39
4
votes
2 answers

SQL query to fetch products with most top 1 most recent price change

I'm using SQL Server 2005. Let's say I have a table for products and another table for prices so that I can track price changes over time. I need a query that fetches distinct products (easy part) plus each product's most recent price and the date…
Byron Sommardahl
  • 12,743
  • 15
  • 74
  • 131
4
votes
1 answer

T-SQL:Transform variable number of rows into columns

When I run a SQL query which returns one column with variable number of rows returned, I'd like to transform each of the row into column VALUE (I don't mind what the column header/titles are). E.g. Column1 ------- a b c d e I want a script which…
viv_acious
  • 2,429
  • 9
  • 34
  • 55
4
votes
3 answers

Select multiple rows from SQL into one row in subquery

I have query like: DECLARE @razem VARCHAR(MAX); SELECT Ordering.orderID , Document.number, (User_info.name +' '+ User_info.surname), Ordering.dateStart, Ordering.dateEnd , ( select …
user13657
  • 745
  • 3
  • 17
  • 36
4
votes
2 answers

Is coalescing triggered for accessing memory in reverse order?

Let's say I have several threads and they access memory at addresses A+0, A+4, A+8, A+12 (each access = next thread). Such access is coalesced, right? However if I have access the same memory but in reverse order, meaning: thread 0 -> A+12 thread 1…
greenoldman
  • 16,895
  • 26
  • 119
  • 185