0

我有一个查询,它给了我以下输出:

select 
                PD.ProductId, TotalCalls = COUNT(DISTINCT PD.LogId), 
                TrueCalls = COUNT(DISTINCT case when PD.ExceptionCode  = ' ' then PD.LogId  END),
                ErrorCalls =COUNT(DISTINCT case when PD.ExceptionCode  != ' ' then PD.LogId  END),
                PassPercentage = CONVERT(DECIMAL(10,1),100 - (CAST(COUNT(DISTINCT case when PD.ExceptionCode  != ' ' then PD.LogId  END) as float)/CAST(COUNT(PD.LogId) as float)*100))
         from 
                Log P 
                INNER JOIN LogProduct PD ON P.LogId = PD.LogId


        WHERE   
                (ResponseTime < '2013-09-28' and  RequestTime > '2013-09-01')

    Group By 
                PD.ProductId

它给了我以下输出:

ProductId   TotalCalls  TrueCalls   ErrorCalls  PassPercentage
1   6   6   0   100.0
2   1   0   1   85.7
3   33  15  18  92.2

现在我有另一个表:

级别:

LevelId Min Max Bool    ProductId
1   100 100 0   2
2   80  99  0   2
3   60  79  0   2
4   40  59  0   2
5   1   39  1   2
6   0   0   0   2
7   -1  -1  0   2
1   100 100 0   1
2   80  99  0   1
3   60  79  1   1
4   40  59  0   1
5   1   39  0   1
6   0   0   0   1
7   -1  -1  0   1

我想做的是比较第一个查询的输出并添加一个新的 LevelId 列:

例子 :

我正在寻找这样的输出:

ProductId   TotalCalls  TrueCalls   ErrorCalls  PassPercentage  LevelId
1   6   6   0   100.0       1
2   1   0   1   85.7         2

这里的逻辑是:我想比较该特定产品每一行的 PassPercentage 并找出它属于哪个级别。

在上面的示例中: 产品 2 的 PassPercentage 为 85.7。如果您检查上面的级别表中 ProductId 2 ,级别 2 应选择为 80 < 87.5 < 99

我无法弄清楚我该怎么做..请让我知道我如何从这里继续前进......或者给我我应该做什么的想法??

4

1 回答 1

1

查询看起来像

with stats as (
select 
                PD.ProductId, TotalCalls = COUNT(DISTINCT PD.LogId), 
                TrueCalls = COUNT(DISTINCT case when PD.ExceptionCode  = ' ' then PD.LogId  END),
                ErrorCalls =COUNT(DISTINCT case when PD.ExceptionCode  != ' ' then PD.LogId  END),
                PassPercentage = CONVERT(DECIMAL(10,1),100 - (CAST(COUNT(DISTINCT case when PD.ExceptionCode  != ' ' then PD.LogId  END) as float)/CAST(COUNT(PD.LogId) as float)*100))
         from 
                Log P 
                INNER JOIN LogProduct PD ON P.LogId = PD.LogId


        WHERE   
                (ResponseTime < '2013-09-28' and  RequestTime > '2013-09-01')

    Group By 
                PD.ProductId
)
select s.*, l.LevelId
  from stats s
  join levels l on l.ProductId = s.ProductId and s.PassPercentage between l.Min and l.Max
于 2013-09-18T22:52:01.750 回答