我已经得到一个地球科学数据数据库,其任务是查询样品位置以及几种元素(金砷,铅和铜)的分析结果。在以前的数据库中,我处理的分析结果已列在表中,其中每个标头是一个元素,主键是样品ID。
此政府数据库已在列中列出了元素
(dbo.SurfaceSampleAttr.AttributeColumn)
,其结果在第二列(dbo.SurfaceSampleAttr.AttributeValue)
中。我设法能够使用以下代码查询一个元素。
SELECT
dbo.SurfaceSample.SampleId,
dbo.SurfaceSample.CompanySampleId,
dbo.SurfaceSampleCoordinate.Easting,
dbo.SurfaceSampleCoordinate.Northing,
dbo.SurfaceSampleCoordinate.Datum,
dbo.SurfaceSampleCoordinate.Projection,
dbo.SurfaceSampleCoordinate.Zone,
dbo.SurfaceSample.CompanyId,
dbo.SurfaceSample.SurfaceSampleType,
dbo.MRTHeader.HeaderValue AS 'Tenement Holder',
dbo.SurfaceSampleAttr.AttributeValue
FROM
((dbo.SurfaceSample
INNER JOIN
dbo.SurfaceSampleCoordinate ON dbo.SurfaceSample.Id = dbo.SurfaceSampleCoordinate.SurfaceSampleId)
LEFT JOIN
dbo.SurfaceSampleAttr ON dbo.SurfaceSampleCoordinate.SurfaceSampleId = dbo.SurfaceSampleAttr.SurfaceSampleId
AND ((dbo.SurfaceSampleAttr.AttributeColumn) = 'Au_ppb'))
INNER JOIN
dbo.MRTHeader ON dbo.SurfaceSample.CompanyId = dbo.MRTHeader.MRTFileId
WHERE
(((dbo.SurfaceSampleCoordinate.Zone) = '50')
AND ((dbo.MRTHeader.HeaderNumber) = 'H0101'));
我理想的最终状态是列出所有样本位置元数据,例如样本ID,位置,公司等,然后将分析左移并带别名加入右侧”;并非所有样本都具有全部(或任何)分析结果,因此我不想将其过滤掉。
我已经尝试在整个FROM中添加其他带括号的SELECT条件,以及尝试在上述代码中添加其他JOIN,但是我无法弄清楚如何为同一列提供具有不同条件的不同别名。
编辑:
如果语法正确,我想以这种方式工作:
SELECT
dbo.SurfaceSample.SampleId,
dbo.SurfaceSample.CompanySampleId,
dbo.SurfaceSampleCoordinate.Easting,
dbo.SurfaceSampleCoordinate.Northing,
dbo.SurfaceSampleCoordinate.Datum,
dbo.SurfaceSampleCoordinate.Projection,
dbo.SurfaceSampleCoordinate.Zone,
dbo.SurfaceSample.Anumber,
dbo.SurfaceSample.CompanyId,
dbo.SurfaceSample.SurfaceSampleType,
dbo.MRTHeader.HeaderValue AS 'Tenement Holder',
FROM
(dbo.SurfaceSample
LEFT JOIN
(SELECT
dbo.SurfaceSampleAttr.AttributeValue AS 'Au_ppb'
FROM
dbo.SurfaceSampleAttr
WHERE
(dbo.SurfaceSampleAttr.AttributeColumn) = 'Au_ppb') ON dbo.SurfaceSampleCoordinate.SurfaceSampleId = dbo.SurfaceSampleAttr.SurfaceSampleId
LEFT JOIN
(SELECT
dbo.SurfaceSampleAttr.AttributeValue AS 'Cu_ppm'
FROM
dbo.SurfaceSampleAttr
WHERE
(dbo.SurfaceSampleAttr.AttributeColumn) = 'Cu_ppm') ON dbo.SurfaceSampleCoordinate.SurfaceSampleId = dbo.SurfaceSampleAttr.SurfaceSampleId
LEFT JOIN
(SELECT
dbo.SurfaceSampleAttr.AttributeValue AS 'Ars_ppm'
FROM
dbo.SurfaceSampleAttr
WHERE
(dbo.SurfaceSampleAttr.AttributeColumn) = 'Ars_ppm') ON dbo.SurfaceSampleCoordinate.SurfaceSampleId = dbo.SurfaceSampleAttr.SurfaceSampleId
LEFT JOIN
(SELECT
dbo.SurfaceSampleAttr.AttributeValue AS 'Pb_ppm'
FROM
dbo.SurfaceSampleAttr
WHERE
(dbo.SurfaceSampleAttr.AttributeColumn) = 'Pb_ppm') ON dbo.SurfaceSampleCoordinate.SurfaceSampleId = dbo.SurfaceSampleAttr.SurfaceSampleId)
WHERE
(((dbo.SurfaceSampleCoordinate.Zone) = '50')
AND ((dbo.MRTHeader.HeaderNumber) = 'H0101'));
上面的屏幕截图是dbo.SurfaceSampleAttr表,而我感兴趣的两列是.AttributeColumn和.AttributeValue。他们没有将元素作为标题,而是将它们列在一栏中。
编辑 Tried the pivot approach. A step closer, but there are a lot of Null values
答案 0 :(得分:1)
尝试在旋转之前预先选择列:
SELECT p.*
FROM (
SELECT
dbo.SurfaceSampleAttr.SampleId,
dbo.SurfaceSampleAttr.AttributeColumn,
dbo.SurfaceSampleAttr.AttributeValue
FROM dbo.SurfaceSampleAttr
) t
PIVOT (
Min(AttributeValue)
FOR AttributeColumn IN ([Au_ppb], etc)
) p
此外,如果您将MS Access用作前端,则它提供了更灵活,功能更强大的TRANSFORM ... PIVOT ...
查询,该查询明确允许在旋转时按多个列进行分组,并动态确定列名。