我收到以下错误
执行查询时“ORA-00923:找不到FROM关键字”
SELECT
count(pf.pref_selection) filter ( WHERE pc.collection = "players") OVER (partition BY pc.collection,pc.user_id ) AS Avg_players
count(pf.pref_selection) filter ( WHERE pc.collection = "teams") OVER (partition BY pc.collection,pc.user_id ) AS Avg_teams
FROM NBA_OWNER.PREFERENCES pf
inner join NBA_OWNER.PREF_COLLECTIONS pc on pc.ID=pf.COLLECTION_ID
where pc.LAST_UPDATE_DATE>"30-SEP-16'`
答案 0 :(得分:1)
您有几个错误。正如@ cricket_007指出的那样,你在两个计数表达式之间缺少一个逗号。但是你的filter
子句的语法也是无效的 - 至少在Oracle中它是不存在的;你在字符串文字周围使用双引号而不是单引号;日期字符串周围的引号甚至更奇怪;并且您依赖于日期格式的NLS设置。
如果我理解你的尝试,那么这更接近你的需要:
SELECT
count(case when pc.collection = 'players' then pf.pref_selection end)
OVER (partition BY pc.user_id) AS Avg_players,
count(case when pc.collection = 'teams' then pf.pref_selection end)
OVER (partition BY pc.user_id) AS Avg_teams
FROM NBA_OWNER.PREFERENCES pf
inner join NBA_OWNER.PREF_COLLECTIONS pc on pc.ID=pf.COLLECTION_ID
where pc.LAST_UPDATE_DATE > date '2016-09-30'
但输出可能不是很有用。对于每个用户ID,您可以获得多行,每行显示总计数(由于某种原因,您称之为avg
)。您可能只希望查看每个ID的计数一次,并查看它们与哪个ID相关,因此我认为您需要聚合而不是分析计数:
SELECT
pc.user_id,
count(case when pc.collection = 'players' then pf.pref_selection end)
AS Avg_players,
count(case when pc.collection = 'teams' then pf.pref_selection end)
AS Avg_teams
FROM NBA_OWNER.PREFERENCES pf
inner join NBA_OWNER.PREF_COLLECTIONS pc on pc.ID=pf.COLLECTION_ID
where pc.LAST_UPDATE_DATE > date '2016-09-30'
group by pc.user_id