2016-07-28 15 views
2

我想根據職位創建組織層次結構的報告。層次結構有4個層次,位置總是最後一層,但路徑不一定是4層。跳過數據爲NULL的重複捲起的行

例如:

公司>位置

公司>科>系>位置

我想獲得的人員數量。

declare @hierarchy table 
(
    Company nvarchar(50), 
    Section nvarchar(50), 
    Department nvarchar(50), 
    Unit nvarchar(50), 
    Position nvarchar(50), 
    Person nvarchar(50) 
) 

insert into @hierarchy values 
('WD', 'Weapons', 'IT', 'officer', null, 'Wile E.'), 
('ACME', 'Weapons', 'IT', 'Network', 'engineer', 'Brain'), 
('ACME', 'Weapons', 'IT', 'Network', 'support', 'Pinky'), 
('ACME', 'Weapons', 'IT', 'officer', null, 'Bugs'), 
('ACME', 'Weapons', 'IT', 'officer', null, 'Elmer'), 
('ACME', 'Weapons', 'IT', 'officer', null, 'Daffy'), 
('ACME', 'Weapons', 'tech', null, null, 'Sylverster'), 
('ACME', 'Anvils', 'officer', null, null, 'Road') 

select Company, Section, Department, Unit, Position, count(Person) from @hierarchy 
group by rollup(Company, Section, Department, Unit, Position) 

在上面的例子中,我得到了同樣的3行的WD(西部數據,砧,官,NULL,NULL),其中一個就足夠了,因爲單位和位置是不適用的。

但是,如果我在查詢放distinct,我得到一個看似不錯的結果

select distinct Company, Section, Department, Unit, Position, count(Person) from @hierarchy 
group by rollup(Company, Section, Department, Unit, Position) 

什麼我不知道的是,如果這只是一些黑客,我真的很幸運,或者如果它是一個正確的方法解決這個問題?

+1

請加期待的結果 – Backs

+0

@backs:如果您運行的查詢用戶提供有重複的行,用戶並不希望這些重複使用 – TheGameiswar

+0

'集團BY'爲每列然後使用'聯盟所有' – NEER

回答

1

讓我們添加GROUPING列每個級別:

SELECT 
    Company, 
    Section, 
    Department, 
    Unit, 
    Position, 
    GROUPING(Company) as Company, 
    GROUPING(Section) AS Section, 
    GROUPING(Department) AS Department, 
    GROUPING(Unit) AS Unit, 
    GROUPING(Position) AS Position, 
    COUNT(*) 
from @hierarchy 
group by ROLLUP(Company, Section, Department, Unit, Position) 

而且看你的重複值:

Company Section Department Unit  Position Company Section Department Unit Position 
---------- ---------- ---------- ---------- ---------- ------- ------- ---------- ---- -------- ----------- 
ACME  Anvils  officer NULL  NULL  0  0  0   0 0  1 
ACME  Anvils  officer NULL  NULL  0  0  0   0 1  1 
ACME  Anvils  officer NULL  NULL  0  0  0   1 1  1 
ACME  Anvils  NULL  NULL  NULL  0  0  1   1 1  1 
ACME  Weapons IT   Network engineer 0  0  0   0 0  1 
ACME  Weapons IT   Network support 0  0  0   0 0  1 
ACME  Weapons IT   Network NULL  0  0  0   0 1  2 
ACME  Weapons IT   officer NULL  0  0  0   0 0  3 
ACME  Weapons IT   officer NULL  0  0  0   0 1  3 
ACME  Weapons IT   NULL  NULL  0  0  0   1 1  5 
ACME  Weapons tech  NULL  NULL  0  0  0   0 0  1 
ACME  Weapons tech  NULL  NULL  0  0  0   0 1  1 
ACME  Weapons tech  NULL  NULL  0  0  0   1 1  1 
ACME  Weapons NULL  NULL  NULL  0  0  1   1 1  6 
ACME  NULL  NULL  NULL  NULL  0  1  1   1 1  7 
WD   Weapons IT   officer NULL  0  0  0   0 0  1 
WD   Weapons IT   officer NULL  0  0  0   0 1  1 
WD   Weapons IT   NULL  NULL  0  0  0   1 1  1 
WD   Weapons NULL  NULL  NULL  0  0  1   1 1  1 
WD   NULL  NULL  NULL  NULL  0  1  1   1 1  1 
NULL  NULL  NULL  NULL  NULL  1  1  1   1 1  8 

(ACME,砧,官)重複3次,但每一次,它是一個新的分組級別:按部門,按部門和單位,按部門,單位和職位劃分。這是因爲我們使用了所有的列。但是單位和頭寸的數值是NULL

因此,您可以將DISTINCT添加到您的查詢中,以通過您的列獲得唯一結果 - 這是正確的。

+0

@romeozor你能接受我的答案,或者我需要添加一些評論? – Backs

0

你可以去一堆工會而不是彙總?

select 
concat(Company,' ',Section,' ',Department,' ',Unit,' ',Position) as Hierarchy, 
Total 
from (
    select Company, Section, Department, Unit, Position, count(*) as total from @hierarchy 
    group by Company, Section, Department, Unit, Position 
    union 
    select Company, Section, Department, Unit, null, count(*) from @hierarchy 
    group by Company, Section, Department, Unit 
    union 
    select Company, Section, Department, null, null, count(*) from @hierarchy 
    group by Company, Section, Department 
    union 
    select Company, Section, null, null, null, count(*) from @hierarchy 
    group by Company, Section 
    union 
    select Company, null, null, null, null, count(*) from @hierarchy 
    group by Company 
) q 
order by Company, Section, Department, Unit, Position; 

給出:

ACME        7 
ACME Anvils       1 
ACME Anvils officer     1 
ACME Weapons      6 
ACME Weapons IT      5 
ACME Weapons IT Network    2 
ACME Weapons IT Network engineer 1 
ACME Weapons IT Network support  1 
ACME Weapons IT officer    3 
ACME Weapons tech     1 
WD         1 
WD Weapons       1 
WD Weapons IT      1 
WD Weapons IT officer    1