我有一个查询,其中包含每月添加的数据,但它给我的SQL模糊错误,我不知道该怎么做。我到处研究过,无法找到问题的答案,所以就这样了!
这是查询:
SELECT REF.TOS AS [TOS Value]
,RSA_BELFAST_1921.Percentage AS [Belfast]
,RSA_BIRMINGHAM_1941$.Percentage AS [Birmingham]
,RSA_CARDIFFREGUS_2911$.Percentage AS [Cardiff Regus]
,RSA_CASTLEMEAD_1941$.Percentage AS [Castlemead]
,RSA_CHELMSFORD_1941$.Percentage AS [Chelmsford]
,RSA_EDNBGH_2951$.Percentage AS [Edimburgh]
,RSA_EUROPE_IOM_DOUG_2911$.Percentage AS [Europe]
,RSA_GLASGOW_7301$.Percentage AS [Glasgow]
,RSA_GUERNSEY_ICICI_1921P$.Percentage AS [Guernsey]
,RSA_HALIFAX_7301$.Percentage AS [Halifax]
,RSA_HORSHAM_1002$.Percentage AS [Horsham]
,RSA_IPSWICH_1921$.Percentage AS [Ipswich]
,RSA_LEEDS_2911$.Percentage AS [Leeds]
,RSA_LEICESTER_2951$.Percentage AS [Leicester]
,RSA_LIVERPOOL_1002$.Percentage AS [Liverpool]
,RSA_MANCHESTER_1941$.Percentage AS [Manchester]
,RSA_ONELONDON_1002.Percentage AS [London]
,RSA_PETERBOROUGH_3825.Percentage AS [Peterborough]
,RSA_REDHILL_2951.Percentage AS [Redhill]
,RSA_SUNDERLAND_1941$.Percentage AS [Sunderland]
FROM ( SELECT TOS FROM RSA_BELFAST_1921 UNION
SELECT TOS FROM RSA_BIRMINGHAM_1941$ UNION
SELECT TOS FROM RSA_CARDIFFREGUS_2911$ UNION
SELECT TOS FROM RSA_CASTLEMEAD_1941$ UNION
SELECT TOS FROM RSA_CHELMSFORD_1941$ UNION
SELECT TOS FROM RSA_EDNBGH_2951$ UNION
SELECT TOS FROM RSA_EUROPE_IOM_DOUG_2911$ UNION
SELECT TOS FROM RSA_GLASGOW_7301$ UNION
SELECT TOS FROM RSA_GUERNSEY_ICICI_1921P$ UNION
SELECT TOS FROM RSA_HALIFAX_7301$ UNION
SELECT TOS FROM RSA_HORSHAM_1002$ UNION
SELECT TOS FROM RSA_IPSWICH_1921$ UNION
SELECT TOS FROM RSA_LEEDS_2911$ UNION
SELECT TOS FROM RSA_LEICESTER_2951$ UNION
SELECT TOS FROM RSA_LIVERPOOL_1002$ UNION
SELECT TOS FROM RSA_MANCHESTER_1941$ UNION
SELECT TOS FROM RSA_ONELONDON_1002 UNION
SELECT TOS FROM RSA_PETERBOROUGH_3825 UNION
SELECT TOS FROM RSA_REDHILL_2951 UNION
SELECT TOS FROM RSA_SUNDERLAND_1941$
) AS REF
FULL OUTER JOIN RSA_BELFAST_1921 ON REF.TOS = RSA_BELFAST_1921.TOS
FULL OUTER JOIN RSA_BIRMINGHAM_1941$ ON REF.TOS = RSA_BIRMINGHAM_1941$.TOS
FULL OUTER JOIN RSA_CARDIFFREGUS_2911$ ON REF.TOS = RSA_CARDIFFREGUS_2911$.TOS
FULL OUTER JOIN RSA_CASTLEMEAD_1941$ ON REF.TOS = RSA_CASTLEMEAD_1941$.TOS
FULL OUTER JOIN RSA_CHELMSFORD_1941$ ON REF.TOS = RSA_CHELMSFORD_1941$.TOS
FULL OUTER JOIN RSA_EDNBGH_2951$ ON REF.TOS = RSA_EDNBGH_2951$.TOS
FULL OUTER JOIN RSA_EUROPE_IOM_DOUG_2911$ ON REF.TOS = RSA_EUROPE_IOM_DOUG_2911$.TOS
FULL OUTER JOIN RSA_GLASGOW_7301$ ON REF.TOS = RSA_GLASGOW_7301$.TOS
FULL OUTER JOIN RSA_GUERNSEY_ICICI_1921P$ ON REF.TOS = RSA_GUERNSEY_ICICI_1921P$.TOS
FULL OUTER JOIN RSA_HALIFAX_7301$ ON REF.TOS = RSA_HALIFAX_7301$.TOS
FULL OUTER JOIN RSA_HORSHAM_1002$ ON REF.TOS = RSA_HORSHAM_1002$.TOS
FULL OUTER JOIN RSA_IPSWICH_1921$ ON REF.TOS = RSA_IPSWICH_1921$.TOS
FULL OUTER JOIN RSA_LEEDS_2911$ ON REF.TOS = RSA_LEEDS_2911$.TOS
FULL OUTER JOIN RSA_LEICESTER_2951$ ON REF.TOS = RSA_LEICESTER_2951$.TOS
FULL OUTER JOIN RSA_LIVERPOOL_1002$ ON REF.TOS = RSA_LIVERPOOL_1002$.TOS
FULL OUTER JOIN RSA_MANCHESTER_1941$ ON REF.TOS = RSA_MANCHESTER_1941$.TOS
FULL OUTER JOIN RSA_ONELONDON_1002 ON REF.TOS = RSA_ONELONDON_1002.TOS
FULL OUTER JOIN RSA_PETERBOROUGH_3825 ON REF.TOS = RSA_PETERBOROUGH_3825.TOS
FULL OUTER JOIN RSA_REDHILL_2951 ON REF.TOS = RSA_REDHILL_2951.TOS
FULL OUTER JOIN RSA_SUNDERLAND_1941$ ON REF.TOS = RSA_SUNDERLAND_1941$.TOS
WHERE REF.TOS = 'ef (DSCP 46)' and [Report Date] = 'April 2016'
ORDER BY [TOS Value]
我们上个月开始这样做了,我们只有1个月的数据,所以查询运行良好并产生了预期的结果:
TOS Value Belfast Birmingham Report Date
ef (DSCP 46) 0.14% 1.91% March 2016
但是现在我们已经添加了4月数据,我们得到了模棱两可的错误!
请帮忙!我的查询有什么问题!? 另外....现在我已经添加了第二个月的数据,如果我运行没有每月“WHERE”参数的查询,那么当它应该只有1行时,查询会生成52k行数据!答案 0 :(得分:5)
据推测,您需要在所有联接中包含[Report Date]
。否则,您将获得笛卡尔积,结果将不正确。
完成此操作后,您将需要以下逻辑:
COALESCE(t1.[Report Date], t2.[Report Date], . . .) = 'April 2016'
我会发表一些意见:
full outer join
的广泛使用也表明基础数据结构存在问题。