[英]Oracle Query to Join Two Tables based on Conditions & Group By
I have table in below format,我有以下格式的表格,
Table 1表格1
Bank银行 | Category类别 | Month_Year月_年 | Loan_Type贷款类型 | Outstanding杰出的 |
---|---|---|---|---|
SI SI | R1 R1 | JAN-21 1月21日 | Home家 | 10 10 |
SI SI | R1 R1 | JAN-21 1月21日 | Land土地 | 50 50 |
SI SI | R2 R2 | FEB-21 2 月 21 日 | Home家 | 30 30 |
SI SI | R2 R2 | MAR-21 MAR-21 | Car车 | 40 40 |
Table 2表 2
Bank银行 | Loan_Type贷款类型 |
---|---|
SI SI | Home家 |
SI SI | Land土地 |
SI SI | Car车 |
SI SI | Jewel宝石 |
SI SI | Education教育 |
I would like to convert the table A and B in to below format using join/query.我想使用连接/查询将表 A 和 B 转换为以下格式。 The data(all rows) from the table-2 should get added based of the Category and Month_Year.表 2 中的数据(所有行)应该根据 Category 和 Month_Year 添加。
BANK银行 | Category类别 | Month_Year月_年 | Loan_Type贷款类型 | Outstanding杰出的 |
---|---|---|---|---|
SI SI | R1 R1 | JAN-21 1月21日 | Home家 | 10 10 |
SI SI | R1 R1 | JAN-21 1月21日 | Land土地 | 50 50 |
SI SI | R1 R1 | JAN-21 1月21日 | Car车 | 0 0 |
SI SI | R1 R1 | JAN-21 1月21日 | Jewel宝石 | 0 0 |
SI SI | R1 R1 | JAN-21 1月21日 | Education教育 | 0 0 |
SI SI | R2 R2 | FEB-21 2 月 21 日 | Home家 | 30 30 |
SI SI | R2 R2 | FEB-21 2 月 21 日 | Land土地 | 0 0 |
SI SI | R2 R2 | FEB-21 2 月 21 日 | Car车 | 0 0 |
SI SI | R2 R2 | FEB-21 2 月 21 日 | Jewel宝石 | 0 0 |
SI SI | R2 R2 | FEB-21 2 月 21 日 | Education教育 | 0 0 |
SI SI | R2 R2 | MAR-21 MAR-21 | Home家 | 0 0 |
SI SI | R2 R2 | MAR-21 MAR-21 | Land土地 | 0 0 |
SI SI | R2 R2 | MAR-21 MAR-21 | Car车 | 40 40 |
SI SI | R2 R2 | MAR-21 MAR-21 | Jewel宝石 | 0 0 |
SI SI | R2 R2 | MAR-21 MAR-21 | Education教育 | 0 0 |
Principally a CROSS JOIN
needed among the tables after Category
and Month_Year
columns are distinctly selected, and Outstanding
column is added in the main query as zero for non-matching values, otherwise returning values of it such as主要是一个CROSS JOIN
后的表中所需Category
和Month_Year
列明显选择, Outstanding
柱在主查询作为零为不匹配的值添加,否则返回它的值,如
SELECT t2.Bank, t2.Category, t2.Month_Year, t2.Loan_Type,
NVL(t1.Outstanding,0) AS Outstanding
FROM (SELECT *
FROM (SELECT DISTINCT Category, Month_Year FROM table1)
CROSS JOIN table2) t2
LEFT JOIN table1 t1
ON t2.Category = t1.Category
AND t2.Month_Year = t1.Month_Year
AND t2.Loan_Type = t1.Loan_Type
ORDER BY t2.Category, t2.Month_Year, t1.Outstanding NULLS LAST
Create a list of all items needed and left join Table1.创建所需的所有项目的列表并左连接表 1。 For example例如
select items.Bank, items.Category, items.Month_Year, items.Loan_Type, coalesce(t1.Outstanding, 0) Outstanding
from (
select t2.Bank, t2.Loan_Type, my.Month_Year, cat.Category
from (select distinct Month_Year
from Table1) my
cross join (select distinct Category
from Table1) cat
cross join Table2 t2
) items
left join Table1 t1 on items.Bank = t1.Bank and items.Loan_Type = t1.Loan_Type and items.Month_Year = t1.Month_Year and items.Category = t1.Category;
If there exists a table Categories
replace it instead of derived categories in the query.如果存在表Categories
替换它而不是查询中的派生类别。 You may also wish to generate a set of Month_Year from prameters or use a calendar table.您可能还希望从参数生成一组 Month_Year 或使用日历表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.