如何在BigQuery SLQ中仅替换完整字符串而不替换子字符串?

时间:2018-08-29 21:07:00

标签: sql google-bigquery

我正在使用REPLACE函数,但这仅针对子字符串而不是整个字符串。我想用这些值替换这些键:

extension SelfieViewController:  AVCaptureVideoDataOutputSampleBufferDelegate{
    func setupAVCapture(){
        session.sessionPreset = AVCaptureSessionPreset640x480

        let devices = AVCaptureDevice.devices();
        // Loop through all the capture devices on this phone
        for device in devices {
            // Make sure this particular device supports video
            if (device.hasMediaType(AVMediaTypeVideo)) {
                // Finally check the position and confirm we've got the front camera
                if(device.position == AVCaptureDevicePosition.Front) {
                    captureDevice = device as? AVCaptureDevice
                    if captureDevice != nil {
                        beginSession()
                        break
                    }
                }
            }
        }
    }

    func beginSession(){
        var err : NSError? = nil
        var deviceInput:AVCaptureDeviceInput = AVCaptureDeviceInput(device: captureDevice, error: &err)
        if err != nil {
            println("error: \(err?.localizedDescription)")
        }
        if self.session.canAddInput(deviceInput){
            self.session.addInput(deviceInput)
        }

        self.videoDataOutput = AVCaptureVideoDataOutput()
        var rgbOutputSettings = [NSNumber(integer: kCMPixelFormat_32BGRA):kCVPixelBufferPixelFormatTypeKey]
        self.videoDataOutput.alwaysDiscardsLateVideoFrames=true
        self.videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL)
        self.videoDataOutput.setSampleBufferDelegate(self, queue:self.videoDataOutputQueue)
        if session.canAddOutput(self.videoDataOutput){
            session.addOutput(self.videoDataOutput)
        }
        self.videoDataOutput.connectionWithMediaType(AVMediaTypeVideo).enabled = true

        self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session)
        self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill

        var rootLayer :CALayer = self.cameraView.layer
        rootLayer.masksToBounds=true
        self.previewLayer.frame = rootLayer.bounds
        rootLayer.addSublayer(self.previewLayer)
        session.startRunning()

    }

    func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
        // do stuff here
    }

    // clean up AVCapture
    func stopCamera(){
        session.stopRunning()
    }

}

我的桌子看起来像这样:

key   | value
--------------
'b'   | 'blue'
'bl'  | 'blue'
'BLUE'| 'blue'

当我这样做时:

color | age 
------------
'b'   | 17
'blue'| 10
'BLUE'| 10

我得到一个看起来像这样的表

SELECT
color,
age, 
REPLACE(REPLACE(REPLACE(color, 'b', 'blue'), 'bl', 'blue'), 'BLUE', 'blue) as color_cleaned
FROM my_table

如何使REPLACE仅匹配完整字符串而不匹配子字符串?

1 个答案:

答案 0 :(得分:1)

以下BigQuery标准SQL示例

#standardSQL
WITH `project.dataset.map` AS (
  SELECT 'b' key, 'blue' value UNION ALL
  SELECT 'bl', 'blue' UNION ALL
  SELECT 'BLUE', 'blue'
), `project.dataset.my_table` AS (
  SELECT 'b' color, 17 age UNION ALL
  SELECT 'blue' , 10 UNION ALL
  SELECT 'BLUE' , 10 
)
SELECT color, age, value AS color_cleaned
FROM `project.dataset.my_table`
JOIN `project.dataset.map`
ON LOWER(key) = LOWER(color)

结果为

Row     color   age     color_cleaned    
1       b       17      blue     
2       blue    10      blue     
3       BLUE    10      blue     
相关问题