我正在尝试帮助同事在两个oracle表上进行内部联接,以便他可以在报表上构建特定的图表。
我没有Oracle经验,只有SQL Server并且已经得到了合适的声明,但是没有用。
SELECT concat(concat(month("a.timestamp"),','),day("a.timestamp")) as monthDay
, min("a.data_value") as minTemp
, max("a.data_value") as maxTemp
, "b.forecast" as forecastTemp
, "a.timestamp" as date
FROM table1 a
WHERE "a.category" = 'temperature'
GROUP BY concat(concat(month("timestamp"),','),day("timestamp"))
INNER JOIN (SELECT "forecast"
, "timestamp"
FROM table2
WHERE "category" = 'temperature') b
ON "a.timestamp" = "b.timestamp"
出于某种原因,它不喜欢我的别名。由于某种原因,它不喜欢没有引号。
当我使用完全得分的名字时,它仍然失败,因为:
ORA-00933 SQL command not properly ended
答案 0 :(得分:1)
查询的顺序应为
SELECT
FROM
INNER JOIN
WHERE
GROUP BY
如下
SELECT concat(concat(month("a.timestamp"),','),day("a.timestamp")) as monthDay
, min("a.data_value") as minTemp
, max("a.data_value") as maxTemp
, "b.forecast" as forecastTemp
, "a.timestamp" as date
FROM table1 a
INNER JOIN (SELECT "forecast"
, "timestamp"
FROM table2
WHERE "category" = 'temperature') b
ON "a.timestamp" = "b.timestamp"
WHERE "category" = 'temperature'
GROUP BY concat(concat(month("timestamp"),','),day("timestamp"))
答案 1 :(得分:1)
在大量的尝试中,这是另一个尝试。
a.timestamp
包含的信息不仅仅是月和日 - 如果是这样的话,它可能会破坏整个结果集,因为数据不会按 monthday <分组/ em>,但是整个日期 - 如果有必要,请考虑将其从SELECT中删除SELECT TO_CHAR(a.timestamp,'mm.dd') monthday,
MIN(a.data_value) mintemp,
MAX(a.data_value) maxtemp,
b.forecast forecasttemp,
a.timestamp c_date
FROM table1 a
JOIN table2 b ON a.timestamp = b.timestamp
AND a.category = b.category
WHERE a.category = 'temperature'
GROUP BY TO_CHAR(a.timestamp,'mm.dd'),
b.forecast,
a.timestamp;
答案 2 :(得分:0)
select的正确(简化)语法是
SELECT <columns>
FROM table1 <alias>
JOIN table2 <alias> <join_condition>
WHERE <condition>
GROUP BY <group by columns>
答案 3 :(得分:0)
你做错了。使用子查询:
SELECT c.*, b.`forecast` as forecastTemp
FROM
(SELECT concat(concat(month(a.`timestamp`),','),day(a.`timestamp`)) as monthDay
, min(a.`data_value`) as minTemp
, max(a.`data_value`) as maxTemp
, a.`timestamp` as date
FROM table1 a
WHERE `category`='temperature'
GROUP BY concat(concat(month(`timestamp`),','),day(`timestamp`))) c
INNER JOIN (SELECT `forecast`
, `timestamp`
FROM table2
WHERE `category` = 'temperature') b
ON c.`timestamp` = b.`timestamp`;
答案 4 :(得分:0)
查询应如下所示:
SELECT to_char(a.timestamp, 'MM-DD') as monthDay,
min(a.data_value) as minTemp,
max(a.data_value) as maxTemp,
b.forecast as forecastTemp
FROM table1 a JOIN
table2 b
ON a.timestamp = b.timestamp and b.category = 'temperature'
WHERE a.category = 'temperature'
GROUP BY to_char(timestamp, 'MM-DD'), b.forecast;
我不是100%确定这是你想要的。您的查询有很多问题和复杂性:
FROM
子句中不需要子查询。to_char()
代替更复杂的日期字符串处理。group by
未包含所有相关字段。答案 5 :(得分:0)
除了其他答案提到的组件的顺序(where
等于join
之后),您还需要删除所有双引号字符。在Oracle中,这些覆盖了标准命名规则,因此"a.category"
仅在您的表实际上有一个名为"a.category"
的列,例如
create table demo ("a.category" varchar2(10));
insert into demo ("a.category") values ('Weird');
select d."a.category" from demo d;
很少需要这样做。