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

MYSQL left join missing out rows

I have 4 tables. 1st table has: date and revenue 2nd table has: date and cost 3rd table has: date and fees 4th table has: date and others I am calculating final revenue values using the formula: final = revenue - (cost + fees + others) To perform…
coderWorld
  • 602
  • 1
  • 8
  • 28
-2
votes
1 answer

Is this how COALESCE works in this MS SQL Query: ORDER BY COALESCE([UpdatedOn], [CreatedOn]) DESC

I'm reviewing some SQL statement which uses a COALESCE statement in an ORDER BY. I've read the docs about COALESCE so i'm trying to understand if I understand how it's getting used, in this scenario. This is the part of the sql statement: ORDER BY…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
-2
votes
1 answer

Imagemagick c api : How to read png image frmaes from gif file , what calls to use

So I created gif file from 5 png files with using MagickCoalesceImages call and store it on disk. How I can read these files back from gif file ? MagickReadImage does not help
Narek yan
  • 37
  • 6
-2
votes
2 answers

MySQL replacing column with another one when null

I'm trying to retrieve data from two joined tables in MySQL. table1 table2 id id - foreign key name address_old ```other col``` address_new I would like to get 'name'…
Yoojin Kim
  • 37
  • 4
-2
votes
1 answer

join table with empty results

i have two tables: users and tasks table users: +----------+---------------------+ | id | usrname | +----------+---------------------+ | 1 | user1 | +----------+---------------------+ | 2 | user2 …
Julien
  • 11
  • 3
-2
votes
1 answer

Using coalesce(1) is taking too much time time for writing dataset to s3

I'm using coalesce(1) for writing the set of records in s3 bucket in csv process. which is taking too much time for 505 records. dataset.coalesce(1).write().csv("s3a://bucketname/path"); And I want to mention that before this writing process, I'm…
-2
votes
1 answer

SQL: How to make a Coalesce on each VARCHAR column after a group by?

Using the table below: id: integer| col_1: VARCHAR| col_2: VARCHAR | col_3: VARCHAR ------------------------------------------------------------ 1 | 'a' | 'b' | null 2 | null | 'b' | …
boriskuete
  • 65
  • 1
  • 6
-2
votes
1 answer

Entity Framework: Select null or value

I'm searching to write with Entity Framework a select query which columns can be null or contain a value. SQL Example: select * from table where column_1 is null or colummn_1 = 2 and column_2 is null or colummn_2 = 89 and column_3 is null or…
staff88
  • 43
  • 1
  • 1
  • 5
-2
votes
1 answer

SQL : Coalesce within where clause giving error Oracle (Not in DB2)

I am trying to use COALESCE in WHERE clause, and I am getting the following error: ORA-00907: missing right parenthesis Failed SQL stmt: If I remove the COALESCE, I don't get the error anymore. I am not sure why would it give me this error as the…
-3
votes
1 answer

MySQL IFNULL syntax on outer query to return a record when query does not return any records

I'm looking for guidance on how to apply IFNULL to an outer query in my mySQL Query. The query checks for new timecards submitted the previous day and kicks off a procedure that writes the results to a file server. Frequently there are no records…
-3
votes
1 answer

Postgresql : get notnull column value alone in select statement

I have a table as below Id String Numeric Date 1 null 1006 null 2 Testing value null null 3 null null 1-1-2023 Id, String, Numeric, Date are the column names. Id is the primary key. Id is going to have only one value (either string…
user20824479
-3
votes
1 answer

Understanding TSQL Coalesce function

I am trying to select all 12 months / year. And I thought following TSQL code would do this. However, this does not include all months like I want. What is the cause of this? This is modified code: DECLARE @END_YEAR VARCHAR(10) DECLARE…
user371819
  • 95
  • 1
  • 4
  • 18
-3
votes
1 answer

combine/merge two columns into 1 ( t-sql )

I have 2 tables: batters and pitchers I want to pull playerid(nvarchar50), firstname(nvarchar50), lastname(nvarchar50), bats(nvarchar50) from the batters table and playerid(nvarchar50), firstname(nvarchar50), lastname(nvarchar50),…
-3
votes
2 answers

How to COUNT(*) WHEN COALESCE - SQL Server

I have this T-SQL statement that adds up all of col1 and col2 but also needs to return the number of rows returned by the WHERE The only way I figured out how to do it is count(FlightLogLegID - (FlightLogLegID - 1)) which results in summing 1 per…
masteroleary
  • 1,014
  • 2
  • 16
  • 33
-4
votes
1 answer

Query account running balance with php

Please assist, how to write php code for the following query: SELECT transactions_id, trn_date, trn_dt_total, trn_ct_total, trn_description, trn_bank_reference, balance FROM (SELECT t.*, @n := IF(@g <> transactions_id, 0, @n) +…
1 2 3
62
63