我有三个具有相同列名的表:[PARTNO],[DATETIME]等,我创建了一个视图来组合所有三个:
SELECT *
FROM SPC.dbo.VDATA
UNION
SELECT *
FROM SPC_Archive.dbo.VDATA
UNION
SELECT *
FROM SPC.dbo.VDATA1
UNION
SELECT *
FROM SPC_Archive.dbo.VDATA1
UNION SELECT *
FROM SPC.dbo.VDATA2
UNION
SELECT *
FROM SPC_Archive.dbo.VDATA2
我的问题遇到的是我需要它只返回[PARTNO]和[DATETIME]的一个值,我不知道如何在联盟上做到这一点....
答案 0 :(得分:0)
听起来好像你在执行UNION之后尝试在PARTNO上进行GROUP。您可以使用嵌套查询。例如,如果您尝试在所有表中找到每个PARTNO的最大DATETIME,您可以:
SELECT PARTNO, MAX([DATETIME])
FROM (
SELECT PARTNO, [DATETIME] FROM SPC.dbo.VDATA
UNION SELECT PARTNO, [DATETIME] FROM SPC_Archive.dbo.VDATA
UNION SELECT ...
UNION SELECT ...
) combined
GROUP BY combined.PARTNO