我使用ADO在excel中运行SQL。
我在Excel工作簿中有7个表:2014,2015,2016,2017,2018,2019,2020
现在,2014年和2015年已经填补了数据.2016已经填补了一半的数据。
2017,2018,2019,2020没有数据,因为今天仍然是2016年。
然后,我使用from ...[$2014] UNION ALL from ...[$2014] UNION ALL from ...[$2015] UNION ALL from ...[$2016]....UNION ALL from ...[$2020]
加入所有表格
后来,我发现当我查阅2016年的信息时,统计数据是完全错误的。我调试了很长时间。然后,我发现问题是2017-2020没有数据。我在2017-2020分别添加一个假行,以便解决问题。
但令人讨厌的是,需要在2017 - 2020年添加假行。
因此,任何建议都可以完全解决问题吗?
SQL (包装在VBA字符串中):
SELECT officer, NULL,
SUM(IIF( isnumeric(mkt) = true AND Survey='CPI' AND Activity='FI' AND Outcome= 'C',
Totalmin, 0 )/468),
SUM(IIF( isnumeric(Non) = true AND Survey='CPI' AND Activity='FI' AND Outcome= 'C',
Totalmin, 0 )/468),
NULL, NULL,
IIF(ISNULL(SUM(mkt)), 0, SUM(mkt)),
Sum(Non), SUM(ICP), (SUM(mkt) + SUM(Non) + SUM(ICP)),
NULL, NULL, NULL,
COUNT(IIF( Survey='CPI' AND Activity='FI',
Totalmin, NULL)),
NULL,
COUNT(IIF(Survey='CPI' AND Activity='FI' AND
(Outcome ='C' OR Outcome='D'OR Outcome='O'), Totalmin, NULL )),
NULL,
SUM(IIF(Survey='CPI' AND Activity='FI',Totalmin, 0 )),
NULL,
SUM(IIF(Survey='CPI' AND Activity='FI' AND
(Outcome ='C' OR Outcome='D'), Totalmin, 0 ))
FROM (SELECT officer, rank, year, month, day, survey, activity,
outcome, mkt, non, totalmin, ICP, tabledate FROM [2014$]
UNION ALL SELECT officer, rank, year, month, day, survey, activity,
outcome, mkt, non, totalmin, ICP, tabledate FROM [2015$]
UNION ALL SELECT officer, rank, year, month, day, survey, activity,
outcome, mkt, non, totalmin, ICP, tabledate FROM [2016$]) as table3
WHERE officer IS NOT NULL
AND officer <> '' AND officer <> ' '
AND tabledate >= # " & frommDate & "#
AND tabledate < # " & tooDate & " #
GROUP BY officer
答案 0 :(得分:0)
来自cha评论的伪代码。
declare SQLunion as string
for each worksheets
if worksheet has data then SQLunion = SQLunion + "select x,y,z from " + worksheet + " union "
next
remove the last "union" from SQLunion
SQLstring = "select a, b, c from (" + SQLunion+ ") where conditions"