sql查询在where语句中使用唯一参数

时间:2014-05-19 12:30:27

标签: sql-server sql-server-2008-r2

所以我的查询效果很好并且做了一些数学

我想要改变的是只有第三列的唯一值

将被考虑在内

这可能吗?

谢谢大家!

SELECT t1.[customer_id] as customer_id,t2.total AS n_results ,                                                                                                                                               
         FORMAT((t2.total/t1.total)*100, 'N2')   AS fail_rate                                                                                                        
            FROM (SELECT [customer_id],                                                                                                                                      
                     1.0*COUNT( * ) AS Total                                                                                                                             
                FROM   [customer_transactions]                                                                                                               
              WHERE  [customer_id] != '' AND [customer_id] NOT LIKE 'D_%'                                                                        
              GROUP BY                                                                                                                                               
                       [customer_id]                                                                                                                                         
              ) t1 JOIN (                                                                                                                                            
              SELECT [customer_id],                                                                                                                                          
                     1*COUNT( * ) AS Total                                                                                                                           
                FROM [customer_transactions]  where  [result]='fail'             
               GROUP BY                                                                                                                                                  
                     [customer_id]                                                                                                                                       
              ) t2                                                                                                                                                   
                        ON t1.[customer_id]=t2.[customer_id] ORDER BY  fail_rate desc

2 个答案:

答案 0 :(得分:0)

您可以使用以下查询

Select Distinct fail_rate From
(
SELECT t1.[customer_id] as customer_id,t2.total AS n_results ,
         FORMAT((t2.total/t1.total)*100, 'N2')   AS fail_rate
            FROM (SELECT [customer_id],
                     1.0*COUNT( * ) AS Total
                FROM   [customer_transactions]
              WHERE  [customer_id] != '' AND [customer_id] NOT LIKE 'D_%'
              GROUP BY
                       [customer_id]
              ) t1 JOIN (
              SELECT [customer_id],
                     1*COUNT( * ) AS Total
                FROM [customer_transactions]  where  [result]='fail'
               GROUP BY
                     [customer_id]
              ) t2 
               ON t1.[customer_id]=t2.[customer_id] ORDER BY  fail_rate desc
)

答案 1 :(得分:0)

Select Distinct fail_rate From
(
SELECT t1.[customer_id] as customer_id,t2.total AS n_results ,
         FORMAT((t2.total/t1.total)*100, 'N2')   AS fail_rate
            FROM (SELECT [customer_id],
                     1.0*COUNT( * ) AS Total
                FROM   [customer_transactions]
              WHERE  [customer_id] != '' AND [customer_id] NOT LIKE 'D_%'
              GROUP BY
                       [customer_id]
              ) t1 JOIN (
              SELECT [customer_id],
                     1*COUNT( * ) AS Total
                FROM [customer_transactions]  where  [result]='fail'
               GROUP BY
                     [customer_id]
              ) t2 
               ON t1.[customer_id]=t2.[customer_id] ORDER BY  fail_rate desc
)
AS test