SQL-范围和百分比问题

时间:2018-10-16 07:17:08

标签: sql sql-server tsql

我有一个星期以上的以下问题,希望能得到您的帮助。这是我的查询:

 WITH S AS 
  (SELECT SHARE_FROM,
             SHARE_TO
        FROM (               
               SELECT 1 AS SHARE_FROM, 20 AS SHARE_TO UNION
               SELECT 21,100 UNION
               SELECT 101,200 UNION 
               SELECT 201,500 UNION
               SELECT 501,1200 UNION
               SELECT 1201,9999)AS T1
 ),
     Z AS 
  (SELECT COUNT(*) AS CNT_OF_SHARES,
             MEMBER_ID
        FROM COS_SHARES
       WHERE SERVICE_PRODUCT = 38001
       GROUP BY MEMBER_ID
 ),
     T AS 
  (SELECT S.SHARE_FROM AS SHARE_FROM,
        S.SHARE_TO AS SHARE_TO,
        COUNT(Z.CNT_OF_SHARES) AS NUMBER_OF_MEMBERS,
       (CASE WHEN SUM(Z.CNT_OF_SHARES) IS NULL THEN 0 ELSE SUM(Z.CNT_OF_SHARES) END) AS  NUMBER_OF_SHARES                            
   FROM S
        LEFT JOIN Z ON(Z.CNT_OF_SHARES BETWEEN S.SHARE_FROM AND S.SHARE_TO)
        LEFT JOIN COS_MEMBERS M
                ON (M.MONITORING_UNIT = 143
                    AND M.MEMBER_STATUS = 1
                    AND M.SHARES_TOTAL <> 0
                    AND M.MEMBER_ID = Z.MEMBER_ID)
   GROUP BY S.SHARE_FROM, S.SHARE_TO
)  
  SELECT T.SHARE_FROM AS COUNT_OF_SHARES_FROM,
         T.SHARE_TO AS COUNT_OF_SHARES_TO, 
         T.NUMBER_OF_MEMBERS AS NUMBER_OF_MEMBERS,
         ROUND (T.NUMBER_OF_MEMBERS / SUM(T.NUMBER_OF_MEMBERS  * 1.0) OVER () *100,5 )  AS PERCENT_MEMBERS_NUMBER,
         ' ?? ' AS MEMBERS_TOTAL,
         T.NUMBER_OF_SHARES AS NUMBER_OF_SHARES,
         ROUND (T.NUMBER_OF_SHARES / SUM(T.NUMBER_OF_SHARES  * 1.0) OVER () *100,5 )    AS PERCENT_SHARES_NUMBER,
         ' ?? ' AS SHARES_TOTALS
  FROM T

此查询的结果如下:

+------+------+-------+-------+-------+------+-------+------+
|cnt_of|cnt_of|no_of  |percent|members|no_of |percent|shares|
|shares|shares|members|members|total  |shares|shares |totals|
|from  | to   |       |number |       |      |number |      |
+------+------+-------+-------+-------+------+-------+------+
|1     |20    | 1     |7.14   |  ??   |20    |0.10   |  ??  | 
|21    |100   | 4     |28.57  |  ??   |187   |0.97   |  ??  |
|101   |200   | 0     |  0    |  ??   | 0    |  0    |  ??  |
|201   |500   | 0     |  0    |  ??   | 0    |  0    |  ??  |
|501   |1200  | 4     |28.57  |  ??   |4003  |20,80  |  ??  |
|1201  |9999  | 5     |35.71  |  ??   |15030 |78,11  |  ??  |
+------+------+-------+-------+-------+------+-------+------+

我的问题是我想在带有“ ??”的列中总结成员 以及到那时为止的股票。

所以结果应该是这样的:

+------+------+-------+-------+-------+------+-------+------+
|cnt_of|cnt_of|no_of  |percent|members|no_of |percent|shares|
|shares|shares|members|members|total  |shares|shares |totals|
|from  | to   |       |number |       |      |number |      |
+------+------+-------+-------+-------+------+-------+------+
|1     |20    | 1     |7.14   |  1    |20    |0.10   | 20   | 
|21    |100   | 4     |28.57  |  5    |187   |0.97   | 207  |
|101   |200   | 0     |  0    |  5    | 0    |  0    | 207  |
|201   |500   | 0     |  0    |  5    | 0    |  0    | 207  |
|501   |1200  | 4     |28.57  |  9    |4003  |20,80  | 4210 |
|1201  |9999  | 5     |35.71  |  14   |15030 |78,11  | 19240|
+------+------+-------+-------+-------+------+-------+------+

有什么想法吗?我在SQL Server 2012中运行此查询。

谢谢!

0 个答案:

没有答案