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
-1
votes
1 answer

Issues selecting rows with next row's IDs

This question is a follow-up to Select first rank if RANK()+1 overflows with JOINS The scenario I have a bunch of predefined messages that my Discord and Twitch bots send to dedicated channels every 30 minutes in round-robin only if X messages where…
dan
  • 3,439
  • 17
  • 54
  • 82
-1
votes
1 answer

Get all results from the customer table. Replace null values in the state and fax columns with an empty string. How to solve this query?

I am very confused about this question .The thing is know that I can use COALESCE function for this but how ?Because there are 2 columns here ,State and Fax
-1
votes
3 answers

How can I coalesce this dataset in R?

I am an ICU physician conducting research which involves taking lots of patient-related data from the ICU computer system (all ethically approved, etc). As is often the case, getting data out then requires cleaning and wrangling before it can be…
-1
votes
3 answers

Calculate AVG in SQL Select statement when some values are NULL

I have struggled to find a solution to this problem, so any help would be hugely appreciated. I have two tables, lets call one table_a and one table_b, representing two possible sources of customer orders. They both contain a field called…
st87_top
  • 57
  • 1
  • 1
  • 7
-1
votes
1 answer

how wors coalesce function with sum

I got 2 table like it table 1 id cost 1 200 2 300 3 500 4 700 NULL NULL NULL NULL table 2 1 200 2 300 3 500 4 700 5 1000 6 2500 and I did it : sum(coalesce(table1.cost,table2.cost)) my aim is to…
sebastien
  • 7
  • 3
-1
votes
1 answer

combine the null columns to get desired concat in query

if my table looks like : id name country 1 kasis Nepal 2 samika 3 Australia How can I combine it to get something like : Concatenated(Desired) 1,kasis,Nepal 2,samika 3,Australia select id || coalesce(name,'') ||…
-1
votes
1 answer

Using joint statement with ifnull function MySQL

I just want to replace the reference id with name from item table in the result as I showed in last table 1st table and Parent table id item amount tax status 1 4 20 2 Y 2 5 15 1 N 3 6 5 0 N 2nd table and child…
Thimothi
  • 35
  • 5
-1
votes
1 answer

SQL - sum a column with common id, then coalesce total with another column

I know somebody can tell me what I'm doing incorrectly here. This is a postgresql db. I'm trying to first sum the values in a column that share the same ID, and then add that total (coalesce) with another column. First, this is how I total up the…
boog
  • 472
  • 1
  • 5
  • 23
-1
votes
1 answer

How would I go about joining or merging data frames of different sizes while also overwriting missing values in R?

R Question I am looking to join multiple data frames of unequal size. While joining the data frames, I would like to overwrite any NAs. I attempted to use the coalesce function, but equal sized data frames were required. Example x <- data.frame( …
-1
votes
2 answers

COALESCE + SUM in MYSQL incorrectly calculating

I am using COALESCE and SUM to get the total value of payments for each invoice. Below is relative data. SELECT COALESCE(SUM(p.payment_amount),0) as amount_paid FROM Payments as p WHERE p.invoice = '13923'; Below is database information…
-1
votes
1 answer

Need replace NULLs with previous value

It is necessary to join the table, but so that instead of NULL values, the last known values ​​are put down and multiplied I have 2 table "oper" и "curs" oper - have 2 column date(date), sum(int) curs - have 2 column"date"(date), "cur"(int) [1]:…
Rmrm
  • 17
  • 4
-1
votes
1 answer

How to correctly use group_concat to concatenate Join query result in MYSQL?

I have written the following MYSQL query and the The Mysql version is 8.0.18-commercial SELECT p.server, 'Type_ABC' AS Check_Type, COALESCE(vmtable.res) AS result FROM server p INNER JOIN truns t ON t.oq_id = p.oq_id AND t.id = (SELECT t2.id FROM…
meallhour
  • 13,921
  • 21
  • 60
  • 117
-1
votes
1 answer

T-SQL prepare dynamic COALESCE

As attached in screenshot, there are two tables. Configuration: Detail Using Configuration and Detail table I would like to populate IdentificationType and IDerivedIdentification column in the Detail table. Following logic should be used,…
Digant
  • 3
  • 3
-1
votes
2 answers

SQL Server 2005 how do I pivot or cross tab data to get my resultset?

I am looking to generate a stored proc that will take a CSV string of projectIds and return the following result set. sample data the projectId and name fields come from the project table while the rest of the fields are a pivot of the milestone…
kacalapy
  • 9,806
  • 20
  • 74
  • 119
-1
votes
3 answers

isnull not working SQL Server

I have the following problem , I'm doing a select with multiple where. example: select isnull(fechamov,0) from movimientos where (provedor='1000' and depto='20') or (provedor='1001' and depto='200') result: |fecha de…