我将更改 SQL 查询中的结果模型。 想象一下下面的查询:
SELECT
es.LatinName,
es.CreatedOn
FROM Salex.ExportFile ef
INNER JOIN Salex.ExportFileStep efs ON efs.ExportFileId = ef.Id
INNER JOIN GeneralSalex.ExportSubject es ON es.Id = efs.ExportSubjectId
WHERE ef.Id = 38
但我想将 LatinName 值放在 Columns 中,其 CreatedOn 值如下(预期结果在一行中):
|Packing List | Export Bill Of Lading | ...
|2021-06-15 16:12:57.6200000| 2021-07-31 09:30:12.5600000| ...
我尝试使用
PIVOT
,如下面的代码,但我无法扩展结果:
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days,
[0], [1], [2], [3], [4], [5], [6], [7], [8]
FROM
(
SELECT
es.LatinName,
es.CreatedOn
FROM Salex.ExportFile ef
INNER JOIN Salex.ExportFileStep efs ON efs.ExportFileId = ef.Id
INNER JOIN GeneralSalex.ExportSubject es ON es.Id = efs.ExportSubjectId
WHERE ef.Id = 38
) AS SourceTable
PIVOT
(
AVG(CreatedOn)
FOR LatinName IN ([0], [1], [2], [3], [4], [5], [6], [7], [8])
) AS PivotTable;
它会是这样的:
SELECT *
FROM (
SELECT es.LatinName, es.CreatedOn
FROM Salex.ExportFile ef
INNER JOIN Salex.ExportFileStep efs ON efs.ExportFileId = ef.Id
INNER JOIN GeneralSalex.ExportSubject es ON es.Id = efs.ExportSubjectId
WHERE ef.Id = 38
) AS SourceTable
PIVOT (
MAX(CreatedOn)
FOR LatinName IN (
[Packing List], [Export Bill Of Lading], [Insurance],
[Provide Export Warranty], [Custom Declaration],
[Inventory Permit], [Export Invoice], [Sale Invoice],
[Issued Certificate]
)
) AS PivotTable;