2016-07-21 120 views
-1

我有一個表,我需要在子查詢中計算兩個不同的列。我有這樣的事情:從DB中獲取兩個子查詢的百分比

SELECT 
    COUNT(z.id_contract) AS countOfContracts, 
    (
     SELECT COUNT(zml.id_contract) FROM [DW].[dbo].[DW_D_CONT] AS zml 
     WHERE zml.p_akt= 'A' 
     AND d_start_dw > '2013-01-01' 
     AND d_start_dw < '2013-01-31' 
    ) AS januar, 
    (
     SELECT COUNT(zml.id_contract) FROM [DW].[dbo].[DW_D_CONT] AS zml 
     WHERE zml.p_akt= 'A' 
     AND d_start_dw > '2013-02-01' 
     AND d_start_dw < '2013-02-28' 
    ) AS februar 
    FROM ... 

我需要做的是這樣

januar/februar * 100 

然而alliases不子查詢工作。於是我想到了這樣的事情:

(
     (SELECT COUNT(zml.id_contract) FROM [DW].[dbo].[DW_D_CONT] AS zml 
     WHERE zml.p_akt= 'A' 
     AND d_start_dw > '2013-01-01' 
     AND d_start_dw < '2013-01-31') 
    ) 
    /
    (
     (SELECT COUNT(zml.id_contract) FROM [DW].[dbo].[DW_D_CONT] AS zml 
     WHERE zml.p_akt= 'A' 
     AND d_start_dw > '2013-02-01' 
     AND d_start_dw < '2013-02-28') 
    ) 

所以我試圖讓「januar」和「februar」像一個子任務計數(你可以看到有不同的日期),但我總是收到100%。我絕對相信,januar和februar的數量是不同的

somone能幫助我嗎?

回答

0

假設你不工作的SQL Server(前2008)的一個過時的版本,你可以使用一個cte

;WITH cte as 
(
     SELECT 
    COUNT(z.id_contract) AS countOfContracts, 
    (
     SELECT COUNT(zml.id_contract) FROM [DW].[dbo].[DW_D_CONT] AS zml 
     WHERE zml.p_akt= 'A' 
     AND d_start_dw > '2013-01-01' 
     AND d_start_dw < '2013-01-31' 
    ) AS Januar, 
    (
     SELECT COUNT(zml.id_contract) FROM [DW].[dbo].[DW_D_CONT] AS zml 
     WHERE zml.p_akt= 'A' 
     AND d_start_dw > '2013-02-01' 
     AND d_start_dw < '2013-02-28' 
    ) AS Februar 
    FROM ... 
) 

SELECT Januar, Februar, Januar/Februar * 100 
FROM cte 
1

我會建議使用OUTER APPLY

SELECT COUNT(z.id_contract) AS countOfContracts, 
     months.jan, months.feb, months.jan/months.feb 
FROM ... OUTER APPLY 
    (SELECT SUM(CASE WHEN d_start_dw > '2013-01-01' AND d_start_dw < '2013-01-31' 
        THEN 1.0 ELSE 0 
       END) as jan, 
      SUM(CASE WHEN d_start_dw > '2013-02-01' AND d_start_dw < '2013-02-28' 
        THEN 1.0 ELSE 0 
       END) as feb 
    FROM [DW].[dbo].[DW_D_CONT] AS zml 
    WHERE zml.p_akt = 'A' 
    ) months; 

這可以讓你在單個子查詢中聚合所有月份,這應該比每個月的單獨子查詢更有效。

你也應該檢查你的日期算法。我不確定爲什麼你會排除本月的第一天和最後一天。

+0

我的問題是這兩個子查詢的劃分。我總是收到1(這意味着100%)。但一月份的數字與二月份不一樣。我不知道它是怎麼可能的: – MegoSoft

+0

@MegoSoft ... SQL Server做整數除法,因此,你需要將值更改爲浮點/十進制表示,我只是將'1'更改爲'1.0'解決這個問題。 –

0

它應該能夠做到這一點,而不使用子查詢,像這樣:

select 
    count(case when p_akt = 'A' then id_contract end) as total_count, 
    count(
     case when p_akt = 'A' and d_start_dw > '2013-01-01' and d_start_dw <= '2013-01-31' 
      then id_contract 
     end 
    ) as januar_count, 
    count(
     case when p_akt = 'A' and d_start_dw > '2013-02-01' and d_start_dw <= '2013-02-28' 
      then id_contract 
     end 
    ) as februar_count 
from 
    [DW].[dbo].[DW_D_CONT] 

它通常是一個好主意,以避免使用子查詢儘可能。

0

這應該適用於任何MS SQL版本。

SELECT 
    COUNT(z.id_contract) AS countOfContracts, 
    COUNT(CASE WHEN d_start_dw > '2013-01-01' AND d_start_dw < '2013-01-31' 
     THEN zml.id_contract END) AS januar, 
    COUNT(CASE WHEN d_start_dw > '2013-02-01' AND d_start_dw < '2013-02-28' 
     THEN zml.id_contract END) AS februar 
FROM ..