Within a query that contains multiple counts based on a combination of data elements, is there a way to display in the results the top 3 records for each count? Desired Output and Current Query text below
SELECT distinct msg.record ,msg.field ,msg.logmsg ,look.error_txt ,COUNT(DISTINCT msg.extra1) AS "Cnt_Extra1" ,COUNT(DISTINCT MSG.KEY1) as "CNT_KEY1" ,msg.run ,look.ERROR_NBR ,look.IDENT FROM dbo msg ,dbo.look WHERE msg.KEY2=look.IDENT and msg.LOGMSG=look.error_nbr and msg.module = 'ENR' and LOOK.IDENT like 'enr%' and msg.RUN_DT>'2014-03-18 09:56:38.000' GROUP BY msg.record ,msg.field ,msg.logmsg ,look.error_txt ,msg.run ,look.ERROR_NBR ,look.IDENTAdding Extra1,Extra1b,Extra1C as samples for each combination of Run/Error_Nbr/Ident
error_txt | Cnt_Extra1 | CNT_KEY1 | run | ERROR_NBR | IDENT | Extra1 | Extra1b | Extra1c |
Value Not Built | 16 | 66 | 2 | 200 | ENR002002 | 123 | 456 | 789 |
Value Not Built | 56 | 164 | 3 | 200 | ENR002002 | 224 | 321 | 555 |
ID Does Not Exist Xref | 1463 | 5 | 2 | 200 | ENR003001 | 410 | 732 | 525 |
ID Does Not Exist Xref | 22795 | 7 | 3 | 200 | ENR003001 | 13232 | 5658 | 65654 |