更新时间:2023-02-06 09:11:59
执行与两个表的连接并按如下所示添加 GROUP BY
子句:
SELECT'doc_id 59678 has'|| count(to_doc_id)as cnt
FROM kms_doc_ref_currnt_v kv,kms_fil_nm_t kt
WHERE kt.doc_id = kv.to_doc_id
AND kv.branch_id = 1
AND kv.ref_typ_cd in('CONREF ','KBA')
AND kv.to_doc_id = 59678
GROUP BY kv.to_doc_id;
编辑:
要从 kms_doc_ref_currnt_v
中获取所有记录,而不管它们在 kms_fil_nm_t
和 kv中的参考可用性.to_doc_id = 59678
,如下所示:
SELECT'doc_id 59678 has'||。 count(to_doc_id)as cnt
FROM kms_doc_ref_currnt_v kv
LEFT JOIN kms_fil_nm_t kt
ON(kt.doc_id = kv.to_doc_id)
WHERE kv.branch_id = 1
AND ('CONREF','KBA')
GROUP BY kv.to_doc_id中的kv.ref_typ_cd;
取代硬编码 59678
,您可以想要写:
SELECT'doc_id'|| kt.doc_id || `有'|| count(to_doc_id)as cnt
FROM kms_doc_ref_currnt_v kv
LEFT JOIN kms_fil_nm_t kt
ON(kt.doc_id = kv.to_doc_id)
WHERE kv.branch_id = 1
AND ('CONREF','KBA')
GROUP BY kv.to_doc_id,kt.doc_id中的kv.ref_typ_cd;
Not sure the title explains the problem well; this is what I'm working with,
I have the following tables,
-- table = kms_doc_ref_currnt_v
DOC_ID VARCHAR2(19)
TO_DOC_ID VARCHAR2(19)
BRANCH_ID NUMBER(8)
REF_TYP_CD VARCHAR2(20)
-- table = kms_fil_nm_t
DOC_ID VARCHAR2(19) PRIMARY KEY UNIQUE
For example, I can get a count of all kms_doc_ref_currnt_v
records that have a to_doc_id = 59678
, where 59678 is one value in kms_fil_nm_t
, with this query,
select 'doc_id 59678 has ' || count(to_doc_id) as cnt from kms_doc_ref_currnt_v where branch_id=1 and ref_typ_cd in ('CONREF', 'KBA') and to_doc_id=59678;
kms_doc_ref_currnt_v.to_doc_id
is a field that has one of the kms_fil_nm_t.doc_id
values. kms_doc_ref_currnt_v.doc_id
is also one of the values in kms_fil_nm_t
.
The single query I'm looking for would loop over each kms_fil_nm_t.doc_id
and count all the rows in kms_doc_ref_currnt_v
that have a similar to_doc_id
. Each row returned would look like the output of the query above. Here's example output,
doc_id 1 has 32
doc_id 2 has 314
doc_id 3 has 2718
doc_id 4 has 42
doc_id 5 has 128
doc_id 6 has 11235
.
.
.
Probably simple but I just can't figure it out.
Do a join with two tables and add a GROUP BY
clause as below:
SELECT 'doc_id 59678 has ' || count(to_doc_id) as cnt
FROM kms_doc_ref_currnt_v kv, kms_fil_nm_t kt
WHERE kt.doc_id= kv.to_doc_id
AND kv.branch_id=1
AND kv.ref_typ_cd in ('CONREF', 'KBA')
AND kv.to_doc_id=59678
GROUP BY kv.to_doc_id;
EDIT:
To get all records from kms_doc_ref_currnt_v
irrespective of their reference availability in kms_fil_nm_t
and kv.to_doc_id=59678
, do like this:
SELECT 'doc_id 59678 has ' || count(to_doc_id) as cnt
FROM kms_doc_ref_currnt_v kv
LEFT JOIN kms_fil_nm_t kt
ON (kt.doc_id= kv.to_doc_id )
WHERE kv.branch_id=1
AND kv.ref_typ_cd in ('CONREF', 'KBA')
GROUP BY kv.to_doc_id;
to replace the hardcoding 59678
, you may want to write:
SELECT 'doc_id ' || kt.doc_id || ` has ' || count(to_doc_id) as cnt
FROM kms_doc_ref_currnt_v kv
LEFT JOIN kms_fil_nm_t kt
ON (kt.doc_id= kv.to_doc_id )
WHERE kv.branch_id=1
AND kv.ref_typ_cd in ('CONREF', 'KBA')
GROUP BY kv.to_doc_id, kt.doc_id;