2014-05-19 67 views
0

,所以我有這個疑問的作品好,做一些數學與獨特的參數SQL查詢,其中statment

我想改變的是,第三列

的唯一的值將被納入賬戶

這是可能的嗎?

謝謝大家!

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

這是我不清楚你想達到的目標。請您能提供一些示例數據以及您希望得到的結果。 – MatBailie

回答

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 
) 
+0

這IAM越來越 附近有語法錯誤)「 - >最後 ')' – user1310492

+0

把任何名稱後括號最後讓我們說ORDER BY fail_rate DESC)爲XYZ –

+0

同樣的錯誤了 )ORDER BY fail_rate遞減 – user1310492

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