我的数据库中有4个表(2014,2013,2012,2011),每个表都有相同的列CODE。我想创建一个这样的新表:
,其中 代码是:所有四个表中的所有唯一代码 和 2014年:此表中的代码为6602658的次数是多少次?
感谢您的任何建议: - )
答案 0 :(得分:0)
创建一个新查询并将其作为SQL:
SELECT DISTINCT 2014 As Year, tblCombined.Code
INTO tblNEWTABLE
FROM (SELECT Code FROM tbl2014
UNION ALL
SELECT Code FROM tbl2013
UNION ALL
SELECT Code FROM tbl2012
UNION ALL
SELECT Code FROM tbl2011) As tblCombined
其中" Code"是该字段的名称和" tbl2014"等等是表格的名称。
您可能正在寻找:
SELECT tblCombined.Code, Sum(tblCombined.C2014) As Count2014
, Sum(tblCombined.C2013) As Count2013
, Sum(tblCombined.C2012) As Count2012
, Sum(tblCombined.C2011) As Count2011
INTO tblNEWTABLE
FROM (SELECT Code, Count(Code) As C2014, 0 As C2013, 0 As C2012, 0 As C2011
FROM tbl2014 GROUP BY Code
UNION ALL
SELECT Code, 0 As C2014, Count(Code) As C2013, 0 As C2012, 0 As C2011
FROM tbl2013 GROUP BY Code
UNION ALL
SELECT Code, 0 As C2014, 0 As C2013, Count(Code) As C2012, 0 As C2011
FROM tbl2012 GROUP BY Code
UNION ALL
SELECT Code, 0 As C2014, 0 As C2013, 0 As C2012, Count(Code) As C2011
FROM tbl2011 GROUP BY Code
) As tblCombined
GROUP BY tblCombined.Code
这应该为您提供每年每个代码的记录数。 这可能不是最有效的方法,但它应该有效。
您还可以使用" CROSSTAB"查询,但这个解决方案也可以。