SQL - Count number of columns that are having matching values then add count to the additional column.
Assuming I have a temp table with this value
| id | prod_name | attr1 | attr2 | attr3 | attr4 |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| 1 | test name | brand | bottle | size | 12x |
then I will need to compare it to product table having values of column by column:
id | prod_name | attr1 | attr2 | attr3 | attr4 |
---|---|---|---|---|---|
3 | test name | brand | bottle | size | 12x |
4 | some name | brand | satchet | size | 1x |
5 | sample | brand | bottle | size | 23x |
expected result should be:
id | prod_name | attr1 | attr2 | attr3 | attr4 | CNT_Match_cols |
---|---|---|---|---|---|---|
3 | test name | brand | bottle | size | 12x | 4 |
4 | some name | brand | satchet | size | 1x | 2 |
5 | sample | brand | bottle | size | 23x | 3 |
*CNT_Match_cols column is get of how many attr(X) columns that are having same values.
1条答案
按热度按时间holgip5t1#
You can cross join your tables and then count the attribute matches: