[英]SQL query to pull data from multi-value column but only if it meets specific criteria
I have a table doc_source which contains id, document_id and file_name. 我有一个表doc_source,其中包含id,document_id和file_name。
The file_name contains various file types such as *pdf, *dgn, *doc, etc. I need to write a query which will pull all documents that have *pdf files and only *pdf files. file_name包含各种文件类型,例如* pdf,* dgn,* doc等。我需要编写一个查询,该查询将提取所有具有* pdf文件且只有* pdf文件的文档。 In other words, lets say I have the following data:
换句话说,可以说我有以下数据:
ID DOCUMENT_Id FILE_NAME
1 100 abc.pdf
2 100 def.doc
3 200 ghi.pdf
4 300 jkl.pdf
5 300 mno.doc
I want to pull all the documents that have only pdfs. 我想拉所有只有pdf的文档。 So in this case, I'd want a query that extracts only Doc Id 200 since that is the only one which has ONLY pdfs and no other file types.
因此,在这种情况下,我想要一个仅提取Doc Id 200的查询,因为这是仅有PDF且没有其他文件类型的唯一查询。
I tried doing something like this, but something is obviously not quite right: 我尝试做这样的事情,但显然不完全正确:
WITH T1 AS (SELECT DOCUMENT_ID, FILE_NAME FROM DOC_SOURCE)
SELECT DOCUMENT_ID, FILE_NAME FROM DOC_SOURCE AS T2
WHERE FILE_NAME LIKE '%PDF'
GROUP BY DOCUMENT_ID, FILE_NAME
HAVING COUNT(DOCUMENT_ID) = (SELECT COUNT(DOCUMENT_ID) FROM T1)
I would use group by
and having
: 我将使用
group by
并having
:
select document
from t
group by document
having sum(case when file_name like '%.pdf' then 1 else 0 end) = count(*);
That is, the number of file names for each document that end in pdf is equal to the total number of documents. 即,以pdf结尾的每个文档的文件名数量等于文档总数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.