选择每组中至少有一行符合条件的行组

时间:2015-10-08 19:34:17

标签: sql oracle select

我正在尝试选择具有特定条件的一行的行组。

我用CASE WHEN语句尝试过但没有成功。请记住,此表有数百条记录。

我想要完成的是:

  • 该组的一行必须subcategory等于“GAMECONSOLE”。
  • 具有相同categorydescriptionsection的行形成一个组。

ID不同,因此MIN和MAX也不起作用。

ID      SECTION     DESCRIPTION   CATEGORY     SUBCATEGORY
21349   14010014    TODDLER       TOY          GAMECONSOLE
21278   14010014    TODDLER       TOY          BICYCLE 
21431   15020021    TODDLER       TOY          CHESS

在此示例中,应选择两个第一行,因为它们形成一个组,并且该组的一行是“GAMECONSOLE”。

6 个答案:

答案 0 :(得分:1)

当您必须在列表达式中做出决定时,使用

CASE WHEN。行级别的过滤必须在WHERE子句中完成:

SELECT T.id, T.section, T.description, T.category, T.subcategory
FROM
    myTable T
    INNER JOIN myTable S
        ON T.section = S.section AND 
           T.description = S.description AND
           T.category = S.category
WHERE
    S.subcategory = 'GAMECONSOLE'

您可以将表连接到必须相等的列上。别名S的表选择正确的子类别。 T选择所有相应的行组。

答案 1 :(得分:0)

SELECT a1.ID 
     , a1.SECTION 
     , a1.DESCRIPTION
     , a1.CATEGORY
     , a1.SUBCATEGORY 
  FROM  MyTable a1 
 INNER JOIN MyTable a2 ON a2.DESCRIPTION = a1.DESCRIPTION
                      AND a2.CATEGORY = a1.CATEGORY 
                 AND a2.SECTION = a1.SECTION 
   WHERE a2.SUBCATEGORY = 'GAMECONSOLE' 

- 您可能希望进一步过滤Where子句并应用或不同的组来获取您想要的实际结果

答案 2 :(得分:0)

您的说明听起来像是:

        response = HttpResponse()
        response['Content-Length'] = os.path.getsize(path)
        response['Content-Disposition'] = 'attachment; filename="{}"'.format(os.path.basename(path))
        response['X-Accel-Redirect'] = server_file_path

答案 3 :(得分:0)

 SELECT *
 FROM myTable T
 WHERE Section = (SELECT Section
                  FROM myTable Q
                  WHERE Q.subcategory = 'GAMECONSOLE')

答案 4 :(得分:0)

使用analytic function,您可以在不使用自我加入的情况下获得答案。

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE TEST( ID, SECTION, DESCRIPTION, CATEGORY, SUBCATEGORY ) AS
          SELECT 1, 1, 'TODDLER', 'TOY', 'GAMECONSOLE' FROM DUAL
UNION ALL SELECT 2, 1, 'TODDLER', 'TOY', 'BICYCLE' FROM DUAL
UNION ALL SELECT 3, 2, 'TODDLER', 'TOY', 'CHESS' FROM DUAL
UNION ALL SELECT 4, 3, 'COMPUTERS', 'SOFTWARE', 'BOOK' FROM DUAL
UNION ALL SELECT 5, 4, 'COMPUTERS', 'SOFTWARE', 'SOFTWARE' FROM DUAL
UNION ALL SELECT 6, 5, 'COMPUTERS', 'HARDWARE', 'MONITOR' FROM DUAL
UNION ALL SELECT 7, 6, 'COMPUTERS', 'HARDWARE', 'GAMECONSOLE' FROM DUAL
UNION ALL SELECT 8, 7, 'COMPUTERS', 'HARDWARE', 'KEYBOARD' FROM DUAL
UNION ALL SELECT 9, 8, 'TODDLER', 'BEDDING', 'BED' FROM DUAL

查询1

SELECT ID, SECTION, DESCRIPTION, CATEGORY, SUBCATEGORY
FROM (
  SELECT t.*,
         COUNT( CASE SUBCATEGORY WHEN 'GAMECONSOLE' THEN 1 END ) OVER ( PARTITION BY DESCRIPTION, CATEGORY ) AS HAS_SUBCATEGORY
  FROM   TEST t
)
WHERE  HAS_SUBCATEGORY > 0

<强> Results

| ID | SECTION | DESCRIPTION | CATEGORY | SUBCATEGORY |
|----|---------|-------------|----------|-------------|
|  8 |       7 |   COMPUTERS | HARDWARE |    KEYBOARD |
|  7 |       6 |   COMPUTERS | HARDWARE | GAMECONSOLE |
|  6 |       5 |   COMPUTERS | HARDWARE |     MONITOR |
|  3 |       2 |     TODDLER |      TOY |       CHESS |
|  2 |       1 |     TODDLER |      TOY |     BICYCLE |
|  1 |       1 |     TODDLER |      TOY | GAMECONSOLE |

答案 5 :(得分:-2)

尝试

SELECT * FROM <TABLE_NAME> WHERE SUBCATEGORY like "GAMECONSOLE";

SELECT * FROM <TABLE_NAME> WHERE SUBCATEGORY = "GAMECONSOLE";

替换&lt; TABLE_NAME&gt;用实际的表名。

进一步阅读: