我正在创建报告,我有一个如下表
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
我怎样才能做到这一点?谢谢
答案 0 :(得分: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;
}