|
我一直在寻找答案,但是找不到完全根据条件获取此不同记录集的方法。我有一个包含以下示例数据的表:8 v2 `6 M, ~. O) ]% V' S' A
Type Color Location Supplier( R0 i$ C- `8 e' d
---- ----- -------- --------3 h8 a- u; ~' Z) l% g
Apple Green New York ABC/ j6 }' b$ C- S: u2 W; ?( w
Apple Green New York XYZ* V* _+ i6 {9 f3 v0 Q) f
Apple Green Los Angeles ABC
+ y2 }6 z$ u# h+ l5 n; _Apple Red Chicago ABC
$ V' i2 D4 y, M" tApple Red Chicago XYZ
* ~ v! b; M8 i7 EApple Red Chicago DEF
, g/ p& j) O% a4 W* r8 a0 y& }5 |Banana Yellow Miami ABC
/ p; l4 H1 v4 k2 a; w y m1 D- |Banana Yellow Miami DEF$ k/ \: E0 M; ?8 p3 `. M6 q% q
Banana Yellow Miami XYZ& K' y; @7 S7 B0 i3 `$ ^3 ^- i
Banana Yellow Atlanta ABC3 u; [% b* j2 u; K: @3 \3 p
我想创建一个查询,以显示唯一位置数量大于1的每种不同Type + Color的唯一位置的计数,例如/ D( I* H8 H$ t3 ` O2 I
Type Color UniqueLocations
7 o" [1 H9 ]2 q2 Q$ k# y( D) E---- ----- --------, [; q) h# q7 l/ ~7 p" | [) e9 X D
Apple Green 2. V" z2 j& X9 J, d( R5 ^8 D
Banana Yellow 2
0 w0 N. X& Z& L3 J/ h请注意,{Apple,Red,1}不会出现,因为红苹果(Chicago)只有1个位置。我想我已经有了这个(但是也许有一个更简单的方法)。我正在使用: B. f& U, y5 L) U' u, b
SELECT Type, Color, Count(Location) FROM
L& T( I$ T5 j(SELECT DISTINCT Type, Color, Location FROM MyTable)1 g6 p* N7 g' t7 y# N
GROUP BY Type, Color HAVING Count(Location)>1;. Q6 @" h" `8 f9 T5 @! C/ C1 m z/ ^
当唯一查询的唯一位置计数大于1时Type,
% W- [0 U7 K; `' H8 ~Color,如何创建另一个查询,该查询列出,并且Location针对每个不同查询?结果记录集如下所示:Type,Color``Type,Color& j& O# A) n5 O* b: V, R
Type Color Location& R/ O0 E: p: `, U" x8 [% E' G
---- ----- --------
6 v6 o& F/ Z6 h+ ?4 t7 I/ ~8 h( FApple Green New York- H$ h' b* `. e
Apple Green Los Angeles
/ u( {8 o1 H8 a3 n1 X; [2 r8 H+ W- iBanana Yellow Miami
5 F" y8 Y5 l( w8 ^7 hBanana Yellow Atlanta
% K! w4 z5 K* V$ O2 I请注意,Apple, Red, Chicago由于红苹果只有1个位置,因此未出现。谢谢!+ `; s2 N( B6 K' @7 g4 g
/ v+ M! M4 q4 Z# v( w解决方案: d( y, V0 z+ u5 F, A
8 s. @! p9 B. a; _# J1 q# z; [8 Z/ G
0 E! b' [+ E+ ~
$ k4 A6 K! h. a/ m. Q& G 使用COUNT(DISTINCT Location)并加入针对一个子查询上Type,并Color 在GROUP6 o2 W8 }2 R4 T' O
BY和HAVING条款,你试图使用他们将做的工作。
( d9 t/ G2 u* x/* Be sure to use DISTINCT in the outer query to de-dup */
2 d% h6 w0 D( c4 A4 |# MSELECT DISTINCT
8 i8 u8 O6 u1 J' P! R# d, D MyTable.Type,
" x& h# F/ ~8 @! Q# R2 b MyTable.Color,0 o* X* V, p- f( p6 ?+ C1 `! H
Location
l& C7 m, F* eFROM
, Y1 Z/ `1 o- k3 W- B& L6 |/ K MyTable, B2 g# U" `6 u ]
INNER JOIN (* k) }8 y( D1 A' x6 ~* ?* F
/* Joined subquery returns type,color pairs having COUNT(DISTINCT Location) > 1 */5 }; s; l5 E# X) D. e5 t: y
SELECT
6 w" B, y: I" X$ V) M Type,2 C7 s) E+ B( j4 I" U
Color,
! y( v" z8 _" M& U: y+ j' W /* Don't actually need to select this value - it could just be in the HAVING */+ J v2 b7 ]3 r8 O) | Z. d; L! y
COUNT(DISTINCT Location) AS UniqueLocations
1 e8 ^/ D7 P. C FROM
* W0 |: V! c# Z# w7 A( S& C$ u MyTable! h/ v: |3 J7 Q5 R- T
GROUP BY Type, Color
7 B& R4 v& G$ c5 R( I1 ~; _0 A8 I, Q. j1 o' L /* Note: Some RDBMS won't allow the alias here and you
( n5 R0 I, f# T1 | would have to use the expanded form; |5 U) G9 S" T9 ~3 m, h( f
HAVING COUNT(DISTINCT Location) > 10 v, ], ]9 \$ U7 | A2 ?, d9 f/ T" h
*/- ^8 p2 w& L# p8 {( ]
HAVING UniqueLocations > 1
% f& S/ E4 J2 C8 S$ p3 X" S2 Z3 ^ /* JOIN back against the main table on Type, Color */: o6 A0 W! g8 ]4 L6 ~6 w$ [
) subq ON MyTable.Type = subq.Type AND MyTable.Color = subq.Color- Z8 _) g$ L$ W+ M* A$ T
这是一个示范 |
|