我有以下代码 -
DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);
select @cols = STUFF((SELECT distinct ',' +
QUOTENAME(Name)
FROM JobPhases
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '');
SELECT @query = 'SELECT *
FROM
(
SELECT c.Registration, p.Name, [x] = 1
FROM JobDetails AS j
INNER JOIN JobPhases p ON p.ID = j.PhaseId
INNER JOIN Jobs job on job.ID = j.JobID
INNER JOIN Cars c on job.CarID = c.ID
) JobDetails
PIVOT
( SUM(x)
FOR Name IN (' + @cols + ')
) pvt'
这会产生这个 -
JobID | Repair & Reshape | Refit Stripped Parts | Polishing
1000 | id | id | id
1001 | id | id | id
1002 | id | id | id
1003 | id | id | id
1004 | id | id | id
但是,我希望它显示 j.EstimatedTime 而不是 j.ID。
表结构——
工作详情
ID - PK Auto increment
JobID - Int (Joined to Jobs table)
PhaseID - String (joined to JobPhases table)
EstimatedTime - decimal
工作阶段
ID - PK String
Name - VarChar(150)
有什么想法可以实现这一目标吗?
谢谢