T-SQL Union with Dissimilar Columns - sql

I have two tables with the following data:
[Animals].[Males]
DataID HerdNumber HerdID NaabCode
e46fff54-a784-46ed-9a7f-4c81e649e6a0 4 'GOLDA' '7JE1067'
fee3e66b-7248-44dd-8670-791a6daa5d49 1 '35' NULL
[Animals].[Females]
DataID HerdNumber HerdID BangsNumber
987110c6-c938-43a7-a5db-194ce2162a20 1 '9' 'NB3829483909488'
1fc83693-9b8a-4054-9d79-fbd66ee99091 2 'NATTIE' 'ID2314843985499'
I want to merge these tables into a view that looks like this:
DataID HerdNumber HerdID NaabCode BangsNumber
e46fff54-a784-46ed-9a7f-4c81e649e6a0 4 'GOLDA' '7JE1067' NULL
fee3e66b-7248-44dd-8670-791a6daa5d49 1 '35' NULL NULL
987110c6-c938-43a7-a5db-194ce2162a20 1 '9' NULL 'NB3829483909488'
1fc83693-9b8a-4054-9d79-fbd66ee99091 2 'NATTIE' NULL 'ID2314843985499'`
When I used the UNION keyword, SQL Server produced a view that merged the NaabCode and BangsNumber into one column. A book that I have on regular SQL suggested UNION CORRESPONDING syntax like so:
SELECT *
FROM [Animals].[Males]
UNION CORRESPONDING (DataID, HerdNumber, HerdID)
SELECT *
FROM [Animals].[Females]`
But when I type this SQL Server says "Incorrect syntax near 'CORRESPONDING'."
Can anyone tell me how to achieve my desired result and/or how to use UNION CORRESPONDING in T-SQL?

You can just do:
SELECT DataID, HerdNumber, HerdID, NaabCode, NULL as BangsNumber
FROM [Animals].[Males]
UNION ALL
SELECT DataID, HerdNumber, HerdID, NULL as NaabCode, BangsNumber
FROM [Animals].[Females]
SQL Fiddle
I don't remember that SQL Server supports the corresponding syntax, but I might be wrong.
Anyway, this query will select null for the BangsNumber column for the males, and for the NaabCode column for the females, while selecting everything else correctly.

Just do the union explicitly listing the columns:
select DataID, HerdNumber, HerdID, NaabCode, NULL as BangsNumber
from Animals.Males
union all
select DataID, HerdNumber, HerdID, NULL, BangsNumber
from Animals.Females;
Note: you should use union all instead of union (assuming that no single animal is both male and female). union incurs a performance overhead to remove duplicates.

SELECT DataID, HerdNumber, HerdID, NaabCode, '' asBangsNumber
FROM [Animals].[Males]
UNION ALL
SELECT DataID, HerdNumber, HerdID, '' as NaabCode, BangsNumber
FROM [Animals].[Females]

You need to state the columns in each select
SELECT DataID, HerdNumber, HerdID
FROM [Animals].[Males]
UNION
SELECT DataID, HerdNumber, HerdID
FROM [Animals].[Females]

Related

Test struct for at least one non null value

Given the data set:
field_a
description_1
metric_1
ball
large
20
ball
small
4
cat
null
null
I want to pack fields description_1 and metric_1 into a struct and then zip them into an array by field_a:
WITH
DATA AS (
SELECT
'ball' AS field_a,
'large' AS description_1,
20 AS metric_1
UNION ALL
SELECT
'ball',
'small',
4
UNION ALL
SELECT
'cat',
NULL,
NULL )
SELECT
field_a,
ARRAY_AGG(STRUCT(description_1,
metric_1))
FROM
DATA
GROUP BY
1;
That gives me:
However, if all fields in the struct are null I would like to see an empty array instead of an array with size of 1 and an struct inside of it.
Desired output:
I figured the following query to test the fields before packing them into the struct:
WITH
DATA AS (
SELECT
'ball' AS field_a,
'large' AS description_1,
20 AS metric_1
UNION ALL
SELECT
'ball' AS field_a,
'small',
4
UNION ALL
SELECT
'cat',
NULL,
NULL )
SELECT
field_a,
ARRAY_AGG(
IF
(description_1 IS NOT NULL
OR metric_1 IS NOT NULL,
STRUCT(description_1,
metric_1),
NULL)IGNORE NULLS)
FROM
DATA
GROUP BY
1;
However it feels like a tedious solution, is there a better way to test if struct contains at least one non-null value? or any other solution to achieve the desired output more elegantly?
Below approach might be slightly cleaner one
select * from (select distinct field_a from your_table)
left join (
select field_a, array_agg(struct(description_1, metric_1))
from your_table
where not description_1 is null and not metric_1 is null
group by field_a
)
using(field_a)
if applied to sample data in your question - output is
Note: there are more ways to write that where clause and depends on your tastes
for example
where format('%t', (description_1, metric_1)) != '(NULL, NULL)'
or
where to_json_string((description_1, metric_1)) != '{"":null,"":null}'
But, I feel, using explicit not ... is ... is more natural and explicit so easier to swallow if you have just two or three columns to check.
When you have more - like 5 or 10, etc. last two version can produce more compact code ...

Is there a concept which is the 'opposite' of SQL NULL?

Is there a concept (with an implementation - in Oracle SQL for starters) which behaves like a 'universal' matcher ?
What I mean is; I know NULL is not equal to anything - including NULL.
Which is why you have to be careful to 'IS NULL' rather than '=NULL' in SQL expressions.
I also know it is useful to use the NVL (in Oracle) function to detect a NULL and replace it with something in the output.
However: what you replace the NULL with using NVL has to match the datatype of the underlying column; otherwise you'll (rightly) get an error.
An example:
I have a table with a NULLABLE column 'name' of type VARCHAR2; and this contains a NULL row.
I can fetch out the NULL and replace it with an NVL like this:
SELECT NVL(name, 'NullyMcNullFace’) from my_table;
Great.
But if the column happens to a NUMBER (say 'age'), then I have to change my NVL:
SELECT NVL(age, 32) from my_table;
Also great.
Now if the column happens to be a DATE (say 'somedate'), then I have to change my NVL again:
SELECT NVL(somedate, sysdate) from my_table;
What I'm getting at here : is that in order to deal with NULLs you have to replace with a specific something ; and that specific something has to 'fit' the data-type.
So is there a construct/concept of (for want of a better word) like 'ANY' here.
Where 'ANY' would fit into a column of any datatype (like NULL), but (unlike NULL and unlike all other specific values) would match ANYTHING (including NULL - ? probably urghhh dunno).
So that I could do:
SELECT NVL(whatever_column, ANY) from my_table;
I think the answer is probably no; and probably 'go away, NULLs are bad enough - never mind this monster you have half-thought of'.
No, there's no "universal acceptor" value in SQL that is equal to everything.
What you can do is raise the NVL into your comparison. Like if you're trying to do a JOIN:
SELECT ...
FROM my_table AS m
JOIN other_table AS o ON o.name = NVL(m.name, o.name)
So if m.name is NULL, then the join will compare o.name to o.name, which is of course always true.
For other uses of NULL, you might have to use another technique that suits the situation.
Adressing the question in the comment on Bill Karwin's answer:
I want to output a 1 if the NEW and OLD value differ and a 0 if they are the same. But (for my purposes) I want to also return 0 for two NULLS.
select
Case When (:New = :Old) or
(:New is NULL and :Old is NULL) then 0
Else
1
End
from dual
In a WHERE CLAUSE you can put a condition like this,
WHERE column1 LIKE NVL(any_column_or_param, '%')
Perhaps DECODE() would suit your purpose here?
WITH t1 AS (SELECT 1 ID, NULL val FROM dual UNION ALL
SELECT 2 ID, NULL val FROM dual UNION ALL
SELECT 3 ID, 1 val FROM dual UNION ALL
SELECT 4 ID, 2 val FROM dual UNION ALL
SELECT 5 ID, 5 val FROM dual),
t2 AS (SELECT 1 ID, NULL val FROM dual UNION ALL
SELECT 2 ID, 3 val FROM dual UNION ALL
SELECT 3 ID, 1 val FROM dual UNION ALL
SELECT 4 ID, 4 val FROM dual UNION ALL
SELECT 6 ID, 5 val FROM dual)
SELECT t1.id t1_id,
t1.val t1_val,
t2.id t2_id,
t2.val t2_val,
DECODE(t1.val, t2.val, 0, 1) different_vals
FROM t1
FULL OUTER JOIN t2 ON t1.id = t2.id
ORDER BY COALESCE(t1.id, t2.id);
T1_ID T1_VAL T2_ID T2_VAL DIFFERENT_VALS
---------- ---------- ---------- ---------- --------------
1 1 0
2 2 3 1
3 1 3 1 0
4 2 4 4 1
5 5 1
6 5 1

Oracle SQL Min in Select Clause

Can some one please help me in writing a sql query that should do a oracle min function based on the following conditions.
For eg for column values
0,0,0,0 then output should be 0
0,null,0,null then output should be o
0,2,4,5,6 then output should be 2 (Note that we are excluding Zero here)
0,2,null,4,5 then output should be 2 (same here we are excluding zero)
null,null,null, null then output should be null.
I wrote query already that satisfies all the above cases but failing for last case when all the column values are null. Instead of returning null it is returning 0. Can some one modify the below query to fit for the last case as well?
select NVL(MIN(NULLIF(columnname,0)),0) from tablename;
Please also keep in mind that the query should be runnable in oracle as well as hsqldb as we are using hsql db for running junits.
If all 4 cases satisfied by your query then just a case will solve your problem.
SELECT CASE WHEN MIN(COLUMNNAME) IS NULL THEN NULL ELSE NVL(MIN(NULLIF(COLUMNNAME,0)),0) END FROM TABLENAME;
Note:- assuming all the cases satisfied by your query except 5th.
I will show below an input table with two columns, ID and VAL, to illustrate the various possibilities. You want a single result per ID (or even for the entire table), so this must be a job for GROUP BY and some aggregate function. You want to distinguish between three types of values: Greater than zero, zero, and null (in this order); you want to pick the "highest priority group" that exists for each ID (in this order of priority), and for that priority group only, you want to pick the min value. This is exactly what the aggregate FIRST/LAST function does. To order by the three "classes" of values, we use a CASE expression in the ORDER BY clause of the aggregate LAST function.
The WITH clause below is not part of the solution - I only include it to create test data (in your real life situation, use your actual table and column names and remove the entire WITH clause).
with
inputs ( id, val ) as (
select 1, 0 from dual union all
select 1, 0 from dual union all
select 1, 0 from dual union all
select 2, 0 from dual union all
select 2, null from dual union all
select 2, 0 from dual union all
select 3, 0 from dual union all
select 3, 2 from dual union all
select 3, 5 from dual union all
select 4, 0 from dual union all
select 4, 3 from dual union all
select 4, null from dual union all
select 5, null from dual union all
select 5, null from dual
)
select id,
min(val) keep (dense_rank last order by case when val > 0 then 2
when val = 0 then 1
else 0
end
) as min_val
from inputs
group by id
order by id
;
ID MIN_VAL
---------- ----------
1 0
2 0
3 2
4 3
5

Keep order from 'IN' clause

Is it possible to keep order from a 'IN' conditional clause?
I found this question on SO but in his example the OP have already a sorted 'IN' clause.
My case is different, 'IN' clause is in random order
Something like this :
SELECT SomeField,OtherField
FROM TestResult
WHERE TestResult.SomeField IN (45,2,445,12,789)
I would like to retrieve results in (45,2,445,12,789) order. I'm using an Oracle database. Maybe there is an attribute in SQL I can use with the conditional clause to specify to keep order of the clause.
There will be no reliable ordering unless you use an ORDER BY clause ..
SELECT SomeField,OtherField
FROM TestResult
WHERE TestResult.SomeField IN (45,2,445,12,789)
order by case TestResult.SomeField
when 45 then 1
when 2 then 2
when 445 then 3
...
end
You could split the query into 5 queries union all'd together though ...
SELECT SomeField,OtherField
FROM TestResult
WHERE TestResult.SomeField = 4
union all
SELECT SomeField,OtherField
FROM TestResult
WHERE TestResult.SomeField = 2
union all
...
I'd trust the former method more, and it would probably perform much better.
Decode function comes handy in this case instead of case expressions:
SELECT SomeField,OtherField
FROM TestResult
WHERE TestResult.SomeField IN (45,2,445,12,789)
ORDER BY DECODE(SomeField, 45,1, 2,2, 445,3, 12,4, 789,5)
Note that value,position pairs (e.g. 445,3) are kept together for readability reasons.
Try this:
SELECT T.SomeField,T.OtherField
FROM TestResult T
JOIN
(
SELECT 1 as Id, 45 as Val FROM dual UNION ALL
SELECT 2, 2 FROM dual UNION ALL
SELECT 3, 445 FROM dual UNION ALL
SELECT 4, 12 FROM dual UNION ALL
SELECT 5, 789 FROM dual
) I
ON T.SomeField = I.Val
ORDER BY I.Id
There is an alternative that uses string functions:
with const as (select ',45,2,445,12,789,' as vals)
select tr.*
from TestResult tr cross join const
where instr(const.vals, ','||cast(tr.somefield as varchar(255))||',') > 0
order by instr(const.vals, ','||cast(tr.somefield as varchar(255))||',')
I offer this because you might find it easier to maintain a string of values rather than an intermediate table.
I was able to do this in my application using (using SQL Server 2016)
select ItemID, iName
from Items
where ItemID in (13,11,12,1)
order by CHARINDEX(' ' + Convert("varchar",ItemID) + ' ',' 13 , 11 , 12 , 1 ')
I used a code-side regex to replace \b (word boundary) with a space. Something like...
var mylist = "13,11,12,1";
var spacedlist = replace(mylist,/\b/," ");
Importantly, because I can in my scenario, I cache the result until the next time the related items are updated, so that the query is only run at item creation/modification, rather than with each item viewing, helping to minimize any performance hit.
Pass the values in via a collection (SYS.ODCINUMBERLIST is an example of a built-in collection) and then order the rows by the collection's order:
SELECT t.SomeField,
t.OtherField
FROM TestResult t
INNER JOIN (
SELECT ROWNUM AS rn,
COLUMN_VALUE AS value
FROM TABLE(SYS.ODCINUMBERLIST(45,2,445,12,789))
) i
ON t.somefield = i.value
ORDER BY rn
Then, for the sample data:
CREATE TABLE TestResult ( somefield, otherfield ) AS
SELECT 2, 'A' FROM DUAL UNION ALL
SELECT 5, 'B' FROM DUAL UNION ALL
SELECT 12, 'C' FROM DUAL UNION ALL
SELECT 37, 'D' FROM DUAL UNION ALL
SELECT 45, 'E' FROM DUAL UNION ALL
SELECT 100, 'F' FROM DUAL UNION ALL
SELECT 445, 'G' FROM DUAL UNION ALL
SELECT 789, 'H' FROM DUAL UNION ALL
SELECT 999, 'I' FROM DUAL;
The output is:
SOMEFIELD
OTHERFIELD
45
E
2
A
445
G
12
C
789
H
fiddle

Is there a way to return each value as a row in SQL

Hi we know that is we do the following query in Oracle SQL we get each value as a column and will return the value as value in each column.
select 'Draft','Submitted','Cancelled','Accepted','Accepted and Modified','Open','Pending','Seller Reject' from dual;
Is there a way to return each value as a new row under single column?
Thanks in advance!
I tried this and it works:
select 'Draft' as Enum
union
select 'Submitted' as Enum
union
select 'Cancelled' as Enum
union
select 'Accepted' as Enum
union
select 'Accepted and Modified' as Enum
union
select 'Open' as Enum
union
select 'Pending' as Enum
union
select 'Seller Reject' as Enum
This will work on SQL Server.
But for oracle, please add the from claus in each select query like select 'Seller Reject' as Enum from Dual
select * from table (
sys.dbms_debug_vc2coll('Draft','Submitted','Cancelled','Accepted','Accepted and Modified','Open','Pending','Seller Reject')
);