0

我有三个头

Scheme| Account| Nav_date| Amount
DYFF  |INVEQU  | 25/01/2013|  100
DYFF  |INVCOD  | 25/01/2013|  100
GBGF  |INVEQU  | 25/01/2013|  200
GBGF  |INVCOD  | 25/01/2013|  100
DYFF  |INVEQU  | 26/01/2013|  150
DYFF  |INVCOD  | 26/01/2013|  150

我想要一个 SQL 查询来获取方案 =“DYFF”的每个日期的该方案的该日期的总金额的 INVEQU 百分比

所以答案就像

Scheme| Account| nav_date| Percentage
DYFF  |INVEQU  |25/01/2013 | 50%
DYFF  |INVEQU  |26/01/2013 | 50%

我正在使用 Oracle 数据库

4

3 回答 3

0

我会使用这种简单的方法。强制使用只有 1 个结果的 SUM 表的 CrossJoin 并将其应用于每一行。

SELECT 
     T.Scheme, T.Account, T.Nav_Date, SUM(T.Amount), SUM(T.Amount)/T2.Total
FROM 
    Table1 T,
    (SELECT SUM(Amount) as Total FROM Table1) T2
WHERE
    T.Scheme = 'DYFF'
GROUP BY
    T.Scheme, T.Account. T.Nav_Date
于 2013-08-29T12:03:26.690 回答
0
WITH Schemes AS
(
    SELECT
     *
    ,ROUND(CAST(Amount AS DECIMAL(18, 2))/SUM(Amount) OVER(PARTITION BY Scheme, Value_Date)*100, 2) AS Percentage
    FROM (VALUES
     ('DYFF'  ,'INVEQU'  , '25/01/2013',  100)
    ,('DYFF'  ,'INVCOD'  , '25/01/2013',  100)
    ,('GBGF'  ,'INVEQU'  , '25/01/2013',  200)
    ,('GBGF'  ,'INVCOD'  , '25/01/2013',  100)
    ,('DYFF'  ,'INVEQU'  , '26/01/2013',  150)
    ,('DYFF'  ,'INVCOD'  , '26/01/2013',  150)) v(Scheme, Account, Value_Date, Amount)
)
SELECT
 *
FROM Schemes
WHERE Scheme='DYFF'
AND Account='INVEQU';

或者在你的情况下

WITH Schemes AS
(
    SELECT
     *
    ,ROUND(CAST(Amount AS DECIMAL(18, 2))/SUM(Amount) OVER(PARTITION BY Scheme, Value_Date)*100, 2) AS Percentage
    FROM YOUR_TABLE
)
SELECT
 *
FROM Schemes
WHERE Scheme='DYFF'
AND Account='INVEQU';
于 2013-08-29T10:10:43.583 回答
0

试试这个:

  select scheme
       , 'INVEQU'    account
       , value_date
       , case tl
            when 0 then 0
            else        100*COALESCE(tl_i,0)/tl
         end         percentage
    from (
                select agg_i.tl             tl_i
                     , agg_tl.tl            tl
                     , agg_tl.scheme
                     , agg_tl.value_date
                  from (
                            select scheme
                                 , value_date
                                 , sum(amount)  tl
                              from table t_tl
                          group by scheme,
                                 , value_date
                        ) agg_tl 
              LEFT JOIN
                        (         
                            select scheme
                                 , value_date
                                 , sum(amount)  tl
                              from table t_i
                             where account = 'INVEQU'
                          group by scheme,
                                 , value_date
                        ) agg_i
                     ON (     agg_i.scheme       = agg_tl.scheme  
                          AND agg_i.value_date   = agg_tl.value_date  )        
          ) pct
          ;
于 2013-08-29T10:11:50.457 回答