Select into #TempTable - sql-server-2017

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

Related

Converting error when try to UNION ALL temporary tables in SQL

I created a few temporary tables which I need to union using UNION ALL. This is how it looks in my code:
SELECT X, Y, Z
INTO #Tab1
FROM dbo.Cust
SELECT X, Y, Z
INTO #Tab2
FROM dbo.Cars
SELECT X, Y, Z
INTO #Tab3
FROM dbo.Colors
SELECT * FROM #Tab1
UNION ALL
SELECT * FROM #Tab2
UNION ALL
SELECT * FROM #Tab3
Unfortunately, I got an error:
Conversion failed when converting the nvarchar value .. to data type
int
However, when I try to union these tables but not as temporary ones, then no error arises. So, the solution below works fine and output is returned properly:
SELECT X, Y, Z
FROM dbo.Cust
UNION ALL
SELECT X, Y, Z
FROM dbo.Cars
UNION ALL
SELECT X, Y, Z
FROM dbo.Colors
Columns X, Y, Z are of the same type as in temp table. I guess there must be an issue with temp tables but don't know what exactly. Where is the problem and why it happens?

Get RowsWise data with Columns Name ColumnWise

I have a Table Structure like:
ApplicationId IsFO20Submitted IsFO08Submitted IsFO07Submitted IsFO09Submitted IsFO10Submitted
CBA202000001 Y Y Y Y Y
CBA202000002 Y Y Y Y Y
CBA202000007 Y Y Y Y Y
I want my Result to be like:
ApplicationId CBA202000001 CBA202000002 CBA202000007
IsFO20Submitted Y Y Y
IsFO08Submitted Y Y Y
IsFO07Submitted Y Y Y
IsFO09Submitted Y Y Y
IsFO10Submitted Y Y Y
Is there anything i can try in SQL to get such result
You can use APPLY :
SELECT TT.ApplicationId,
MAX(CASE WHEN t.ApplicationId = 'CBA202000001' then ApplicationVal END) AS [CBA202000001],
MAX(CASE WHEN t.ApplicationId = 'CBA202000002' then ApplicationVal END) AS [CBA202000002],
MAX(CASE WHEN t.ApplicationId = 'CBA202000007' then ApplicationVal END) AS [CBA202000002]
FROM table t CROSS APPLY
( VALUES ('IsFO20Submitted', IsFO20Submitted),
('IsFO08Submitted',IsFO08Submitted),
. . .
('IsFO10Submitted',IsFO10Submitted)
) TT(ApplicationId, ApplicationVal)
GROUP BY TT.ApplicationId;
Check pivot - unpivot function. You'll have to do the unpivot first and then pivot the result. Didn't test it though so you'll have to try it first...
Pivot example:
SELECT * FROM
(
SELECT column1, column2
FROM tables
WHERE conditions
)
PIVOT
(
aggregate_function(column2)
FOR column2
IN ( expr1, expr2, ... expr_n) | subquery
)
ORDER BY expression [ ASC | DESC ];
Unpivot example:
SELECT *
FROM unpivot_test
UNPIVOT (quantity FOR product_code IN (product_code_a AS 'A', product_code_b AS 'B', product_code_c AS 'C', product_code_d AS 'D'));

Condition statement in a select

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

Select where x in multi column subquery

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

Issue with recursive CTE in PostgreSQL

This query generates the numbers from 1 to 4.
with recursive z(q) as (
select 1
union all
select q + 1 from z where q < 4
)
select * from z;
But, if I modify it to this,
with x as (
select 1 y
),
recursive z(q) as (
select y from x
union all
select q + 1 from z where q < 4
)
select * from z;
It gives
ERROR: syntax error at or near "z"
What did i do wrong here?
I think this is because RECURSIVE is modifier of WITH statement, not a property of common table expression z, so you can use it like this:
with recursive
x as (
select 1 y
),
z(q) as (
select y from x
union all
select q + 1 from z where q < 4
)
select * from z;
sql fiddle demo