简体   繁体   中英

Oracle - SQL Query to return a value from a column in another table where value equals a different column

From within the results of a query I need to take the first 3 digits (got this part - substr) from a column in one table and pull the value from a second table where the 3 digit code is in a different column of the same row. While I'm not looking for assistance in this part I will be grouping by the code column for reference

    Example
    Table 1        Table 2
    3_Digit        Code_Column    Description
    123456         123            Blue
    103456         103            Green

so if my substr query return 123 5 times I'm looking for

    Blue 3 
select  t2.Code_Column, count(*)
from table1 t1
join table2 t2
on  substr(t1.digit, 0, 3) = t2.Code_Column
group by t2.Code_Column

SQL Fiddle sample

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM