简体   繁体   English

SQL查询从多值列中提取数据,但前提是它满足特定条件

[英]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 byhaving

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.

相关问题 需要sql查询才能在一个查询中从同一张表中提取满足几组条件的数据 - Need sql query to pull back data that meets several groups of criteria from same table in one query Oracle SQL - 如何将单个值连接到多值列? - Oracle SQL - How to join a single value to a multi-value column? SQL按多值字符串列中的不同值分组 - SQL grouping by distinct values in a multi-value string column 将数据从多值D3数据库导入SQL问题 - Importing data from multi-value D3 database into SQL issues 如何从SQL的单个列中选择满足多个条件的行? - How to select rows that meets multiple criteria from a single column in SQL? Postgres查询,如果满足条件,则有条件地从记录子集中返回一个值 - Postgres query to conditionally return a value from a subset of records if it meets a criteria SQL查询/ Linq:如何从具有特定条件的多列中获取数据以馈送到下拉列表 - SQL Query/Linq : How to get data from mutiple column with specific criteria to feed to dropdownlist 将关联表展平为多值列? - Flatten association table to multi-value column? 仅查询符合条件的元素 - Query only elements which meets criteria 与execute(@sql)一起使用的多值参数 - Multi-value parameter used with execute(@sql)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM