获取DB中两个subselect的百分比

时间:2016-07-21 11:55:57

标签: sql sql-server

我有一张桌子,我需要在subselect中计算两个不同的列。我有这样的事情:

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

然而,联盟在子选择中不起作用。所以我想到了这样的事情:

    (
        (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')
    )

所以我想要算上&#34; januar&#34;和&#34; februar&#34;就像一个子任务(你可以看到那里的不同日期),但我总是得到100%。我绝对肯定,januar和februar的数量是不同的

能够帮助我?

4 个答案:

答案 0 :(得分: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;

这允许您在单个子查询中聚合所有月份,这应该比每个月的单独子查询更有效。

您还应该查看日期算术。我不确定你为什么要排除这个月的第一天和最后一天。

答案 1 :(得分: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

答案 2 :(得分:0)

应该可以在不使用子查询的情况下执行此操作,如下所示:

<head runat="server">
    <title></title>
    <script src="jquery.js"></script>
    <script>
        $(document).ready(function () {
            jQuery.support.cors = true;
            ;
            $.ajax({
                //url: 'http://192.168.1.105:2143/api/values',
                url: 'public class Customer
    {
        public int CustomerId { get; set; }
        public string CustomerName { get; set; }
    }
',


                type: 'GET',
                dataType: 'json',            
                success: function (data) {                

                    alert(data[0].CustomerName);
                },
            });
        });
    </script>
</head>

尽可能避免使用子查询通常是一个好主意。

答案 3 :(得分: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 ..