SQL查询以选择我的第一列常见的记录

时间:2014-01-04 05:13:46

标签: sql sql-server

    SEG_AIRLINE SEG_ORIGIN_CODE SEG_DESTINATION_CODE
    G9              ALA             DEL                         
    G9              ALA             DAC             
    G9              BKK             OOL                 
    SG              IXA             MAA         
    SG              BKK             OOL     
    SG              IXA             GAU         
    6E              IXA             AMD
    6E              BKK             OOL
    6E              IXA             DEL
    AK              BKK             MEL
    AK              BKK             OOL
    AK              BKK             PER

我有一张如上所述的表格。现在我想选择SEG_ORIGIN_CODE和SEG_DESTINATION_CODE 这对所有SEG_AIRLINE都是通用的。

我想输出

    SEG_AIRLINE SEG_ORIGIN_CODE SEG_DESTINATION_CODE                                
    G9              BKK             OOL                         
    SG              BKK             OOL                 
    6E              BKK             OOL
    AK              BKK             OOL

我尝试过这个查询

    SELECT SEG_AIRLINE ,SEG_ORIGIN_CODE ,SEG_DESTINATION_CODE 
    FROM T_N_SEGMENTS
    GROUP BY SEG_AIRLINE
    HAVING COUNT(DISTINCT SEG_ORIGIN_CODE) = (SELECT COUNT(DISTINCT SEG_ORIGIN_CODE) FROM T_N_SEGMENTS)

我试过的另一个解决方案

    declare @airlineCount int
    select @airlineCount = count(distinct seg_airline) from T_N_SEGMENTS

    select seg_airline, seg_origin_code, seg_destination_code from T_N_SEGMENTS
    where
    (Select count(distinct seg_airline) from T_N_SEGMENTS s where s.seg_origin_code = T_N_SEGMENTS.seg_origin_code) = @airlineCount
    and
    (Select count(distinct seg_airline) from T_N_SEGMENTS s where s.seg_destination_code = T_N_SEGMENTS.seg_destination_code) = @airlineCount

但我不会返回任何记录

1 个答案:

答案 0 :(得分:3)

根据您的评论

更新。这会为您提供最常见的 seg_origin_code, seg_destination_code

的行
SELECT seg_airline, seg_origin_code, seg_destination_code
  FROM
(
  SELECT seg_airline, seg_origin_code, seg_destination_code, 
         RANK() OVER (ORDER BY cnt DESC) rank
    FROM
  (
    SELECT seg_airline, seg_origin_code, seg_destination_code,
           COUNT(*) OVER (PARTITION BY seg_origin_code, seg_destination_code) cnt
      FROM t_n_segments
  ) q
) p
 WHERE rank = 1

这是 SQLFiddle 演示


你可以这样做

SELECT s.seg_airline, s.seg_origin_code, s.seg_destination_code
  FROM
(
  SELECT seg_origin_code, seg_destination_code 
    FROM t_n_segments
   GROUP BY seg_origin_code,seg_destination_code
  HAVING COUNT(DISTINCT seg_airline) = 
  (
    SELECT COUNT(DISTINCT seg_airline)
      FROM t_n_segments
  )
) q JOIN t_n_segments s
    ON q.seg_origin_code = s.seg_origin_code
   AND q.seg_destination_code = s.seg_destination_code

或使用窗口函数

SELECT seg_airline, seg_origin_code, seg_destination_code
  FROM
(
  SELECT seg_airline, seg_origin_code, seg_destination_code,
         COUNT(*) OVER (PARTITION BY seg_origin_code, seg_destination_code) cnt
    FROM t_n_segments
) q
 WHERE cnt = 
(
  SELECT COUNT(DISTINCT seg_airline)
    FROM t_n_segments
)

输出:

| SEG_AIRLINE | SEG_ORIGIN_CODE | SEG_DESTINATION_CODE |
|-------------|-----------------|----------------------|
|          G9 |             BKK |                  OOL |
|          SG |             BKK |                  OOL |
|          6E |             BKK |                  OOL |
|          AK |             BKK |                  OOL |

这是 SQLFiddle 演示