Select where x in multi column subquery - sql

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

Related

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'));

INTERSECT ALL not working on PostgreSQL 11

The PostgreSQL operation INTERSECT ALL does not seem to work. What am I missing?
The following query returns just one row containing the value two, but I am expecting two with the value as I am using intersect all.
(
(select 1 as z)
union all
(select 2 as z)
union all
(select 2 as z)
)
intersect all
(select 2 as z)
Does anyone have a guess?
There is only one row (with a value of 2 for the column z) in the second operand to INTERSECT ALL so only that one can be used to find a matching partner in the other operand.
Add a second row to the second operand of the INTERSECT ALL and you'll have two rows in the result.
(SELECT 1 z
UNION ALL
SELECT 2 z
UNION ALL
SELECT 2 z)
INTERSECT ALL
(SELECT 2 z
UNION ALL
SELECT 2 z);
Or you might instead want a join.
SELECT *
FROM (SELECT 1 z
UNION ALL
SELECT 2 z
UNION ALL
SELECT 2 z) x1
INNER JOIN (SELECT 2 z) x2
ON x2.z = x1.z;
This is how it works in all version, not just 11.
The single value of '2' on the right side of the INTERSECT ALL is consumed upon matching, and can't match multiple times.
Do you really want WHERE EXISTS (..) instead?

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

SQLite: Combining 'WHERE' conditions

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

Select into #TempTable

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