I wish to build a query to calculate the table score based on a column combination in the table.
So, I have table 'A' that contains database metadata.
| DE_Type | Column_Name | Table_Name | Data_System_Instance | Database_Name | Data_Element_Counts |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| Name | SalesName | tbl_sales | DB_PROD | Daily_Sales | 40 |
| Govt_ID | SSN | tbl_sales | DB_PROD | Daily_Sales | 40 |
| Name | CustName | tbl_customer | DB_PROD | Daily_Customer | 5200 |
| Name | PIC | tbl_supplier | DB_PROD | Daily_Supplier | 21 |
| Email Address | Email | tbl_supplier | DB_PROD | Daily_Supplier | 21 |
| Phone Number | Handphone | tbl_supplier | DB_PROD | Daily_Supplier | 21 |
and table 'B' as a parameter table
ID | DE_1 | DE_2 | Score |
---|---|---|---|
1 | Name | Govt_ID | 3 |
2 | Name | Passport | 3 |
3 | Name | Email Address | 2 |
4 | Name | Phone Number | 2 |
5 | Govt_ID | Email Address | 1.5 |
I am imagining will have an output like this:
Table_Name | Data_Element_Counts | DE_Score |
---|---|---|
tbl_sales | 40 | 3 |
tbl_supplier | 21 | 4 |
tbl_customer | 5200 | 1 |
Table_Name is obtained from Table 'A' Table_Name; Data_Element_Counts is obtained from Table 'A' Data_Element_Count; DE_Score is the hardest part, it needs to gather all DE_type from single table, then lookup to table 'B', to find whether the collected columns have a combination like in table 'B' (DE_1 & DE-2)
So, how can I develop a query to have an output like this?
1条答案
按热度按时间wf82jlnq1#
As you did not explain how the
DE_1
-DE_2
comes about, I assumed, you need to use all possible combination ofDE_Type
to match with the parameter table.The first CTE finds the combination of
DE_Type
after that join to the parameter table to get theScore
.For the case of
tbl_customers
it will not have any match with the parameter table at all. And it handled separately andUNION
with the matching result.