2016-04-09 4 views
-2

tbl_employeeは、私のような出力をしたいデビット</p> <p>のための信用のためのスタンドと2 1ここでは、このシナリオ

empid empname openingbal 
2  jhon 400  
3  smith 500 

tbl_transection1

tid empid amount creditdebit date 
1 2  100  1   2016-01-06 00:00:00.000 
2 2  200  1   2016-01-08 00:00:00.000 
3 2  100  2   2016-01-11 00:00:00.000 
4 2  700  1   2016-01-15 00:00:00.000 
5 3  100  1   2016-02-03 00:00:00.000 
6 3  200  2   2016-02-06 00:00:00.000 
7 3  400  1   2016-02-07 00:00:00.000 

tbl_transection2

tid empid amount creditdebit date 
1 2  100  1   2016-01-07 00:00:00.000 
2 2  200  1   2016-01-08 00:00:00.000 
3 2  100  2   2016-01-09 00:00:00.000 
4 2  700  1   2016-01-14 00:00:00.000 
5 3  100  1   2016-02-04 00:00:00.000 
6 3  200  2   2016-02-05 00:00:00.000 
7 3  400  1   2016-02-08 00:00:00.000 

ためのSQLクエリをしたいです

empid empname details   debitamount creditamount balance  Dr/Cr date 
2  jhon  opening Bal         400   Cr  
2  jhon  transection 1     100   500   Cr  2016-01-06 00:00:00.000 
2  jhon  transection 2     100   600   Cr  2016-01-07 00:00:00.000 
2  jhon  transection 1     200   800   Cr  2016-01-08 00:00:00.000 
2  jhon  transection 2     200   1000   Cr  2016-01-08 00:00:00.000 
2  jhon  transection 2  100      900   Dr  2016-01-09 00:00:00.000 
2  jhon  transection 1  100      800   Dr  2016-01-11 00:00:00.000 
2  jhon  transection 2     700   1500   Cr  2016-01-14 00:00:00.000 
2  jhon  transection 1     700   2200   Cr  2016-01-15 00:00:00.000 
3  smith  opening Bal         500   Cr  
3  smith  transection 1     100   600   Cr  2016-02-03 00:00:00.000 
3  smith  transection 2     100   700   Cr  2016-02-04 00:00:00.000 
3  smith  transection 2  200      500   Dr  2016-02-05 00:00:00.000 
3  smith  transection 1  200      300   Dr  2016-02-06 00:00:00.000 
3  smith  transection 1     400   700   Cr  2016-02-07 00:00:00.000 
3  smith  transection 2     400   1100   Cr  2016-02-08 00:00:00.000 
+1

に例(http://stackoverflow.com/help)。これは無料のコード作成サービスでもチュートリアルサイトでもありません。試したコードをどのように動作していないのか、そして期待される結果がどうなるかについての説明とともに提示するために、基本的な研究をすでに行ったことが期待されます。 – jbm

+0

ちょうど親切なヒント、このページを読むことができます:[How-To-Ask Guide](https://stackoverflow.com/help/how-to-ask)可能な限り簡単に答えることができます。あなたが抱えている問題を修正するためにあなたがした努力と、それらの修正を試みたときに何が起こったのかを必ず含めてください。ショーコードとエラーメッセージも忘れないでください! – rsjaffe

答えて

0

あなたはこのような何かでそれを行うことができます。

select 
    empid, sum(amount) over (partition by empid order by date) as balance, details 
from (
    select 
    empid, case creditdebit when 1 then amount else -amount end as amount, date, details 
    from (
    select empid, openingbal as amount, 1 as creditdebit, '19000101' as date, 'opening Bal' as details 
    from tbl_employee 
    union all 
    select empid, amount, creditdebit, date, 'transection 1' 
    from tbl_transection1 
    union all 
    select empid, amount, creditdebit, date, 'transection 2' 
    from tbl_transection2 
) X 
) Y 

最も内側の選択は3つのテーブルからデータを収集することです、次が金額のために+/-を計算することであり、一番外側は天びんを計算することです。

[ヘルプセンター]で、ここで質問をするためのガイドラインを再読み込みするために時間を割いてくださいSQL Fiddle

関連する問題

 関連する問題