2016-12-19 62 views
-1

林在創建報表的過程中,我有一個像下面SQL集團通過以單獨的結果表

EmployeeName | Department | Zone | 

Joseph  | IT  | 1 | 

Jack   | IT  | 3 | 

John   | IT  | 2 | 

James  | IT  | 3 | 

Jashua  | IT  | 1 | 

Jingle  | IT  | 2 | 

Sam   | HR  | 4 | 

Sid   | HR  | 5 | 

Steve  | HR  | 6 | 

Sal   | HR  | 5 | 

Stephen  | HR  | 6 | 

Signa  | HR  | 4 | 

結果集應在以下格式

Department | Zone 

IT   | 1 

EmployeeName 

Joseph 

Jashua 



Department | Zone 

IT   | 2 


EmployeeName 

John 

Jingle 


Department | Zone 

IT   | 3 


EmployeeName 

Jack 

James 



Department | Zone 
HR   | 4 

EmployeeName 

Sam 

Signa 



Department | Zone 

HR   | 5 


EmployeeName 

Sid 

Sal 


Department | Zone 

HR   | 6 


EmployeeName 

Steve 

Stephen 

表哪有我做到這一點?謝謝

回答

1

SQL select語句的結果始終是一個表(或單個值),但從不是一組動態表。 我建議使用查詢像

select department, zone, employename 
from my table 
order by department, zone 

在SQL,作爲結果是一個表,你不會有任何分隔符時,部門或區域的變化。 但是,如果您使用其他編程語言生成報告,則可以瀏覽結果並觀察部門和區域的更改並輸入水平線。

僞代碼,這可能如下所示:

prevDept=''; 
prevZone=''; 
while (hasNextRecord())) 
{ 
    record = getNextRecord(); 
    if (prevDept <> record.department OR prevZone <> record.zone) 
    writeHorizontalLine(); 
    prevDept = record.departnemt; 
    prevZone = record.zone; 
}