Here's what i wanna do
Select X, Y, if Z IS NULL THEN ( select something ) else Z
Basically I want to select the 'Z' if it's null I want to select another value, can someone please suggest a code example with a case or something that I can follow through it ?
Select X, Y, NVL(Z, showThis) as Z
will return showThis if Z is null in ORACLE
Select X, Y, ISNULL(Z, showThis) as Z
will return showThis if Z is null in SQL-SERVER
Do you want coalesce()?
Select X, Y,
coalesce(z, <something else>) as z
Choose with CASE:
select
X, Y,
case
when Z is null then (select something)
else (select something else)
end as col
from tablename
Related
A question for my general understanding:
If I have:
SELECT X, Y, Z FROM MyTable
and I want only entries where none of the values are null I have to use
SELECT X, Y, Z FROM MyTable
WHERE X IS NOT NULL AND Y IS NOT NULL AND Z IS NOT NULL
Is there a shorter option? Something like
SELECT X, Y, Z FROM MyTable
WHERE X, Y, Z IS NOT NULL
This is just a short example, but I guess in queries with many conditions and requests something like this could make the query string much shorter and more readable.
There is no such shorter form than below query.
SELECT X, Y, Z
FROM MyTable
WHERE X IS NOT NULL AND Y IS NOT NULL AND Z IS NOT NULL
I have something like the following scenario
Select x, y, z
from mytable
where x = 1
UNION
Select x, y, z
from mytable2
where x = 1
I would like to put the results into a #TempTable and then create a Pivot of the results into #TempTable2
I have tried
SELECT * INTO #TempTable FROM (
Select x, y, z
from mytable
where x = 1
UNION
Select x, y, z
from mytable2
where x = 1
)
But it get Incorrect syntax near ')'
I've forgotten all the other variations I have made but none of them have worked
Add an alias to the derived table. Here I use X because I'm imaginative
SELECT *
INTO #TempTable
FROM
(
Select x, y, z
from mytable
where x = 1
UNION
Select x, y, z
from mytable2
where x = 1
) AS X
SQL Server needs a reference for the objects in the FROM clause. No alias = no reference
You can see this if we rewrite the query using a CTE
WITH myUnion AS
(
Select x, y, z
from mytable
where x = 1
UNION
Select x, y, z
from mytable2
where x = 1
)
SELECT *
INTO #TempTable
FROM myUnion
I Want to Select where the results are in a specific column of a subquery, how do I specify the column the IN should check from?
Select x from foo
where x in (Select y, Max(z) as MaxEntry
From bar
Group By y)
say I'm selecting from this:
Y Z
1 2
1 4
2 7
2 8
I want to see if x is in the 4 or 8 set of the Data
If you only check x column then you can write like this.
Select x from foo
where x in (Select x from bar where blah)
but if you want to check x and y then you can write like this.
Select x from foo
where exists(Select * from bar where bar.x = foo.x and bar.y = foo.y )
for your query, you don't need to write y at the select
Select x from foo
where x in (Select Max(z) as MaxEntry
From bar
Group By y)
I think you want:
SELECT x
FROM foo
WHERE EXISTS
(SELECT 1
FROM bar
GROUP BY y
HAVING MAX(bar.z) = foo.x)
If I understood your question correctly, then this should work:
Select x from foo
join (Select y, Max(z) as MaxEntry
From bar
Group By y)
as m on m.MaxEntry = x
This is possible:
SELECT 'Bla' AS X
why is this not possible in TeraData:
SELECT 'Bla' AS X
UNION
SELECT 'DiBla' AS X
Is there a way to achieve the above without having to create a temp/volatile/working table in TeraData?
PS:
The error is: A select for a union, intersect or minus must reference a table
If you want two columns on one row, then use:
SELECT 'Bla' AS X, 'DiBla' AS Y
If you want:
X
Bla
DiBla
Then you just do:
select 'Bla' as X
union all
select 'DiBlah' as X;
If you want:
X Y
Bla NULL
NULL DiBla
Then:
SELECT 'Bla' as X, NULL as Y
UNION ALL
SELECT NULL as X, 'DiBla' as Y
You have a attribute name mishmash in your UNION. You can not perform UNION between relations having different structure. Therefore, use
SELECT cast('Bla' as varchar(6)) AS X FROM (SELECT 1 a) t
UNION
SELECT cast('DiBla' as varchar(6)) AS X FROM (SELECT 1 a) t
the explicit casting make sure that the data types are equivalent as well as the attribute names. Another solution could be
SELECT * FROM
(
SELECT cast('Bla' as varchar(6)) AS X
) t
UNION
SELECT * FROM
(
SELECT cast('DiBla' as varchar(6)) AS X
) t
I have a table with all string columns, but I know certain columns are numbers (or dates). Is there a built in function in BigQuery to infer the data type of individual columns? Something like select is_string(column_name) from table_name?
One idea that comes to mind is using SAFE_CAST in combination with LOGICAL_AND, e.g.:
#standardSQL
WITH T AS (
SELECT '2017-05-01' AS x, '3.14' AS y, '5' AS z UNION ALL
SELECT '2017-03-02' AS x, '1.59' AS y, '-1' AS z UNION ALL
SELECT NULL AS x, NULL AS y, NULL AS z
)
SELECT
LOGICAL_AND(x IS NULL OR SAFE_CAST(x AS DATE) IS NOT NULL) AS x_is_date,
LOGICAL_AND(y IS NULL OR SAFE_CAST(y AS FLOAT64) IS NOT NULL) AS y_is_float64,
LOGICAL_AND(z IS NULL OR SAFE_CAST(z AS TIMESTAMP) IS NOT NULL) AS z_is_timestamp
FROM T;
This results in true, true, and false (the z values are not timestamps). If you want to reuse the same expression multiple times, you can make this a little less verbose with a SQL UDF:
#standardSQL
CREATE TEMP FUNCTION IsDate(x STRING) AS (
x IS NULL OR SAFE_CAST(x AS DATE) IS NOT NULL
);
WITH T AS (
SELECT '2017-05-01' AS x, '3.14' AS y, '5' AS z UNION ALL
SELECT '2017-03-02' AS x, '1.59' AS y, '-1' AS z UNION ALL
SELECT NULL AS x, NULL AS y, NULL AS z
)
SELECT
LOGICAL_AND(IsDate(x)) AS x_is_date,
LOGICAL_AND(IsDate(y)) AS y_is_date,
LOGICAL_AND(IsDate(z)) AS z_is_date
FROM T;
This results in true, false, false, since only x has values in date format.