0

我的数据库中有这些数据,它显示了有多少用户被邀请和参与,我想计算以下内容:

1- 邀请总数(总数)
2- 参与(允许的总和,to_be_paid, already_paid)
3- 已响应(允许、to_be_paid、 already_paid、user_declined、user_willing 的总和)
4- 无响应(过期,提供的总和)
5-响应率(响应/总数*100)
6- 参与率(参与/总*100)

表视图

我希望能够得到这样的结果:

最后结果

4

1 回答 1

0

我认为这可以帮助你。我改成这个并测试它是正确的

  declare @Responded int
  declare @Total int
  declare @Participated int

  set @Total=(select SUM([count]) as Total from [dbo].[tb1])

  set @Participated=(select SUM([count]) as Participated  from [dbo].[tb1]
  where Status in('allowed', 'to_be_paid', 'already_paid'))

  set @Responded=( select SUM([count]) as Responded   from [dbo].[tb1]
  where Status in('allowed', 'to_be_paid', 'already_paid', 'user_declined', 'user_willing'))
(SELECT  [Status]
      ,cast([count] as DECIMAL(38,0))
  FROM [Identity].[dbo].[tb1]
  )
  union 
  (
  select 'Total',cast(@Total as DECIMAL(38,0))
  )
  union 
  (
  select 'Participated',cast(@Participated as DECIMAL(38,0))
  )
  union 
  (
  select 'Responded',cast(@Responded as DECIMAL(38,0))
  )
  union 
  (
  select 'No response',cast(SUM([count]) as DECIMAL(38,0)) as [count] from [dbo].[tb1]
  where Status in('expired', 'offered')
  )
  union(
  select 'Response rate', cast(((100.0 * @Responded)/ @Total)as DECIMAL(38,0)) as [count]
  )
  union(
  select 'Participation rate',cast(((100.0 * @Participated)/ @Total)as DECIMAL(38,0))as [count]
  )

结果

于 2017-10-22T09:46:41.793 回答