Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8
CH-0000001 IN-0000001 IN-0000001 ALL NULL NULL NULL NULL
CH-0000001 PR-0000001 NULL NULL PR-0000001 ALL NULL NULL
CH-0000001 PU-0000001 NULL NULL NULL NULL PU-0000001 ALL
Hey guys I am trying to squish all this information into a single row based on a key Col1. I have seen a trick using MAX and SUM but it only seems to work for numbers. Here is the desired result.
Col1 Col3 Col4 Col5 Col6 Col7 Col8
CH-0000001 IN-0000001 ALL PR-0000001 ALL PU-0000001 ALL
Also, this problem arose from LEFT JOINing 3 tables from one master table of keys. Let me know if my approach to getting to this state is the wrong way to approach this problem. Here is the query
SELECT * FROM GCL_Blackboard A
LEFT JOIN GCL_Investors B ON A.Adjective_ID = B.Investor_ID
LEFT JOIN GCL_Loan_Programs C ON A.Adjective_ID = C.Program_ID
LEFT JOIN GCL_Loan_Purposes D ON A.Adjective_ID = D.Purpose_ID
I think this is the logic you want:
SELECT a.col1,
max(coalesce(b.col2, c.col2, d.col2)) as col2,
max(coalesce(b.col3, c.col3, d.col3)) as col3,
max(coalesce(b.col4, c.col4, d.col4)) as col4,
max(coalesce(b.col5, c.col5, d.col5)) as col5,
max(coalesce(b.col6, c.col6, d.col6)) as col6,
max(coalesce(b.col7, c.col7, d.col7)) as col7,
max(coalesce(b.col8, c.col8, d.col8)) as col8
FROM GCL_Blackboard A LEFT JOIN
GCL_Investors B
ON A.Adjective_ID = B.Investor_ID LEFT JOIN
GCL_Loan_Programs C
ON A.Adjective_ID = C.Program_ID LEFT JOIN
GCL_Loan_Purposes D
ON A.Adjective_ID = D.Purpose_ID
GROUP BY a.col1;
The query is a bit of a guess, because I don't know where the columns are coming from.
You can do:
SELECT A.Col1,
MIN(Col3) Col3,
MIN(Col4) Col4 ....
FROM GCL_Blackboard A
LEFT JOIN GCL_Investors B ON A.Adjective_ID = B.Investor_ID
LEFT JOIN GCL_Loan_Programs C ON A.Adjective_ID = C.Program_ID
LEFT JOIN GCL_Loan_Purposes D ON A.Adjective_ID = D.Purpose_ID
GROUP BY A.Col1
Related
I have below tables.
Table1
Col1,col2,col3,col4
1,2,3,''
3,4,5,''
Table2
col5,col6
4,5
1,1
Table3
col7
7
0
Want the output as below:
output table:
col1,col2,col3,col4,col5,col6,col7
4,6,8,'',5,6,7
Could you please help with the above requirement.
Aggregate before joining:
select *
from (select sum(col1) as col1, sum(col2) as col2, sum(col3) as col3
from t1
) t1 cross join
(select sum(col5) as col5, sum(col6) as col6
from t2
) t2 cross join
(select sum(col7) as col7
from t3
) t3;
Note: Oracle treats '' as NULL, so SUM() works fine on that column.
I have following table in Postgres 11:
col1 col2 col3 col4
1 trial_1 ag-270 ag
2 trial_2 ag ag
3 trial_3 methotexate (mtx) mtx
4 trial_4 mtx mtx
5 trial_5 hep-nor-b nor-b
I would like to search each value of col4 throughout the column col3. If the value in col4 exists in col3, I would like to keep the rows else the row should be excluded.
Desired output is:
col1 col2 col3 col4
1 trial_1 ag-270 ag
2 trial_2 ag ag
3 trial_3 methotexate (mtx) mtx
4 trial_4 mtx mtx
I could not try anything on this as I am unable to find a solution to this yet.
If the value in col4 exists in col3, I would like to keep the rows.
... translates to:
SELECT *
FROM tbl a
WHERE EXISTS (SELECT FROM tbl b WHERE b.col3 = a.col4);
db<>fiddle here
Produces your desired result.
This can be done as an inner join:
select distinct t.col1, t.col2, t.col3, t,col4
from T t inner join T t2 on t2.col3 = t.col4
select a.*
from myTable a
where exists (
select 1
from myTable b
where b.col3 = a.col4)
If your table has many rows, you should ensure that col3 is indexed.
I have the following 3 tables.
PROP_MASTER
--------------------
BORO
BLOCK
LOT
COL1
COL2
COL3
TABLE2
-------------
BORO
BLOCK
LOT
COL4
TABLE3
-------------
BORO
BLOCK
LOT
COL5
In one query I would like to get
Boro
Block
Lot
COL1
COL2
COL3
COL4
COL5
However, if record doesn't exist for the same BORO,BLOCK AND LOT IN either TABLE2 COL4 should return 0 and if record doesn't exist in Table3 then COL5 SHOULD BE ZERO.
How can I join these 3 tables.
Select B.BORO,B.BLOCK,B.LOT,B.COL1,B.COL2,B.COL3
FROM PROP_MASTER B, TABLE2 T2, TABLE3 T3
WHERE B.BORO=T2.BORO AND B.BORO=T3.BORO AND
B.BLOCK=T2.BLOCK AND B.BLOCK=T3.BLOCK AND
B.LOT=T3.LOT AND B.LOT=T3.LOT
I think your best best is to LEFT JOIN Table2 and Table3 to your PROP_MASTER based on the common attributes (BORO, BLOCK, and LOT), and then use COALESCE:
SELECT
PROP_MASTER.BORO,
PROP_MASTER.BLOCK,
PROP_MASTER.LOT,
PROP_MASTER.COL1,
PROP_MASTER.COL2,
PROP_MASTER.COL3,
COALESCE(TABLE2.COL4, 0) AS COL4,
COALESCE(TABLE3.COL5, 0) AS COL5
FROM PROP_MASTER
LEFT JOIN TABLE2 ON
TABLE2.BORO = PROP_MASTER.BORO
AND TABLE2.BLOCK = PROP_MASTER.BLOCK
AND TABLE2.LOT = PROP_MASTER.LOT
LEFT JOIN TABLE3 ON
TABLE3.BORO = PROP_MASTER.BORO
AND TABLE3.BLOCK = PROP_MASTER.BLOCK
AND TABLE3.LOT = PROP_MASTER.LOT
do union
select col from PROP_MASTER
union
select col from TABLE2
union
select col from TABLE3
you can use this
Select B.BORO,B.BLOCK,B.LOT,B.COL1,B.COL2,B.COL3,T2.COL4,T3.COL5
FROM PROP_MASTER B
LEFT join TABLE2 T2 ON B.BORO=T2.BORO AND B.BLOCK=T2.BLOCK AND B.LOT=T2.LOT
LEFT join TABLE3 T3 ON B.BORO=T3.BORO AND B.BLOCK=T3.BLOCK AND B.LOT=T3.LOT
WHERE
(T2.BORO IS NULL and T2.BLOCK IS NULL and T2.LOT IS NULL) OR
(T3.BORO IS NULL and T3.BLOCK IS NULL and T3.LOT IS NULL)
Is it a good approach to Find Maximums from nested SQL query and then use that maximum value again in the same query.
For example:
Select Col1, Col2, Col3, Col2/Col3 AS Col4, CASE Alot of statements END Col5
from Table A inner join Table B on A.Id = B.Id
Inner Join Table C on B.Id = C.Id
Inner Join Table D on C.Id = D.Id
Now I need to find Maximum integer from Col5 and then later use that Max Value to divide Col4 value.
I also need to display col1 and col2 along with results.
Expected result:
Col1, Col2, (Col4/MaxValue of Col5)
Could anybody guide me what's the best solution in this case?
If those are the only results you need, then no subquery/CTE is needed:
Select Col1, Col2,
( (Col2 / Col3) /
max(case Alot of statements end) over ()
) as ratio
from Table A inner join
Table B
on A.Id = B.Id Inner Join
Table C
on B.Id = C.Id Inner Join
Table D
on C.Id = D.Id;
Tim's answer is definitely a good answer if you need col5 more than once in the final result set.
You can use a common table expression:
WITH cte AS (
SELECT Col1, Col2, Col3, Col2/Col3 AS Col4,
CASE Alot of statments END Col5
FROM TableA A
INNER JOIN TableB B
ON A.Id = B.Id
INNER JOIN TableC C
ON B.Id = C.Id
INNER JOIN TableD D
ON C.Id = D.Id
)
SELECT t.Col1, t.Col2,
t.Col4 / MAX(t.Col5) OVER ()
FROM cte t
I would like to
Select columns from table a
Where extension table a = extension in table b
And department in both table b and table c match
Based on that match that deptartment in table c should = yes
Select col1, col2, col3, col4, col5 from table a
Where col2 table a = col2 table b
And col3 in table b = col3 in table c
And col4 tabel c = yes
can anyone help.
Although your question is ambiguous have you tried:
SELECT A.COL1, A.COL2, A.COL3, A.COL4, A.COL5
FROM TABLE_A A
JOIN TABLE_B B
ON A.Extension = B.Extension
JOIN TABLE_C C
ON B.Department = C.Department
AND C.Department = 'yes'
You can do this in different ways. As following:
Way 1
SELECT col1, col2, col3, col4, col5
FROM tableA
JOIN tableB ON tableA.col2 = tableB.col2
JOIN tableC ON tableB.col3 = tableC.col3 AND tableC.col4 = 'yes'
Way 2
SELECT col1, col2, col3, col4, col5
FROM tableA
JOIN tableB ON tableA.col2 = tableB.col2
JOIN tableC ON tableB.col3 = tableC.col3
WHERE tableC.col4 = 'yes'
Way 3
SELECT *
INTO #col4yesTable
FROM tableC
Where col4 = 'yes'
SELECT tableA.col1, tableA.col2, tableA.col3, tableA.col4, tableA.col5
FROM #col4yesTable
JOIN tableB ON #col4yesTable.col3 = tableB.col3
JOIN tableB ON tableA.col2 = tableB.col2
Depends on your need you can use one of them. The last one, creates a temporary table to store all tableC objects that has col4 = 'yes'