我有以下两个问题:
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),但我不知道该怎么做。
请帮我找出最适合这种情况的查询。
答案 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);