我有具体的报告,我需要创建。我有两张桌子,一张有我公司的工作数据,另一张有在那里工作的人的数据。在工作表中有一个数据字段,其中包含有多少人可以从事某项工作的数据(工作名称和1,2或其他人数)。我需要报告,它将显示作业的名称,然后显示在该作业上工作的人员列表,但这是问题,如果人数较少,那么工作地点我需要为每个空工作地点添加空行。例如:
job name 1 = 3 places
job name 2 = 4 places
报告:
JOB NAME 1
----------------------
WORKING MAN NAME 1
WORKING MAN NAME 2
--- empty place ---
JOB NAME 2
----------------------
WORKING MAN NAME 1
WORKING MAN NAME 2
--- empty place ---
--- empty place ---
我忘了提到我正在使用IBM系统Z上的DB2数据库以及用于构建我正在使用Visual Studio 10的报表。我的两个表是:
JOB:job_id,job_name,job_num_places
WORKER:worker_id,worker_name,job_id
我正在job_id上加入表格
答案 0 :(得分:0)
WITH JobPositions AS (
SELECT 'Job1' AS JobName, 3 AS Positions
UNION ALL
SELECT 'Job2', 4 AS Positions
)
,Workers AS (
SELECT 'Worker1' AS WorkerName, 'Job1' AS JobName
UNION ALL
SELECT 'Worker2' AS WorkerName, 'Job1' AS JobName
UNION ALL
SELECT 'Worker3' AS WorkerName, 'Job2' AS JobName
)
-- Real query starts here.
-- Create a line for each position in each job
, JobPositionNumber AS (
SELECT JobName, Positions, 1 AS PositionNumber
FROM JobPositions
UNION ALL
SELECT JobName, Positions, PositionNumber + 1
FROM JobPositionNumber
WHERE PositionNumber < Positions
)
-- Number the workers from 1 and up per job
, WorkerPositionNumber AS (
SELECT WorkerName
,JobName
,ROW_NUMBER() OVER (PARTITION BY JobName
ORDER BY WorkerName) AS PositionNumber
FROM Workers
)
-- Now just join numbered jobs with numbered workers
SELECT JobPositionNumber.JobName
,CASE WHEN WorkerPositionNumber.WorkerName IS NULL
THEN '--- empty place ---'
ELSE WorkerName
END AS WorkerName
FROM JobPositionNumber
LEFT JOIN WorkerPositionNumber
ON JobPositionNumber.JobName = WorkerPositionNumber.JobName
AND JobPositionNumber.PositionNumber = WorkerPositionNumber.PositionNumber
ORDER BY JobPositionNumber.JobName
,JobPositionNumber.PositionNumber