2013-04-02 260 views
0

我有這樣顯示結果的查詢

SELECT TOP 100 PERCENT 
    COUNT(kodeall) AS Total, kodeall, kode, LEFT(kodeall, 1) AS kode1 
FROM 
    dbo.data 
WHERE 
    date BETWEEN '2013/03/01 00:00:00' AND '2013/03/01 23:59:00' 
    AND (kodeall IS NOT NULL) 
GROUP BY 
    kodeall, kode 
ORDER BY 
    kode1 

2個SQL查詢和結果

Total l kodeall l kode l kode1 
2  1a   AA  1  
5  1d   BB  1   
2  2a   CC  2   
6  2d   DD  2   
1  2e   EE  2   
3  3a   FF  3   
2  3c   GG  3   
1  3f   WW  3  

查詢#2:

SELECT  
    COUNT(kodeall) AS Sum, LEFT(kodeall, 1) AS kode1 
From 
    dbo.data 
WHERE 
    date BETWEEN '2013/03/01 00:00:00' 
    AND '2013/03/01 23:59:00' 
    AND (kodeall IS NOT NULL) 
GROUP BY 
    LEFT(kodeall, 1) 

和結果

Sum l kode1 l 
7  1  
9  2  
6  3  

和,如果我想顯示的結果這樣

Total l kodeall l kode l kode1 l sum l Percentage 
2  1a   AA  1  7  28.57 % 
5  1d   BB  1  7  71.43 % 
2  2a   CC  2  9  22.22 % 
6  2d   DD  2  9  66.66 % 
1  2e   EE  2  9  11.11 % 
3  3a   FF  3  6  50 % 
2  3c   GG  3  6  33.33 % 
1  3f   WW  3  6  16.67 % 

總和計kodeall通過在查詢A. kode1

和百分比=合計/總和* 100 例2/7 * 100

請幫我

+2

歡迎StackOverflow上:如果您發佈的代碼,XML或數據樣本,** **請在高亮文本編輯器這些線路上,並點擊了的「代碼示例」按鈕('{}')編輯器工具欄以很好地格式化和語法突出顯示它!這樣,你不需要任何雜亂的'
'標籤! –

+1

我是sory.thx您的信息。 – rian

回答

0

我認爲你自己已經相當接近地回答了。我以SQL Server作爲RDBMS爲例。你的問題中沒有提到RDBMS。
我使用兩個CTE(公用表表達式)來保存你的兩個查詢,然後將它們連接在一起計算百分比,請注意,你最終結果中的百分比實際上是一個字符串。


;WITH CTE_TotalKodeall 
AS 
(
    SELECT TOP 100 PERCENT 
     COUNT(kodeall) AS Total, kodeall, kode, LEFT(kodeall, 1) AS kode1 
    FROM 
     dbo.data 
    WHERE 
     date BETWEEN '2013/03/01 00:00:00' AND '2013/03/01 23:59:00' 
     AND (kodeall IS NOT NULL) 
    GROUP BY 
     kodeall, kode 
    ORDER BY 
     kode1 
), CTE_SUMKodeall 
AS 
(
    SELECT  
    COUNT(kodeall) AS [Sum], LEFT(kodeall, 1) AS kode1 
    From 
    dbo.data 
    WHERE 
    date BETWEEN '2013/03/01 00:00:00' 
    AND '2013/03/01 23:59:00' 
    AND (kodeall IS NOT NULL) 
    GROUP BY 
    LEFT(kodeall, 1) 
) 
SELECT A.Total, A.kodeall, A.kode, A.kode1, B.[sum], 
     LEFT(cast((A.Total/(B.[SUM]*1.0) *100.0) as varchar(30)),5)+'%' as Percentage 
FROM CTE_TotalKodeall A 
JOIN CTE_SUMKodeall B 
ON A.kode1 = B.kode1 
+0

thx @ljh ... thx..hehe ..它的工作在SQL服務器2005年 – rian

+0

如何設置百分比與小數2位 – rian

+0

你的意思是你只是想顯示74%,而不是74.13%?如果是這樣,您需要使用ROUND()函數,如下所示:LEFT((CAST(ROUND((2 /(7 * 1.0)* 100.0),0)AS VARCHAR(30))),2)+'%' – ljh

0
SELECT TOP 100 PERCENT 
    COUNT(kodeall) AS Total, 
kodeall, kode, 
LEFT(kodeall, 1) AS kode1 , 
    sum1, 
    ((Count(kodeall)/sum1)* 100) As percentage 
FROM 
    dbo.data 
INNER JOIN 
(SELECT  
    COUNT(kodeall) AS Sum, 
LEFT(kodeall, 1) AS kode1 
    From 
    dbo.data 
    WHERE 
     date BETWEEN '2013/03/01 00:00:00' 
     AND '2013/03/01 23:59:00' 
     AND (kodeall IS NOT NULL) 
    GROUP BY 
    LEFT(kodeall, 1) 
) temp on temp.kode1 = dbo.data.kode1 

WHERE 
    date BETWEEN '2013/03/01 00:00:00' AND '2013/03/01 23:59:00' 
    AND (kodeall IS NOT NULL) 
    GROUP BY 
    kodeall, kode 
ORDER BY 
    dbo.data.kode1 
+0

關鍵字'WHERE'附近的語法錯誤。 – rian

+0

@rian嘗試更新查詢 – Dhinakar

0

這應該是相當接近(未經測試)

With cte1 as (
    SELECT COUNT(kodeall) AS Total, kodeall, kode, LEFT(kodeall, 1) AS kode1 
    FROM dbo.data 
    WHERE date >= '2013/03/01' AND date < '2013/03/02' 
     AND kodeall IS NOT NULL 
    GROUP BY kodeall, kode 
), cte2 as (
    SELECT COUNT(kodeall) AS Sum, LEFT(kodeall, 1) AS kode1 
    From dbo.data 
    WHERE date >= '2013/03/01' AND date < '2013/03/02' 
     AND kodeall IS NOT NULL 
    GROUP BY LEFT(kodeall, 1) 
) 
SELECT c1.Total, c1.kodeall, c1.kode, c1.kode1, 
    c2.sum, c1.Total/c2.sum percentage 
FROM cte1 c1 
    JOIN cte2 c2 ON c1.kode1 = c2.kode1 

請小心使用BETWEEN,特別是日期字段。我會使用大於或小於。

+0

什麼是c1和c2 – rian

+0

@rian - 這些只是別名 - 比輸入整個表/ cte名稱更容易。你可以使用列和表:)別名 – sgeddes