如果存在单词,请删除

时间:2018-11-14 19:31:00

标签: sql amazon-redshift

我有一个ETF名称和它们的代码列表,但是看起来ETF名称有时会重复,这是由于末尾加上了“ etf”这个词:

from scipy.optimize import leastsq
import numpy as np

# Read the data files    
Pm = np.array( [ float(v) for v in open( "impulse_data.txt", "r" ).readlines() ] )
print type(Pm), Pm.shape

tm = np.array( [ float(v) for v in open( "Impulse_time_axis.txt", "r" ).readlines() ] )
print type(tm), tm.shape

output = np.array( [ float(v) for v in open( "Output_data.txt", "r" ).readlines() ] )
print type(output), output.shape

tout = np.array( [ float(v) for v in open( "Output_time_axis.txt", "r" ).readlines() ] )
print type(tout), tout.shape

# Define the function that calculates the residuals
def residuals(  coeffs, output, tm ):
    dt, alpha1, alpha2 = coeffs
    res = np.zeros( tm.shape )
    for n,t in enumerate(tm):
        # integrate to "t"
        value = sum( Pm[:n]*(1-np.e**( -(alpha1 * (tm[:n]+dt))) )*np.e**(-(alpha2 * (tm[:n]+dt))) )
        # retrieve output at t+dt
        n1 = (np.abs(tout - (t+dt) )).argmin()
        # construct the residual
        res[n] =  output[n1] - value
    return res

# Initial guess for the parameters
x0 = (10.,1.,1.)

# Run the least squares routine
coeffs, flag = leastsq( residuals, x0, args=(output,tm) )

# Report the results
print( coeffs )
print( flag )

如何编写它,使得如果ETF名称以“ etf”结尾,那么提取就从输出中提取出来了?

当前我的查询如下所示:

Ishares Global Tech
Ishares Global Tech etf
Ishares Edge Msci Usa Momentum Factor
Ishares Edge Msci Usa Momentum Factor etf

3 个答案:

答案 0 :(得分:2)

您可以像下面这样使用REPLACE:

select REPLACE(display_name,' ETF','')
from table
where RIGHT(display_name,4) = ' ETF'

答案 1 :(得分:2)

我会使用regex_replace

SELECT REGEXP_REPLACE(display_name, '^.*(etf)$', '', 'i')
from table 

我意识到您想过滤掉以'etf'结尾的所有内容,然后您就可以这样做

select initcap(display_name) name, upper(symbol) as symbol, sum(amount) aum 
from table 
where display_name !~* '^.*etf$'
group by 1,2

答案 2 :(得分:1)

假设' etf'仅发生一次,您可以这样做:

select split_part(display_name, ' etf', 1)

或者,我会去case

select (case when display_name like '% etf'
             then left(display_name, len(display_name) - 4)
             else display_name
        end) as new_display_name