Home > Software design >  Combining 2 different value into 1 in SQL
Combining 2 different value into 1 in SQL

Time:01-19

First of all, I apologize for not being able to show my question with code. I have 2 tables. I'm merging these tables and returning a result. My values ​​include values ​​that are the same but written with different letters ( for example, INSTALL and INSTALL). These two values ​​are essentially the same. but it returns 2 different results because they are written with different letters. what I want is to convert the İNSTALL value to the INSTALL value and increase the total INSTALL value to 5. Any idea?

Column1 Values
INSTALL 2
İNSTALL 3

CodePudding user response:

Use UPPER() function available in different DBMS.

select UPPER(Column1),sum(values) from Table group by UPPER(Column1)

UPPER: https://www.w3schools.com/sql/func_sqlserver_upper.asp

CodePudding user response:

Depending on the anticipated overlap of letters, you could go about this a couple of ways.

Assuming the headers are the same across both tables, and only the cases are different (if they're not, use aliases in the CTE):

SELECT 
LOWER(column_1) AS column_1,
SUM(Values) AS values_total
FROM (SELECT * FROM table_1 UNION SELECT * FROM table_2) combined_tables
GROUP BY 1

If there is different spelling across both tables, you could use a lower case cast of only the rightmost or leftmost letters.

SELECT 
LOWER(RIGHT(column_1),3) AS column_1,
SUM(Values) AS values_total
FROM (SELECT * FROM table_1 UNION SELECT * FROM table_2) combined_tables
GROUP BY 1

If you expect a mix of misspellings, non-text characters, I'd suggest using regex to do fuzzy matching on the column values.

  •  Tags:  
  • Related