0

示例 1

+--------------------------+
| IDENT | CURRENT | SOURCE |
+--------------------------+
| 12345 | 12345   | A      |
| 23456 | 12345   | B      |
| 34567 | 12345   | C      |
+--------------------------+

示例 2

+--------------------------+
| IDENT | CURRENT | SOURCE |
+--------------------------+
| 12345 | 55555   | A      |
| 23456 | 55555   | B      |
+--------------------------+

尝试编写将显示 CURRENT count = 2 且 SOURCE 同时包含 A 和 B(NOT C)的所有记录的选择查询。

示例 A 不应显示,因为 CURRENT 有 3 个条目,因为记录链接到 SOURCE C。
示例 B 是我正在查找要查找的查询,CURRENT 有两条记录,并且仅链接到 SOURCE 'A' 和 '乙'。

目前,如果我运行类似于“其中 SOURCE = A 或 SOURCE = B”的内容,则结果是只有 A 或 A+C 的源的记录。注意: IDENT 始终是唯一值。CURRENT 链接来自不同 SOURCE 的多个 IDENT。

4

5 回答 5

1

我们显然缺少更多信息。让我们以示例数据为例(感谢忧郁的最初小提琴)。

| ID | CURRENT | SOURCE |
|----|---------|--------|
|  1 |     111 |      A |
|  2 |     111 |      B |
|  3 |     111 |      C |
|  4 |     222 |      A |
|  5 |     222 |      B |
|  6 |     333 |      A |
|  7 |     333 |      C |
|  8 |     444 |      B |
|  9 |     444 |      C |
| 10 |     555 |      B |
| 11 |     666 |      A |
| 12 |     666 |      A |
| 13 |     666 |      B |
| 14 |     777 |      A |
| 15 |     777 |      A |

我假设你只需要这个作为结果:

| ID | CURRENT | SOURCE |
|----|---------|--------|
|  4 |     222 |      A |
|  5 |     222 |      B |

此查询将使用任意数量的源并产生预期的输出:

SELECT * FROM test
WHERE CURRENT IN (
  SELECT CURRENT FROM test
  WHERE CURRENT NOT IN (
    SELECT CURRENT FROM test
    WHERE SOURCE NOT IN ('A', 'B')
  )
  GROUP BY CURRENT
  HAVING count(SOURCE) = 2 AND count(DISTINCT SOURCE) = 2
)
于 2013-10-15T18:47:08.247 回答
0

http://sqlfiddle.com/#!2/69be9/8/0

select * from test where current in (

    select test_a.current 

    from 

       (select * 
        from   test 
        where  source = 'A') as test_a

       join (select * 
             from   test 
             where  source = 'B') as test_b
          on test_b.current = test_a.current

    where test_a.current not in 
            (select current from test where source='C')
)
于 2013-10-15T18:34:10.497 回答
0

当前计数 = 2 的记录:

SELECT CURRENT
FROM table
GROUP BY CURRENT
HAVING COUNT(*) = 2

记录 C 在 SOURCE 值中的位置:

SELECT CURRENT
FROM table
WHERE SOURCE = 'C'

全局查询:

SELECT t.*
FROM TABLE t
WHERE t.CURRENT IN (
    SELECT CURRENT
    FROM table
    GROUP BY CURRENT
    HAVING COUNT(*) = 2
) AND t.CURRENT NOT IN (
    SELECT CURRENT
    FROM table
    WHERE SOURCE = 'C'
)
于 2013-10-15T18:28:24.607 回答
0
  1. 如果SOURCE保证每个值是唯一的CURRENT

    SELECT CURRENT
    FROM atable
    GROUP BY CURRENT
    HAVING COUNT(SOURCE) = 2
       AND COUNT(CASE WHEN SOURCE IN ('A', 'B') THEN SOURCE END) = 2
    ;
    
  2. 如果SOURCE值不是唯一的 perCURRENT但s 具有orCURRENT的重复条目是允许的:'A''B'

    SELECT CURRENT
    FROM atable
    GROUP BY CURRENT
    HAVING COUNT(DISTINCT SOURCE) = 2
       AND COUNT(DISTINCT CASE WHEN SOURCE IN ('A', 'B') THEN SOURCE END) = 2
    ;
    
  3. 如果SOURCE值不是唯一的并且SOURCE不允许具有重复条目的组:

    SELECT CURRENT
    FROM atable
    GROUP BY CURRENT
    HAVING COUNT(SOURCE) = 2
       AND COUNT(DISTINCT SOURCE) = 2
       AND COUNT(DISTINCT CASE WHEN SOURCE IN ('A', 'B') THEN SOURCE END) = 2
    ;
    

每个查询只返回CURRENT符合要求的不同值。将查询用作派生数据集并将其连接回您的表以获取详细信息。

以上所有选项都假设要么SOURCENOT NULL列,要么可以忽略 NULL。

于 2013-10-17T01:58:16.427 回答
0
SELECT *
FROM TABLE mainTbl,
   (SELECT CURRENT
   FROM TABLE
   WHERE source IN ('A', 'B')
   HAVING COUNT(1) = 2
   GROUP BY CURRENT
   ) selectedSet
WHERE mainTbl.current = selectedSet.current
AND mainTbl.source IN ('A', 'B');
于 2013-10-15T18:44:49.957 回答