如何在BQ中显示以特定字符开头的名称

时间:2018-05-02 18:40:10

标签: google-bigquery

如何显示以A,G和E开头的国家/地区名称?

import java.io.*;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;

import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;

public class ExcelDrawImagesOnCell {

 private static void drawImageOnExcelSheet(Sheet sheet, int row, int col, 
  int picHeight/*in px*/, int picWidth/*in px*/, int pictureIdx, boolean right) throws Exception {

  int DEFAULT_COL_WIDTH = 10 * 256; // 1/256th of a character width
  float DEFAULT_ROW_HEIGHT = 12.75f; //255 twips = 12.75 pt

  Row rowObject = sheet.getRow(row);
  float rowHeight = (rowObject!=null)?rowObject.getHeightInPoints():DEFAULT_ROW_HEIGHT;

  CreationHelper helper = sheet.getWorkbook().getCreationHelper();

  Drawing drawing = sheet.createDrawingPatriarch();

  ClientAnchor anchor = helper.createClientAnchor();
  anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE);

  int columWidthInPx = Math.round(sheet.getColumnWidthInPixels(col));
  int columWidth = sheet.getColumnWidth(col); // 1/256th of a character width

  anchor.setCol1(col); //first anchor determines upper left position
  anchor.setRow1(row);
  if (sheet instanceof XSSFSheet) {
   if (right) {
    anchor.setDx1(Units.pixelToEMU(columWidthInPx) - Units.pixelToEMU(picWidth)); //dx = right - wanted width
    anchor.setDy1(0); //dy = top
   } else {
    anchor.setDx1(0); //dx = left
    anchor.setDy1(0); //dy = top
   }
  } else if (sheet instanceof HSSFSheet) {
   if (right) {
    anchor.setDx1((int)Math.round(
     Units.DEFAULT_CHARACTER_WIDTH / 256f * 14.75 * DEFAULT_COL_WIDTH //right = constant for all possible column widths
     -
     picWidth * 14.75 * DEFAULT_COL_WIDTH / columWidth //wanted width = in relation to column width
    )); //dx = right - wanted width

    anchor.setDy1(0); //dy = top
   } else {
    anchor.setDx1(0); //dx = left
    anchor.setDy1(0); //dy = top
   }
  }

  anchor.setCol2(col); //second anchor determines bottom right position
  anchor.setRow2(row); 
  if (sheet instanceof XSSFSheet) {
   if (right) {
    anchor.setDx2(Units.pixelToEMU(columWidthInPx)); //dx = right
    anchor.setDy2(Units.pixelToEMU(picHeight)); //dy = top + wanted height
   } else {
    anchor.setDx2(Units.pixelToEMU(picWidth)); //dx = left + wanted width
    anchor.setDy2(Units.pixelToEMU(picHeight)); //dy = top + wanted height
   }
  } else if (sheet instanceof HSSFSheet) {
   if (right) {
    anchor.setDx2((int)Math.round(Units.DEFAULT_CHARACTER_WIDTH / 256f * 14.75 * DEFAULT_COL_WIDTH)); //dx = right = constant for all possible column widths
    anchor.setDy2((int)Math.round(picHeight * 14.75 * DEFAULT_ROW_HEIGHT / rowHeight)); //dy = top + wanted height
   } else {
    anchor.setDx2((int)Math.round(picWidth * 14.75 * DEFAULT_COL_WIDTH / columWidth)); //dx = left + wanted width
    anchor.setDy2((int)Math.round(picHeight * 14.75 * DEFAULT_ROW_HEIGHT / rowHeight)); //dy = top + wanted height
   }
  }

  drawing.createPicture(anchor, pictureIdx);

 }

 public static void main(String[] args) throws Exception {
  //Workbook wb = new XSSFWorkbook();
  Workbook wb = new HSSFWorkbook();
  Sheet sheet = wb.createSheet();

  InputStream is = new FileInputStream("samplePict.jpeg");
  byte[] bytes = IOUtils.toByteArray(is);
  int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
  is.close();

  sheet.setColumnWidth(1, 20*256); //set the column width to 20 character widths
  for (int r = 0; r < 10; r++ ) {
   sheet.createRow(r).createCell(1).setCellValue("    Picture " + (r+1));
   if ((r % 2) == 0) {
    drawImageOnExcelSheet(sheet, r, 1, 16/*px*/, 16/*px*/, pictureIdx, false);
   } else {
    drawImageOnExcelSheet(sheet, r, 1, 16/*px*/, 16/*px*/, pictureIdx, true/*right*/);
   }
  }

  if (wb instanceof XSSFWorkbook) {
   wb.write(new FileOutputStream("ExcelDrawImagesOnCell.xlsx"));
  } else if (wb instanceof HSSFWorkbook) {
   wb.write(new FileOutputStream("ExcelDrawImagesOnCell.xls"));
  }
  wb.close();
 }
}

1 个答案:

答案 0 :(得分:4)

以下是BigQuery Standard SQL

   
#standardSQL
WITH table1 AS(
  SELECT "America" AS country_name UNION ALL
  SELECT "Germany" AS country_name UNION ALL
  SELECT "England" AS country_name UNION ALL
  SELECT "Nauru" AS country_name UNION ALL
  SELECT "Brunei" AS country_name UNION ALL
  SELECT "Kiribati" AS country_name UNION ALL
  SELECT "Djibouti" AS country_name UNION ALL
  SELECT "Malta" AS country_name
)
SELECT *
FROM table1
ORDER BY CASE 
  WHEN LOWER(SUBSTR(country_name, 1, 1)) IN ('a', 'g', 'e') 
  THEN CONCAT(' ', country_name) ELSE country_name 
END  

预期产出

Row country_name     
1   America  
2   England  
3   Germany  
4   Brunei   
5   Djibouti     
6   Kiribati     
7   Malta    
8   Nauru    

如果您只需要那些国家/地区

#standardSQL
WITH table1 AS(
  SELECT "America" AS country_name UNION ALL
  SELECT "Germany" AS country_name UNION ALL
  SELECT "England" AS country_name UNION ALL
  SELECT "Nauru" AS country_name UNION ALL
  SELECT "Brunei" AS country_name UNION ALL
  SELECT "Kiribati" AS country_name UNION ALL
  SELECT "Djibouti" AS country_name UNION ALL
  SELECT "Malta" AS country_name
)
SELECT *
FROM table1
WHERE LOWER(SUBSTR(country_name, 1, 1)) IN ('a', 'g', 'e') 

#standardSQL
WITH table1 AS(
  SELECT "America" AS country_name UNION ALL
  SELECT "Germany" AS country_name UNION ALL
  SELECT "England" AS country_name UNION ALL
  SELECT "Nauru" AS country_name UNION ALL
  SELECT "Brunei" AS country_name UNION ALL
  SELECT "Kiribati" AS country_name UNION ALL
  SELECT "Djibouti" AS country_name UNION ALL
  SELECT "Malta" AS country_name
)
SELECT * 
FROM table1
WHERE REGEXP_CONTAINS(country_name, r'(?i)^(a|g|e)') 

以上都返回:

Row country_name     
1   America  
2   Germany  
3   England