Get RowsWise data with Columns Name ColumnWise - sql

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

Related

SQL query with array of counts?

I have a table which looks like this:
record no firstType secondtype win?
1 X A 1
2 X A 0
3 X B 1
4 Y B 0
5 Y B 1
6 X B 1
7 X B 1
and what I need output is this.
firstType secondType winCounts
X [A,B] [A:1,B:3]
Y [B] [B:1]
So notice how the arrays under secondType tell where they OCCURED with firstType, while the arrays under winCounts tell how many wins of each secondType came with each firstType.
I can make the arrays using ARRAY_AGG but I'm lost for any possible way to make the winCounts column.
Use two levels of aggregation:
select firsttype, array_agg(secondtype order by secondtype),
array_agg(secondtype || ':' || wins order by secondtype)
from (select firsttype, secondtype, sum(win) as wins
from t
group by firsttype, secondtype
) t
group by firsttype;
Here's a more-complicated solution with a lambda method, because why not:
SELECT
PP.firstType AS "firstType"
, ARRAY_DISTINCT(
ARRAY_AGG(PP.secondType)
) AS "secondType"
, ZIP_WITH(
ARRAY_DISTINCT(
ARRAY_AGG(PP.secondType)
),
ARRAY_AGG(PP.count_str),
(x, y) -> x || ':' || y
) AS "winCount"
FROM (
SELECT
firstType
, secondType
, CAST(SUM("win?") AS VARCHAR(5))
FROM dataTable
WHERE "win?" > 0
GROUP BY
firstType
, secondType
) AS PP (firstType, secondType, count_str)
GROUP BY PP.firstType;

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 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

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