21

I've searched for an answer on this but can't find quite how to get this distinct recordset based on a condition. I have a table with the following sample data:

Type    Color   Location    Supplier
----    -----   --------    --------
Apple   Green   New York    ABC
Apple   Green   New York    XYZ
Apple   Green   Los Angeles ABC
Apple   Red     Chicago     ABC
Apple   Red     Chicago     XYZ
Apple   Red     Chicago     DEF
Banana  Yellow  Miami       ABC
Banana  Yellow  Miami       DEF
Banana  Yellow  Miami       XYZ
Banana  Yellow  Atlanta     ABC

I'd like to create a query that shows the count of unique locations for each distinct Type+Color where the number of unique locations is more than 1, e.g.

Type    Color   UniqueLocations
----    -----   --------
Apple   Green   2
Banana  Yellow  2

Note that {Apple, Red, 1} doesn't appear because there is only 1 location for red apples (Chicago). I think I've got this one (but perhaps there is a simpler method). I'm using:

SELECT Type, Color, Count(Location) FROM
(SELECT DISTINCT Type, Color, Location FROM MyTable)
GROUP BY Type, Color HAVING Count(Location)>1;

How can I create another query that lists the Type, Color, and Location for each distinct Type,Color when the count of unique locations for that Type,Color is greater than 1? The resulting recordset would look like:

Type    Color   Location
----    -----   --------
Apple   Green   New York
Apple   Green   Los Angeles
Banana  Yellow  Miami
Banana  Yellow  Atlanta

Note that Apple, Red, Chicago doesn't appear because there is only 1 location for red apples. Thanks!

4

2 回答 2

23

对子查询使用 a COUNT(DISTINCT Location)and joinTypeColor The GROUP BYandHAVING子句,因为您尝试使用它们将完成这项工作。

/* Be sure to use DISTINCT in the outer query to de-dup */
SELECT DISTINCT
   MyTable.Type,
   MyTable.Color,
   Location
FROM 
  MyTable
  INNER JOIN (
    /* Joined subquery returns type,color pairs having COUNT(DISTINCT Location) > 1 */
    SELECT
      Type,
      Color,
      /* Don't actually need to select this value - it could just be in the HAVING */
      COUNT(DISTINCT Location) AS UniqueLocations
    FROM
      MyTable
    GROUP BY Type, Color
    /* Note: Some RDBMS won't allow the alias here and you 
       would have to use the expanded form
       HAVING COUNT(DISTINCT Location) > 1
     */
    HAVING UniqueLocations > 1
  /* JOIN back against the main table on Type, Color */
  ) subq ON MyTable.Type = subq.Type AND MyTable.Color = subq.Color

这是一个演示

于 2012-11-06T20:02:22.957 回答
4

你可以这样写你的第一个查询:

Select Type, Color, Count(Distinct Location) As UniqueLocations
From Table
Group By Type, Color
Having Count(Distinct Location) > 1

(如果您使用 MySQL,您可以UniqueLocationshaving子句中使用别名,但在许多其他系统上,别名尚不可用,因为having子句在子句之前进行评估select,在这种情况下,您必须对两个子句重复计数)。

对于第二种,有很多不同的写法,这可能是一种:

Select Distinct Type, Color, Location
From Table
Where
  Exists (
    Select
      *
    From
      Table Table_1
    Where
      Table_1.Type = Table.Type
      and Table_1.Color = Table.Color
    Group By
      Type, Color
    Having
      Count(Distinct Location) > 1
  )
于 2012-11-06T20:05:57.340 回答