I have a table: customer and has three columns. I want to select the first not null column.
Select ID coalesce( columnA, ColumnB)
from customer.
The Expect result should be : 101 AABB.
The actual result is 101 blank.
The columnA may have space, I trim the column.
Select ID coalesce( trim(columnA), ColumnB) from customer.
I get the same result: 101 blank.
How can I get result: 101 AABB?
The ColumnA may have space, null value, and actual value
Use CONCAT. NULL is not same as space.COALESCE will return the first non null value and hence you are getting space.
select ID,CONCAT(trim(ColumnA),trim(ColumnB)) from customer
EDIT
select ID,
CASE WHEN ColumnA is null OR trim(ColumnA)=' ' then ColumnB else ColumnA END
from customer
Related
For example, if column1 has a bunch of descriptions and multiple NULLs, and I want to replace each NULL with a unique description.
using a COALESCE function I can do
COALESCE(Column1,'Description')
and this will replace every NULL in the column with "description", how can I adress each NULL individually and not replace all of them with the same value?
You have to decide first you are replacing the nulls based on what criteria.Then you can use CASE to decide individual description for each criteria or condition.
Here below i have taken the criteria of Row number
WITH data
AS (SELECT NULL id
FROM dual
UNION ALL
SELECT NULL
FROM dual),
d1
AS (SELECT ROWNUM rw,
d.*
FROM data d)
SELECT CASE
WHEN rw = 1 THEN COALESCE(id, 'star')
ELSE COALESCE(id, 'moon')
END AS id
FROM d1;
I am using BigQuery at work and I am doing some data validation. As part of this I am trying to find all columns in a given table that consist entirely of null values. I know that I can query each column individually using something like
SELECT count(id), <column_name> FROM <dataset>.<table>
WHERE <column_name> IS NOT NULL
GROUP EACH BY 2 HAVING count(id) = 0
I would rather not have to do this for every column in the table as there are a large number of them.
This can be done without running a query per column with something like the following. Take a SUM of the non-null values in a SELECT, and look for the columns that contain zero non-nulls.
SELECT
SUM(column_1 IS NOT NULL),
SUM(column_2 IS NOT NULL),
SUM(column_3 IS NOT NULL)
FROM
(SELECT NULL AS column_1, NULL AS column_2, 17 AS column_3),
(SELECT 18 AS column_1, NULL AS column_2, 19 AS column_3)
In this case, the results are:
| column_1 | column_2 | column_3 |
|----------|----------|----------|
| 1 | 0 | 2 |
which tells us that column_2 contains only nulls.
SELECT
column_name, COUNT(1) AS nulls_count
FROM
`projectID.Dataset.Table`,
UNNEST(REGEXP_EXTRACT_ALL(TO_JSON_STRING(`projectID.Dataset.Table`), r'"(\w+)":null')) column_name
GROUP BY
column_name
ORDER BY
nulls_count DESC
The above bigquery gives the number of records having NULL Value and group the columns. Also note that query will not give non null columns.
To add to the above answer,
the above answer works only for integer columns, if there are columns of other type you can do the following
SELECT
SUM(IF(column1 IS NULL, 0, 1)),
SUM(IF(column2 IS NULL, 0, 1))
FROM
table
In this case irrespective of the data type, all null values are considered as 0 and other values are considered as 1. If the result is 0, then the column contains all values as null.
I want to get the Name from this table but I only have the Number which is not unique. How do I get the correct Name (please give me some SQL syntax)?
Below the database table you can see the input and expected output for
Database Table
Number Name
1 Anna
1 Anna
2 Brad
2 NULL
2 NULL
2 NULL
3 NULL
3 NULL
4 Adam
5 NULL
Input and expected output:
Number (Input) Name (Expected outpu)
1 Anna
2 Brad
3 NULL
4 Adam
5 NULL
What do I need to add to my query to make it work?
SELECT Name FROM tablename
WHERE Number='chosen number'
A simple MAX/GROUP BY will return your expected result set:
SELECT Number, MAX(Name)
FROM table name
GROUP BY 1;
Btw, NUMBER is a Reserved Name in Teradata
SELECT
number,
MAX(name)
FROM
your_table
GROUP BY
number
And/Or
SELECT
MAX(name)
FROM
your_table
WHERE
number = x
MAX, MIN, etc, all treat NULL as the last value; you only get a NULL if all values are NULL.
This is a step in the right direction, the problem is that you will still get NULL aswell if you have a row with NULL give me a bit to figure out how to filter those out if there is a name
Mysql would be this:
http://sqlfiddle.com/#!9/4beca/1
SELECT DISTINCT Name
FROM mytable
WHERE Number=1 AND Name is not null
UNION
SELECT NULL
FROM mytable
LIMIT 1
SQL-Server would be this:
http://sqlfiddle.com/#!3/f4078/11
SELECT TOP 1 sub.Name
FROM
(SELECT DISTINCT Name
FROM mytable
WHERE Number=1 AND Name is not null
UNION
SELECT NULL AS Name
FROM mytable) AS sub
Name varchar, Value int, Active bit
-----------------------------------
'Name1',1,1
'Name2',2,1
'Name1',3,0
'Name2',4,0
'Name3',1,1
'Name4',1,1
I want to return where Active is anything but prioritize when it's 0 so I want to return this:
'Name1',3
'Name2',4
'Name3',1
'Name4',1
I tried this, but get an error to include Active in my return statement
Select Distinct Name, Value From Table Order by Active
So I tried this:
Select Distinct Name, Value, Active From Table Order by Active
But now it returns all the rows. I would like to prioritize where Active = 0 in the distinct results but since it requires I put Active in the return statement makes this complicated.
Can someone help?
Your question is a little confusing, but if I'm understanding it correctly, you need to use a group by statement:
select name,
max(case when active = 0 then value end) value
from yourtable
group by name
SQL Fiddle Demo
With your edits, you can use coalesce and still get it to work:
select name, coalesce(max(case when active = 0 then value end), max(value)) value
from yourtable
group by name
More Fiddle
You can order by fields not contained in the select clause
Select Name, Value
From Table
ORDER BY Active, Name, Value
But you cannot use SELECT DISTINCT at the same time.
If you use "select distinct" there is the possibility that some rows will be discarded, when this happens there is no longer any viable relationship retained between [Active] and the "distinct" rows. So if using select distinct, and you need to order by [Active], then [Active] MUST be in the select clause.
I couldn't delete the post b/c of the other answers, but here is answer I was looking for in case anyone else was wondering.
SELECT Distinct Name,Value FROM Table WHERE Active = 0
UNION ALL
SELECT Distinct Name,Value FROM Table a WHERE Active = 1 AND NOT EXISTS (
SELECT TOP 1 1 FROM Table a2 WHERE a2.Active = 0 AND a2.Name = a.Name
)
Review #Sgeddes 's answer for a better solution.
Thanks to everyone for their help.
Perhaps this:
create table #t(
Active int not null,
Name varchar(10) not null,
Value int not null,
primary key clustered (Active desc,Name,Value)
);
insert #t(Active,Name,Value)
select Active,Name,Value from [Table];
select Name, Value
from #t;
go
yields as desired:
Name Value
---------- -----------
Name1 1
Name2 2
Name3 1
Name4 1
Name1 3
Name2 4
I have a quite unique need to make select always return one row
My SQL:
select * from table1 Where (table1.pk = :p1) or (table1.fk1 = :p1)
The above SQL always has two cases for return:
1- my Select return two records:
The only different is one of the records has data while the other has only the ID filled with data while the rest of its fields are null. I need in this case to return only the one that has data in other fields.
2- my Select return one record
In this case the record returned has only the ID field filled with data while the rest of the fields are null however this is what I want and no need for any further processing.
Please advise if is it possible to do that in one plain Select SQL. I can not use stored procedure.
You can use the first clause of the select statement to get only 1 row.
Given your specific conditions, you can order the result set descending by the rest of the fields to be sure the null row is selected only in case there's no data row (null goes first in firebird 2.5, but AFAIK this changed somewhere in the last versions, so check your specific version before applying this).
Your final query will look like this:
select first 1 *
from table1
where (table1.pk = :p1)
or (table1.fk1 = :p1)
order by somecolumn;
somecolumn being the most relevant of the other fields that can contain null values.
you can test this with this statements:
--two rows, one with ID and values and the other with ID and null
with q1 as (
select 1 id, 'data' othercolumn
from rdb$database
union
select 2 id, null othercolumn
from rdb$database
)
select first 1 *
from q1
order by othercolumn nulls last;
--versus:
--onw row with ID and null
with q1 as (
select 2 id, null othercolumn
from rdb$database
)
select first 1 *
from q1
order by othercolumn nulls last;