我有以下查询,它获取每个月从三个不同表中获取数据的总数:
--DROP TABLE [database].[dbo].[finaltable]
--TRUNCATE TABLE [database].[dbo].[finaltable]
;WITH
Ranges AS (
SELECT 'January' AS M, '1/1/2014' AS [START], '1/31/2014' AS [END]
UNION ALL
SELECT 'February', '2/1/2014', '2/28/2014'
UNION ALL
SELECT 'March', '3/1/2014', '3/31/2014'
UNION ALL
SELECT 'April', '4/1/2014', '4/30/2014'
UNION ALL
SELECT 'May', '5/1/2014', '5/31/2014'
UNION ALL
SELECT 'June', '6/1/2014', '6/30/2014'
UNION ALL
SELECT 'July', '7/1/2014', '7/31/2014'
UNION ALL
SELECT 'August', '8/1/2014', '8/31/2014'
UNION ALL
SELECT 'September', '9/1/2014', '9/30/2014'
UNION ALL
SELECT 'October', '10/1/2014', '10/31/2014'
UNION ALL
SELECT 'November', '11/1/2014', '11/30/2014'
UNION ALL
SELECT 'December', '12/1/2014', '12/31/2014'
),
Data AS (
SELECT [Date], [APSURGICALTOTAL], [APDERMTOTAL], [GYNNONGYNTOTAL], [CYTOHPV], [INHOUSE], [HEMOTOLOGY], [CHEMISTRY], [BLOODBANK], [REFLAB], [URINALYSIS], [SEROLOGY], [MICROBIOLOGY]
FROM [database].[dbo].[weeklytable]
UNION ALL
SELECT CONVERT(VARCHAR(10), [Date], 101), [LastWeekEMROrders], [LastWeekAccLabOrders], [LastWeekLabResults], NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
FROM [database].[dbo].[dailytable]
UNION ALL
SELECT CONVERT(VARCHAR(10), [Date], 101), [RAD EMR ORDERS], [EMR ORDERS], [ACCESSIONED LAB ORDERS], [RAD RESULTED], [LAB_RESULT], [RAD ACCESSIONED], [PENDING FOR PROCESSING], NULL, NULL, NULL, NULL, NULL
FROM [database].[dbo].[othertable]
),
Joined AS (
SELECT M AS [Month],
[APSURGICALTOTAL],
[APDERMTOTAL],
[GYNNONGYNTOTAL],
[CYTOHPV],
[INHOUSE],
[HEMOTOLOGY],
[CHEMISTRY],
[BLOODBANK],
[REFLAB],
[URINALYSIS],
[SEROLOGY],
[MICROBIOLOGY],
[LastWeekEMROrders],
[LastWeekAccLabOrders],
[LastWeekLabResults],
[RAD EMR ORDERS],
[EMR ORDERS],
[ACCESSIONED LAB ORDERS],
[RAD RESULTED],
[LAB_RESULT],
[RAD ACCESSIONED],
[PENDING FOR PROCESSING]
FROM Ranges R
JOIN Data D ON D.[DATE] >= R.[START] AND D.[Date] <= R.[END]
)
SELECT [Month],
SUM([APSURGICALTOTAL]) AS [SURGTOTAL],
SUM([APDERMTOTAL]) AS [DERMTOTAL],
SUM(GYNNONGYNTOTAL) AS [GNGTOTAL],
SUM(CYTOHPV) AS [HPVTOTAL],
SUM(INHOUSE) AS [HOUSETOTAL],
SUM([HEMOTOLOGY]) AS [HEMOTOTAL],
SUM([CHEMISTRY]) AS [CHEMTOTAL],
SUM([BLOODBANK]) AS [BBTOTAL],
SUM([REFLAB]) AS [REFTOTAL],
SUM([URINALYSIS]) AS [URINTOTAL],
SUM([SEROLOGY]) AS [SEROTOTAL],
SUM([MICROBIOLOGY]) AS [MICROTOTAL],
SUM([LastWeekEMROrders]) AS [LWEMRORDERSTOTAL],
SUM([LastWeekAccLabOrders]) AS [LWACCLABORDERSTOTAL],
SUM([LastWeekLabResults]) AS [LWLABRESULTSTOTAL],
SUM([RAD EMR ORDERS]) AS [RADEMRTOTAL],
SUM([EMR ORDERS]) AS [EMRORDERTOTAL],
SUM([ACCESSIONED LAB ORDERS]) AS [ACCLABORDERSTOTAL],
SUM([RAD RESULTED]) AS [RADRESTOTAL],
SUM([LAB_RESULT]) AS [LABREDTOTAL],
SUM([RAD ACCESSIONED]) AS [RADACCTOTAL],
SUM([PENDING FOR PROCESSING]) AS [PENFORPROCTOTAL],
month([Month]+'1 2010') AS [MonthNum]
INTO [database].[dbo].[finaltable]
FROM Joined
GROUP BY [Month]
我收到以下错误:
Msg 207, Level 16, State 1, Line 56
Invalid column name 'LastWeekEMROrders'.
Msg 207, Level 16, State 1, Line 57
Invalid column name 'LastWeekAccLabOrders'.
Msg 207, Level 16, State 1, Line 58
Invalid column name 'LastWeekLabResults'.
Msg 207, Level 16, State 1, Line 59
Invalid column name 'RAD EMR ORDERS'.
Msg 207, Level 16, State 1, Line 60
Invalid column name 'EMR ORDERS'.
Msg 207, Level 16, State 1, Line 61
Invalid column name 'ACCESSIONED LAB ORDERS'.
Msg 207, Level 16, State 1, Line 62
Invalid column name 'RAD RESULTED'.
Msg 207, Level 16, State 1, Line 63
Invalid column name 'LAB_RESULT'.
Msg 207, Level 16, State 1, Line 64
Invalid column name 'RAD ACCESSIONED'.
Msg 207, Level 16, State 1, Line 65
Invalid column name 'PENDING FOR PROCESSING'.
我在这里用虚拟表格问我原来的问题:Stackoverflow Question Link
很多人帮助我使用虚拟桌子。现在我试图用我真实的表做同样的事情,但它给了我上面列出的所有错误。
如何解决错误,所以它就像使用虚拟表一样工作?
答案 0 :(得分:2)
问题出在这一部分:
SELECT [Date], [APSURGICALTOTAL], [APDERMTOTAL], [GYNNONGYNTOTAL], [CYTOHPV], [INHOUSE], [HEMOTOLOGY], [CHEMISTRY], [BLOODBANK], [REFLAB], [URINALYSIS], [SEROLOGY], [MICROBIOLOGY]
FROM [database].[dbo].[weeklytable]
UNION ALL
SELECT CONVERT(VARCHAR(10), [Date], 101), [LastWeekEMROrders], [LastWeekAccLabOrders], [LastWeekLabResults], NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
FROM [database].[dbo].[dailytable]
UNION ALL
SELECT CONVERT(VARCHAR(10), [Date], 101), [RAD EMR ORDERS], [EMR ORDERS], [ACCESSIONED LAB ORDERS], [RAD RESULTED], [LAB_RESULT], [RAD ACCESSIONED], [PENDING FOR PROCESSING], NULL, NULL, NULL, NULL, NULL
FROM [database].[dbo].[othertable]
问题列未显示在原始选择中。当您使用UNION ALL时,第一个选择的列应与以下查询匹配。
答案 1 :(得分:1)
您需要确保UNION ALL语句中的列“排队”。更容易看出是否将列名放在所有列中 - 如下所示:
您需要确保日期列是日期或日期时间类型。如果您存储字符串然后转换它们 - 尝试对字符串进行日期比较是一个糟糕的计划 - 使用日期并让服务器将苹果与苹果进行比较。
Data AS (
SELECT
CAST([Date] AS DateTime) AS [DATE],
[APSURGICALTOTAL],
[APDERMTOTAL],
[GYNNONGYNTOTAL],
[CYTOHPV],
[INHOUSE],
[HEMOTOLOGY],
[CHEMISTRY],
[BLOODBANK],
[REFLAB],
[URINALYSIS],
[SEROLOGY],
[MICROBIOLOGY],
NULL AS [LastWeekEMROrders],
NULL AS [LastWeekAccLabOrders],
NULL AS [LastWeekLabResults],
NULL AS [RAD EMR ORDERS],
NULL AS [EMR ORDERS],
NULL AS [ACCESSIONED LAB ORDERS],
NULL AS [RAD RESULTED],
NULL AS [LAB_RESULT],
NULL AS [RAD ACCESSIONED],
NULL AS [PENDING FOR PROCESSING]
FROM [database].[dbo].[weeklytable]
UNION ALL
SELECT
[Date],
NULL AS [APSURGICALTOTAL],
NULL AS [APDERMTOTAL],
NULL AS [GYNNONGYNTOTAL],
NULL AS [CYTOHPV],
NULL AS [INHOUSE],
NULL AS [HEMOTOLOGY],
NULL AS [CHEMISTRY],
NULL AS [BLOODBANK],
NULL AS [REFLAB],
NULL AS [URINALYSIS],
NULL AS [SEROLOGY],
NULL AS [MICROBIOLOGY],
[LastWeekEMROrders],
[LastWeekAccLabOrders],
[LastWeekLabResults],
NULL AS [RAD EMR ORDERS],
NULL AS [EMR ORDERS],
NULL AS [ACCESSIONED LAB ORDERS],
NULL AS [RAD RESULTED],
NULL AS [LAB_RESULT],
NULL AS [RAD ACCESSIONED],
NULL AS [PENDING FOR PROCESSING]
FROM [database].[dbo].[dailytable]
UNION ALL
SELECT
[Date],
NULL AS [APSURGICALTOTAL],
NULL AS [APDERMTOTAL],
NULL AS [GYNNONGYNTOTAL],
NULL AS [CYTOHPV],
NULL AS [INHOUSE],
NULL AS [HEMOTOLOGY],
NULL AS [CHEMISTRY],
NULL AS [BLOODBANK],
NULL AS [REFLAB],
NULL AS [URINALYSIS],
NULL AS [SEROLOGY],
NULL AS [MICROBIOLOGY],
NULL AS [LastWeekEMROrders],
NULL AS [LastWeekAccLabOrders],
NULL AS [LastWeekLabResults],
[RAD EMR ORDERS],
[EMR ORDERS],
[ACCESSIONED LAB ORDERS],
[RAD RESULTED],
[LAB_RESULT],
[RAD ACCESSIONED],
[PENDING FOR PROCESSING]
FROM [database].[dbo].[othertable]
),