1

いくつかの異なるテーブルから集計合計値を取得するストアド プロシージャを作成しています。これとは別に、クエリはさまざまなフィルターで単純化されています。

クエリは結合する必要があり、次のようになります。

select distinct(bus_name), sum(act) as 'totrev', sum(budget) as 'budget rev' 
from finance
where year = '2011' 
and type_desc = 'rev'
group by bus_code, bus_name
order by bus_name asc

select distinct(bus_name), sum(act) as 'totalexp', sum(budget) as 'budget exp' 
from finance
where year = '2011' 
and type_desc = 'exp'
group by bus_code, bus_name
order by bus_name asc

select distinct(bus_name), sum(end_balance) as 'total assets' 
from Balance
where year = '2011' 
and type_desc = 'assets'
group by bus_code, bus_name
order by bus_name asc

select distinct(bus_name), sum(end_balance) as 'Cash' 
from Balance
where year = '2011' 
and type_desc = 'equity'
group by bus_code, bus_name
order by bus_name asc

select bus_code, bus_name, count(bus_code) as '#of bldgs'
from building
group by bus_code, bus_name
order by bus_name asc

基本的に1つのテーブルに表示されるすべての列をマージ/結合しようとしています。finance_table
列 = bus_code、bus_name、ビル数、tot_rev、budget_rev、totalexp、budget exp、総資産、現金

4

2 に答える 2

1

SQLがCASE式をサポートしている場合は、それらを使用してタイプごとに「仮想」フィールドを作成し、それらを合計することができます。

select bus_code, bus_name
 ,sum(case when type_desc = 'rev' then act else 0 end) as 'totrev'
 ,sum(case when type_desc = 'rev' then budgetelse 0 end) as 'budget rev' 
 ,sum(case when type_desc = 'exp' then act else 0 end) as 'totexp'
 ,sum(case when type_desc = 'exp' then budgetelse 0 end) as 'budget exp' 
 ... ... etc.
from finance
where year = '2011' 
group by bus_code, bus_name
order by bus_name asc

最後の(構築)テーブルは、バスコードでこのテーブルに簡単に結合できます

于 2013-01-25T20:03:53.127 に答える
1

ネストされたクエリを使用して、次のようなことを試してください。

SELECT T5.bus_code, T5.bus_name, T5.[# of bldgs], T1.tot_rev, T1.budget_rev, T2.totalexp, T2.[budget exp], T3.[total assets], T4.cash
FROM
    (
    select distinct(bus_name), sum(act) as 'totrev', sum(budget) as 'budget rev' 
    from finance
    where year = '2011' 
    and type_desc = 'rev'
    group by bus_code, bus_name
    order by bus_name asc
    ) T1 INNER JOIN
    (
    select distinct(bus_name), sum(act) as 'totalexp', sum(budget) as 'budget exp' 
    from finance
    where year = '2011' 
    and type_desc = 'exp'
    group by bus_code, bus_name
    order by bus_name asc
    ) T2 ON T1.bus_name = T2.bus_name
    INNER JOIN
    (
    select distinct(bus_name), sum(end_balance) as 'total assets' 
    from Balance
    where year = '2011' 
    and type_desc = 'assets'
    group by bus_code, bus_name
    order by bus_name asc
    ) T3 ON T2.bus_name = T3.bus_name
    INNER JOIN
    (
    select distinct(bus_name), sum(end_balance) as 'Cash' 
    from Balance
    where year = '2011' 
    and type_desc = 'equity'
    group by bus_code, bus_name
    order by bus_name asc
    ) T4 ON T3.bus_name = T4.bus_name
    INNER JOIN
    (
    select bus_code, bus_name, count(bus_code) as '#of bldgs'
    from building
    group by bus_code, bus_name
    order by bus_name asc
    ) T5 ON T4.bus_name = T5.bus_name

内部結合を想定していますが、これらの一部に特定のビジネスのエントリがない場合は、外部結合を使用する必要がある場合があります。しかし、一般的なテクニックは同じです。

于 2013-01-25T19:05:13.997 に答える