运行数据仓库兼容脚本时出现操作数冲突错误

时间:2018-06-12 11:42:49

标签: azure azure-sqldw

您好我收到如下错误:操作数类型冲突:日期与int不兼容。

以下是我在SQL Server上运行的查询:

CREATE TABLE val.census_last_month
WITH(
    DISTRIBUTION = ROUND_ROBIN
,   CLUSTERED COLUMNSTORE INDEX
)
 AS
  SELECT
    dt_mydate                                                                       AS dt_census,
    (SELECT count(DISTINCT encounter_id)
     FROM prod.encounter
     WHERE encounter_type = 'Inpatient' AND (ts_admit BETWEEN dt_mydate - 30 AND dt_mydate) AND
           (ts_discharge IS NULL OR ts_discharge > dt_mydate))                      AS census,
    (SELECT count(DISTINCT encounter_id)
     FROM prod.encounter
     WHERE encounter_type = 'Inpatient' AND cast(ts_admit AS DATE) = dt_mydate)     AS admits,
    (SELECT count(DISTINCT encounter_id)
     FROM prod.encounter
     WHERE encounter_type = 'Inpatient' AND cast(ts_discharge AS DATE) = dt_mydate) AS discharges
  FROM ref.calendar_day
  WHERE ref.calendar_day.dt_mydate BETWEEN (cast(getdate() as date) - 30) AND cast(getdate() as date);

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

此脚本存在多个问题,但是您是否可以确认以“ts_”开头的数据类型是以yyyyMMdd格式存储为整数的日期,而开头“dt_”的数据类型是DATE

基于这些假设,这是我的尝试改写:

SELECT dt_mydate AS dt_census,
    (
    SELECT COUNT( DISTINCT encounter_id )
    FROM prod.encounter
    WHERE encounter_type = 'Inpatient' AND ( CAST( CAST( ts_admit AS CHAR(8) ) AS DATE ) BETWEEN DATEADD( day, -30, dt_mydate ) AND dt_mydate )
    AND ( ts_discharge IS NULL OR CAST( CAST( ts_discharge AS CHAR(8) ) AS DATE ) > dt_mydate)
    ) AS census,
    (
    SELECT COUNT( DISTINCT encounter_id )
    FROM prod.encounter
    WHERE encounter_type = 'Inpatient' AND CAST( CAST( ts_admit AS CHAR(8) ) AS DATE ) = dt_mydate
    ) AS admits,
    (
    SELECT COUNT( DISTINCT encounter_id )
    FROM prod.encounter
    WHERE encounter_type = 'Inpatient' 
    AND CAST( CAST( ts_discharge AS CHAR(8) ) AS DATE ) = dt_mydate
    ) AS discharges
FROM ref.calendar_day
WHERE ref.calendar_day.dt_mydate BETWEEN CAST ( DATEADD( day, -30, GETDATE() ) AS DATE ) AND CAST( GETDATE() AS DATE );

如果我的任何一个假设不正确,请告诉我,我会更新脚本。