Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Showing Top 3 within a multiple count query

$
0
0

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.IDENT
Adding Extra1,Extra1b,Extra1C as samples for each combination of Run/Error_Nbr/Ident
error_txtCnt_Extra1 CNT_KEY1runERROR_NBRIDENTExtra1Extra1bExtra1c
Value Not Built16662200ENR002002123456789
Value Not Built561643200ENR002002224321555
ID Does Not Exist Xref146352200ENR003001410732525
ID Does Not Exist Xref2279573200ENR00300113232565865654

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>