简体   繁体   English

SQL选择行,其中多值字段的值仅出现一次

[英]SQL Select Rows where a value of a multiple value field occurs only once

I have the following problem: 我有以下问题:

I have a table with different columns describing objects. 我有一个表,其中包含描述对象的不同列。 One of this column let's assume can contain the values 1,2,3,4,5,6,7,8,9,10. 我们假设其中一列可以包含值1,2,3,4,5,6,7,8,9,10。 Within this table objects can contain all of these values or some just contain for example value 1,3,5 (so 0 to n values) 在这个表中,对象可以包含所有这些值,或者一些只包含例如值1,3,5(所以0到n的值)

Now I want to find all the objects containing only the value 1 and 2, but I do not want them in my result set if they contain 1,2,3 or other combinations but (1,2). 现在我想找到只包含值1和2的所有对象,但如果它们包含1,2,3或其他组合(1,2),我不希望它们在我的结果集中。

How do I write this SQL statement? 我该如何编写这个SQL语句?

Sample data (Result set to be expected --> Mark and Michael): 样本数据(预期结果集 - > Mark和Michael):

+---------+--------------------+---------------------------+--+
|   OBJ   | OBJ_CHARACTERISTIC | CHARACTERISTIC_DATE_ADDED |  |
+---------+--------------------+---------------------------+--+
| Mark    |                  1 |                15.01.2018 |  |
| Mark    |                  2 |                15.02.2018 |  |
| Jimmy   |                  1 |                31.01.2018 |  |
| Jimmy   |                  2 |                11.02.2018 |  |
| Jimmy   |                  4 |                15.03.2018 |  |
| Jimmy   |                  5 |                15.04.2018 |  |
| Jimmy   |                  6 |                15.04.2018 |  |
| Harry   |                  1 |                08.01.2018 |  |
| Harry   |                  2 |                11.01.2018 |  |
| Harry   |                  3 |                15.02.2018 |  |
| Michael |                  1 |                15.06.2018 |  |
| Michael |                  2 |                15.07.2018 |  |
| Dwayne  |                  4 |                15.01.2018 |  |
| Dwayne  |                  5 |                15.01.2018 |  |
| Dwayne  |                  6 |                15.01.2018 |  |
+---------+--------------------+---------------------------+--+

You could use analytic counts to see how many characteristics each object has, and how many of the ones you are looking for; 您可以使用分析计数来查看每个对象有多少特征,以及您要查找的特征数量; and then compare those counts: 然后比较这些计数:

select obj, obj_characteristic, characteristic_date_added
from (
  select obj, obj_characteristic, characteristic_date_added,
    count(distinct obj_characteristic) over (partition by obj) as c1,
    count(distinct case when obj_characteristic in (1,2) then obj_characteristic end)
      over (partition by obj) as c2
  from your_table
)
where c1 = c2;

With your sample data that gives: 使用您的示例数据,可以:

OBJ     OBJ_CHARACTERISTIC CHARACTERI
------- ------------------ ----------
Mark                     1 2018-01-15
Mark                     2 2018-02-15
Michael                  1 2018-06-15
Michael                  2 2018-07-15

From the way the question is worded it sounds like you want the complete rows, as above; 从问题的方式来看,听起来你想要完整的行,如上所述; froma comment you may only want the names. 你可能只想要这些名字。 If so you can just change the outer select to: 如果是这样,您只需将外部选择更改为:

select distinct obj
from ...

OBJ    
-------
Mark
Michael

or use aggregates instead via a having clause: 或者通过having子句使用聚合:

select obj
from your_table
group by obj
having count(distinct obj_characteristic)
  = count(distinct case when obj_characteristic in (1,2) then obj_characteristic end);

OBJ    
-------
Mark
Michael

db<>fiddle demo of all three . db <>这三个小提琴演示


In this case, as 1 and 2 are contiguous, you could also do this with min/max, as an aggregate to just get the names: 在这种情况下,由于1和2是连续的,您也可以使用min / max作为聚合来获取名称:

select obj
from your_table
group by obj
having min (obj_characteristic) = 1
and max(obj_characteristic) = 2;

or analytically to get the complete rows: 或分析地获得完整的行:

select obj, obj_characteristic, characteristic_date_added
from (
  select obj, obj_characteristic, characteristic_date_added,
    min(obj_characteristic) over (partition by obj) as min_char,
    max(obj_characteristic) over (partition by obj) as max_char
  from your_table
)
where min_char = 1
and max_char = 2;

but the earlier versions are more generic. 但早期版本更通用。

If you are just looking for sql to return rows values '1,2' and nothing else use: 如果你只是寻找sql返回行值'1,2'而没有其他用途:

select * from table where column like '%1,2'

Post an example of the data, it may be more helpful to understand. 发布数据示例,可能更有帮助理解。

@dwin90 You could try: @ dwin90您可以尝试:

SELECT obj
FROM your_table
WHERE (OBJ_CHARACTERISTIC=1 OR OBJ_HARACTERISTIC=2 AND OBJ_CHARACTERISTIC !> 2
)GROUP BY OBJ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL选择行,其中列值是唯一的(仅出现一次) - SQL select rows, where column value is unique (only appears once) 如何 select 在 postgresql 中多次出现多列值的所有行? - How to select all rows where a multiple column value occurs more than once in postgresql? SQL 查询获取只出现一次的值 - SQL Query To Obtain Value that Occurs Only Once 选择行名称中一个值出现在多行中的行 - Select rows where one value in field name occured in multiple rows SQL:筛选出列值多次出现的行 - SQL: Filter out rows where column value occurs more than once SELECT 从两个表中获取结果,其中一个值仅在表列中出现一次 - SELECT To obtain results from two tables where a value occurs once only in a table column SQL查询选择多个行,其中&#39;field&#39;等于&#39;value&#39;,然后添加与结果关联的其他id - SQL query select multiple rows where 'field' equals to 'value' then add the other ids associated with the result SQL-如何从表中选择具有特定数据值和仅出现一次的键的行 - SQL - How to select a row from a table with a specific data value and a key that only occurs once 从SQL文件导入MySQL,但仅在field = value的行中导入 - Import into MySQL from SQL file, but only rows where field = value SQL查询仅在字段等于value时选择最高ID - SQL query to select highest ID only where field is equal to value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM