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
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 に答える
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 に答える