2009-06-10 42 views
0

に助けが必要ですが、あなたがすることによってグループ化し、カウントを取得している2列があるだろう:複雑なSQLクエリ

select col1, col2, count(*) 
from yourtable 
group by col1, col2 

をごcol2がユニークになりますが、あなたはCOL1年代繰り返されます。これを回避するには、次のことができます - ラグ(col1,1,0)(COL1による順)を超える -

select decode(lag(col1,1,0) over (order by col1),col1,' ',col1) col1, col2, cnt 
from 
(select col1, col2, count(*) cnt 
from yourtable 
group by col1, col2) 

これは、前の値と比較しますと、現在の値。 同じ場合は、代わりに空白が返されます。


私の質問私は、質問の最後に与えられた形式でデータを表示するには、以下のようにSELECTを記述しようとしています

。 ITSは機能していない、私はこれにアプローチする最も簡単な方法は、物事を簡単にすることであると思い

SELECT DECODE (LAG (firstname, 1, 0) OVER (ORDER BY firstname), 
       firstname, ' ', 
       firstname 
      ) firstname, 
     DECODE (LAG (emplid, 1, 0) OVER (ORDER BY emplid), 
       emplid, ' ', 
       emplid 
      ) emplid, 
     DECODE (LAG (tplan_name, 1, 0) OVER (ORDER BY tplan_name), 
       tplan_name, ' ', 
       tplan_name 
      ) tplan_name, 
    completed_cdt_act,    ----->This is 1 more calculated field corresponding to each TPLAN_NAME 

     DECODE (LAG (t_objective_id, 1, 0) OVER (ORDER BY t_objective_id), 
       t_objective_id, ' ', 
       t_objective_id 
      ) t_objective_id, 
     completed_activities 
    FROM (SELECT test_cp.firstname, test_op.emplid,tp.tplan_name, 
      fn_tp_get_cmpltd_act_cnt (tp.tplan_id, 
           test_cp.person_id, 
           test_o.org_id 
           ) completed_cdt_act, 
       test_tpo.t_objective_id, 
       ( fn_tp_obj_comp_req_act_cdt (test_lp.lp_person_id, 
               tp.tplan_id, 
               test_tpo.t_objective_id, 
               tp.is_credit_based 
              ) 
       + fn_tp_obj_comp_opt_act_cdt (test_lp.lp_person_id, 
               tp.tplan_id, 
               test_tpo.t_objective_id, 
               tp.is_credit_based 
              ) 
       ) completed_activities, 
       tpobjact.activity_id activity_id, -----> Again ,each objective_id we select CAN have multiple activity_ids.The remaining fields gives the activity_name,their status 

      lr.catalog_item_name, 
     fn_tp_get_act_type (tpobjact.is_preferred, 
          tpobjact.is_required) status, 
     lr.catalog_item_type activity_type, lr.delivery_method, lr.status status1, 
      FROM test_learning_plan test_lp, 
       test_training_plan tp, 
       test_person test_cp, 
       test_org_person test_op, 
       test_org test_o 
       test_tp_objective test_tpo, 
       test_train_obj_activity tpobjact, 

     test_learning_record lr, 
     test_training_objective obj, 
     WHERE test_lp.lp_person_id = '1' 
      AND test_cp.person_id = test_lp.lp_person_id 
      AND tp.tplan_id = test_lp.lp_catalog_hist_id 
      AND test_op.o_person_id = test_cp.person_id 
      and test_o.org_id = test_op.O_ORG_ID 
      AND test_tpo.tplan_id = tp.tplan_id 
      and lr.tp_ref_lp_id = test_lp.learning_plan_id 
    AND lr.lr_catalog_history_id = tpobjact.activity_id 


    AND tpobjact.t_objective_id = test_tpo.t_objective_id); 





****************************************************************************************************** 
The display format is something like this. 

firstname emplid Tplan name completed(for TP) Objective Name Credits (for objective) Number of activities completed(for objective) activity                                                           name activity completion status 

U1 U1     TP1          5     
                      OBJ1      4       3         C1   PASSED 
                                           C2  PASSED 
                                           C3 WAIVED 
          T1 ENROLLED 
          T2 ENROLLED 
                       OBJ2 3    2   
                         S1 ENROLLED 
          S2 PASSED 
          T3 WAIVED 
U1 U1 TP2     C4 INPROGRESS 
      50 OBJ11 50 30 C11 PASSED 
          C22 PASSED 
          C33 WAIVED 
          T11 ENROLLED 
          T22 ENROLLED 
       OBJ22 40 20  
          S11 ENROLLED 
          S22 PASSED 

答えて

0

を助けてください。

このように開始します。表示したい何

1) - あなたは、各従業員に対して「いくつかのもの」と従業員のリストをしたい場合は、単に従業員のリストを取得するスクリプトを書く

のリストが載っています、すなわち。

2)あなたが)のそれを追加します。あなたがするオプションがあり、従業員が行っている「何かの数を」知っているテーブルを結合し、グループ化Bを使用する必要がある場合は複雑

のビットを追加します)サブセレクトステートメントを実行して値を取得します。あなたの選択は、パフォーマンスの考慮事項に依存します。

3)これは動作していますか?もう少し複雑なものを追加します。各従業員が必要とする次のものは何ですか(つまり、複雑なアイテムを追加するために繰り返し実行し、各ステージで機能することを認識します)。

+0

ガイダンスのおかげで多くのことがありました。段階的にデータが必要となるのと同じように、徐々にテーブルを追加していました。でも、まだ私はそれらを表示する際に問題に直面しています。 選択するデータはわかりましたが、選択したデータが特定の形式で表示されているため、私は立ち往生しています。 また、LAGをデコードして、この必要な表示を取得し、繰り返し値を排除しました。しかし、上記のSELECTを見ていただければ幸いです。それは必要に応じてディスプレイを取得する。 ありがとう –