【问题标题】:Sql query join gives invalid parameter types errorSql 查询连接给出无效的参数类型错误
【发布时间】:2020-09-04 05:17:02
【问题描述】:

我正在尝试通过将联接更改为在 study 和 id 和 datedate -1 上来转换我的查询(在一个表上是正常数据,在另一个表中是 date-1),但我我犯了一些错误,这给了我一个错误。

数据库:Oracle 并在 Denodo Server 上运行

外科医生

study id        cal_dtm    total
RSCLS CA10001  2020-08-11    52
RSCLS CA10001  2020-08-10    52
ETDLD CA20302  2020-08-11    99
ERGKG CA34524  2020-08-11    31

查询:

  select
  tt1.study,
  tt1.id,
  tt1.cal_dtm,
  tt1.total,
  tt1.total-coalesce(tt2.total, 0) as delta
  from pedics tt1
  left outer JOIN pedics tt2 on tt1.total = tt2.total
    and extract(month from tt1.cal_dtm)-extract(month from tt2.cal_dtm)=1

使用引发错误所需的条件进行查询:

select
  tt1.study,
  tt1.id,
  tt1.cal_dtm,
  tt1.total,
  (tt1.total-coalesce(tt2.total 0)) as delta
  from pedics tt1
  left outer JOIN pedics tt2 on tt1.study_name = tt2.study_name and tt1.site_id = tt2.site_id
  and extract(month from tt1.cal_dtm)-extract(month from tt2.cal_dtm-1)

错误: 连接视图条件错误:函数'-(tt2.cal_dtm, '1')'的参数类型无效

【问题讨论】:

  • 你的数据库是什么?
  • Oracle 并在 denodo 服务器上运行。
  • 在您的查询中,您使用以下列:name、study_name、site_id,而在您的数据示例中没有这样的列?
  • 我从没听说过 Denodo,但也许用 months_between() 表达会更开心。
  • denodo 使用 vql 脚本

标签: sql oracle denodo


【解决方案1】:

如果您要查找该行中的当天与前一天之间的增量,则使用 LAG 分析函数的效率要高得多。这样您就可以只查看前一行的“总计”并找出当前行总计之间的差异。

设置

我添加了比您提供的更多的示例数据以显示更多案例。

CREATE TABLE pedics
(
    study      VARCHAR2 (5),
    id         VARCHAR2 (7),
    cal_dtm    DATE,
    total      NUMBER
);

INSERT INTO pedics (study, id, cal_dtm, total) VALUES ('RSCLS', 'CA10001', DATE '2020-08-11', 52);
INSERT INTO pedics (study, id, cal_dtm, total) VALUES ('RSCLS', 'CA10001', DATE '2020-08-10', 52);
INSERT INTO pedics (study, id, cal_dtm, total) VALUES ('RSCLS', 'CA10001', DATE '2020-08-09', 50);
INSERT INTO pedics (study, id, cal_dtm, total) VALUES ('ETDLD', 'CA20302', DATE '2020-08-11', 99);
INSERT INTO pedics (study, id, cal_dtm, total) VALUES ('ERGKG', 'CA34524', DATE '2020-08-11', 31);
INSERT INTO pedics (study, id, cal_dtm, total) VALUES ('ERGKG', 'CA34524', DATE '2020-08-12', 35);
INSERT INTO pedics (study, id, cal_dtm, total) VALUES ('ERGKG', 'CA34524', DATE '2020-08-13', 26);

查询

  SELECT p.*, total - LAG (total) OVER (PARTITION BY study, id ORDER BY cal_dtm) AS delta
    FROM pedics p
ORDER BY study, id, cal_dtm;

结果

STUDY | ID      | CAL_DTM   | TOTAL | DELTA
--------------------------------------------
ERGKG | CA34524 | 8/11/2020 |    31 | 
ERGKG | CA34524 | 8/12/2020 |    35 | 4
ERGKG | CA34524 | 8/13/2020 |    26 | -9
ETDLD | CA20302 | 8/11/2020 |    99 | 
RSCLS | CA10001 | 8/9/2020  |    50 | 
RSCLS | CA10001 | 8/10/2020 |    52 | 2
RSCLS | CA10001 | 8/11/2020 |    52 | 0

【讨论】:

  • 我不应该添加 LAG (total, 1, 0) 吗?
  • 如果不指定偏移量,默认使用 1,这样不会改变任何结果。指定 0 将导致出现 0,即使没有每个研究/ID 组合的先前记录。如果这满足您的业务需求,那么一定要去做!
猜你喜欢
  • 1970-01-01
  • 2019-11-21
  • 1970-01-01
  • 2013-04-07
  • 2019-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多