写一个获得糟糕的Mastered记录和正确的Mastered记录的查询?

时间:2014-12-08 16:58:41

标签: sql sql-server oracle

  1. 我必须编写一个查询,其中包含所有正确掌握的收件人(按first_name和last_name分组)
  2. 我必须编写另一个包含所有收件人都不好的查询(按first_name,last_name分组)
  3. 请查看下面的图片,如果有多个Master ID对抗名字和姓氏,那么它的Mastered很差..如果它有相同的Master ID,那么它正确掌握。
  4. 查询的示例数据在下面提供
  5. enter image description here enter image description here

    WITH DATA1 AS
    (
      SELECT 5175133 ID,'Yun'  FIRST_NAME,'Yue' LAST_NAME,NULL MASTER_ID FROM dual UNION ALL
      SELECT  5157093,'Yun','Yue',5157093 FROM dual UNION ALL
      SELECT  5226656,'Yun','Yue',NULL FROM dual UNION ALL
      SELECT  6345852,'Yun','Yue',5157093 FROM dual UNION ALL
      SELECT  5882603,'Ye','Han',5157093 FROM dual UNION ALL
      SELECT  5902219,'Ye','Han',5157093 FROM dual UNION ALL
    
      SELECT  6362890,'Rick','Kaylor',NULL FROM dual UNION ALL
      SELECT  6362940,'Rick','Kaylor',NULL FROM dual UNION ALL
      SELECT  5215659,'Rick','Kaylor',NULL FROM dual UNION ALL
      SELECT  5962837,'Rick','Kaylor',5962837 FROM dual UNION ALL
      SELECT  5841556,'Rick','Kaylor',5841556 FROM dual UNION ALL
    
      SELECT  5916218,'Sherlene','Heard',5916218 FROM dual UNION ALL
      SELECT  6356086,'Sherlene','Heard',5916218 FROM dual UNION ALL
    
      SELECT  5885157,'Ye','Kong',5884937 FROM dual UNION ALL
      SELECT  5884937,'Ye','Kong',NULL FROM dual UNION ALL
      SELECT  5898890,'Ye','Kong',5884937 FROM dual 
     )
    
     SELECT * FROM DATA1
    

    我认为它的简单查询请提供帮助? 感谢

1 个答案:

答案 0 :(得分:1)

因为这很可能是某种家庭作业或作业,只是一个线索:

您是否考虑过在子查询中使用COUNT(*)?据我所知,“正确掌握的收件人”将只有一个master_id ......