连接2个查询

时间:2015-05-12 02:31:50

标签: sql

我有以下两个问题:

SELECT Count(DISTINCT custpersonid) AS count1 
FROM   my_precustperson 
WHERE  rsv_no = 1510708 
       AND custpersonid IS NOT NULL 

SELECT Count(*) AS count2 
FROM   my_precustperson 
WHERE  rsv_no = 1510708 
       AND custpersonid IS NULL 

我希望将值count1加到count2(count1 + count2),但我不知道该怎么做。 请帮我找出最适合这种情况的查询。

2 个答案:

答案 0 :(得分:2)

我将使用select,其中count distinct和之前的COUNT以及NULL的条件SELECT count(DISTINCT custpersonid) AS count1, count(case when custpersonid IS NULL then 1 else null end) as count2 FROM my_precustperson WHERE rsv_no = 1510708 。 (COUNT只计算非空行。)

    AssetManager assetManager = getAssets();
    InputStream in = null;
    OutputStream out = null;
    File file = new File(getFilesDir(), "sample.pdf");
    try {
        in = assetManager.open("sample.pdf");
        out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e) {
        Log.e("tag", e.getMessage());
    }

    Intent intent = new Intent(this, MyPdfViewerActivity.class);
    intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, getFilesDir() + "/sample.pdf");
    startActivity(intent);

}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}}

ANSI SQL兼容!

答案 1 :(得分:1)

SELECT (SELECT Count(DISTINCT custpersonid) AS count1 
    FROM   my_precustperson 
    WHERE  rsv_no = 1510708 
           AND custpersonid IS NOT NULL) 
    + (SELECT Count(*) AS count2 
      FROM   my_precustperson 
      WHERE  rsv_no = 1510708 
             AND custpersonid IS NULL); 
相关问题