SQL:如何避免重复值

时间:2014-04-11 13:03:44

标签: sql

这是我的疑问:

SELECT DISTINCT 
    ca.id_number,
    CONVERT(VARCHAR(19),MAX(ca.creation_time),120) as creation_time,
    CONVERT(VARCHAR(19),MAX(cc.close_date),120) as close_date,
    cc.summary
FROM dbo.table_case ca
    INNER JOIN table_case_display cd on cd.id_number = ca.id_number
    INNER JOIN table_act_entry acte on ca.objid = acte.act_entry2case
    INNER JOIN table_gbst_elm elm on elm.objid = acte.entry_name2gbst_elm
    LEFT JOIN table_close_case cc on acte.objid = cc.close_case2act_entry 
WHERE 
    "cd"."creation_time">={ts '2004-09-30 19:58:21'} 
    AND  "ca"."id_number"<>'001' 
    AND ( "cd"."severity"='A' 
    OR "cd"."severity"='B' 
    OR "cd"."severity"='C' 
    OR "cd"."severity"='E' 
    OR "cd"."severity"='TBD') 
    AND "cd"."site_name"='Nextel International (Hosting)'
    AND (ca.creation_time>={ts '2014-03-01 00:00:00'} AND ca.creation_time<{ts '2014-04-10 00:00:01'})
GROUP BY ca.creation_time, cc.close_date, ca.id_number, cc.summary
ORDER BY ca.id_number asc

这是输出:

C00001  2014-03-10 20:35:17 NULL                    NULL
C00002  2014-03-11 03:16:01 NULL                    NULL
C00002  2014-03-11 03:16:01 2014-03-11 18:10:41     test
C00003  2014-03-07 02:53:22 NULL                    NULL
C00003  2014-03-07 02:53:22 2014-03-12 18:41:59     test 

从上面的输出中,它显示了重复的ID。 如何编写查询以避免此问题?

1 个答案:

答案 0 :(得分:0)

更改

GROUP BY ca.creation_time, cc.close_date, ca.id_number, cc.summary

GROUP BY  ca.id_number, cc.summary