存储过程计数在计数中具有id号的行数

时间:2017-12-13 04:32:36

标签: sql sql-server

我想用ID来计算索赔的总数。

SELECT   
    InsuranceId,Name,
    COUNT(claimid) AS TotalPendingClaims
FROM 
    Claim 
GROUP BY
    InsuranceId

这是我的查询,但我想获得claimid也会在上面的查询中计算出来。我需要专栏claimIds=(1,2,4,5)

预期结果:

InsuranceId  Name TotalPendingClaims  ClaimIds
-----------------------------------------------
1            Med     2                  23,24
2            TX      1                   55
3            TED     3                  44,45,46

4 个答案:

答案 0 :(得分:1)

你可以试试这个

.flex.flex-column.w50p.mx-auto
  = form_for :agreegated_answer, url: survey_aggregated_answers_path(@survey), method: :post  do |f|
    = f.label :procedures_that_have_tags
    = f.collection_select(:tag_ids, @survey.tags, :name, :name, { selected: [] },
      multiple: true, class: 'js-input-tags border-light', placeholder: 'Select tags')
    = f.label :answered_from
    = f.date_field :start_from
    = f.label :answered_to
    = f.date_field :ends_from
    = f.submit 'Generate results'

答案 1 :(得分:0)

您可以使用以下查询,其中的内容连接索赔ID,并且可以单独计算数量

SELECT DISTINCT t2.InsuranceId ,
    STUFF((Select ','+ CONVERT(VARCHAR(5), claimid) FROM #TEMP_INSURANCE T1 
WHERE T1.InsuranceId=T2.InsuranceId FOR XML PATH('')),1,1,'')  AS ClaimID,
    Cnt
FROM #TEMP_INSURANCE t2 INNER JOIN
    (SELECT InsuranceId,COUNT(claimid) Cnt FROM #TEMP_INSURANCE GROUP BY InsuranceId)T ON t.InsuranceId = t2.InsuranceId

答案 2 :(得分:0)

我的问题的答案是:

--changes addded by Ali Imran 12/13/2017 get also the claim number

claimid=stuff
(
 ( select distinct','+cast(c1.claimid as varchar(max))
  from #Claim c1 where
  c1.insuranceid=ins.insuranceid

  for xml path('')
  ),1,1,''
),

--Changes ended by Ali Imran

答案 3 :(得分:0)

尝试此查询

SELECT InsuranceId, name, COUNT(*) TotalPendingClaims, 
ClaimIds = STUFF((SELECT ', ' + CONVERT(varchar, claimid)
           FROM  dbo.Claim c2
           WHERE c1.InsuranceId = c2.InsuranceId AND c1.name = c2.name
           FOR XML PATH('')), 1, 2, '')
FROM claim c1
GROUP BY InsuranceId, name
ORDER BY InsuranceId, name