我有这是Oracle数据库中的表:
1-用户表
╔════════╦═════════╗
║ UserID ║ Name ║
╠════════╬═════════╣
║ 12345 ║ Scott ║
╠════════╬═════════╣
║ 54321 ║ Jeffry ║
╠════════╬═════════╣
║ 98765 ║ Larry ║
╚════════╩═════════╝
2- ScoreTypes表
╔══════════════════╦════════════╗
║ ScoreTypeID (PK) ║ Title ║
╠══════════════════╬════════════╣
║ 1 ║ Football ║
╠══════════════════╬════════════╣
║ 2 ║ Volleyball ║
╠══════════════════╬════════════╣
║ 3 ║ Basketball ║
╚══════════════════╩════════════╝
3- UserResults
╔═════════════╦══════════════════╦══════════╗
║ UserID (FK) ║ ScoreTypeID (FK) ║ ScoreNum ║
╠═════════════╬══════════════════╬══════════╣
║ 12345 ║ 1 ║ 89 ║
╠═════════════╬══════════════════╬══════════╣
║ 12345 ║ 2 ║ 45 ║
╠═════════════╬══════════════════╬══════════╣
║ 54321 ║ 1 ║ 95 ║
╠═════════════╬══════════════════╬══════════╣
║ 54321 ║ 2 ║ 60 ║
╠═════════════╬══════════════════╬══════════╣
║ 98765 ║ 1 ║ 100 ║
╠═════════════╬══════════════════╬══════════╣
║ 98765 ║ 3 ║ 35 ║
╚═════════════╩══════════════════╩══════════╝
4- ScorePolicyPasses表
╔═════════╦══════════════════╦═══════════╗
║ ID (PK) ║ ScoreTypeID (FK) ║ PassScore ║
╠═════════╬══════════════════╬═══════════╣
║ 1 ║ 1 ║ 90 ║
╠═════════╬══════════════════╬═══════════╣
║ 2 ║ 2 ║ 50 ║
╚═════════╩══════════════════╩═══════════╝
有什么想法?
表格中的用户可能有“UserResults”的记录 它包含一个列,用于显示在“ScoreTypes”表中特定游戏中获得的得分数。 每个游戏都有特定于表格“ScorePolicyPasses”中的Win的得分传递
我想写一个查询 返回在“UserResults表”中具有记录的用户
但是,只有那些没有超出“ScorePolicyPasses表”中政策的人
结果SQL查询:
╔══════════════╦════════════╦══════════════════════════════╗
║ Users.UserID ║ Users.Name ║ Comment ║
╠══════════════╬════════════╬══════════════════════════════╣
║ 12345 ║ Scott ║ Football: 89, Volleyball: 45 ║
╚══════════════╩════════════╩══════════════════════════════╝
只有一位用户未达到以前提出的政策
的结果Soctt在足球比赛中得分为90分,但是在排球比赛中45分< 50 也是,如果是足球< 90和排球< 50没有实现
杰弗里实现了理想的足球> 90和排球> 50 拉里取得了理想的足球> 90 虽然拉里在表“UserResults”中有篮球记录 但篮球在“ScorePolicyPasses表”中没有政策, 因此,将被忽略。
是否可以通过SQL Only或PLSQL编写查询?怎么样?
答案 0 :(得分:5)
Oracle安装程序:
CREATE TABLE Users ( UserID, Name ) AS
SELECT 12345, 'Scott' FROM DUAL UNION ALL
SELECT 54321, 'Jeffry' FROM DUAL UNION ALL
SELECT 98765, 'Larry' FROM DUAL;
CREATE TABLE ScoreTypes ( ScoreTypeID, Title ) AS
SELECT 1, 'Football' FROM DUAL UNION ALL
SELECT 2, 'Volleyball' FROM DUAL UNION ALL
SELECT 3, 'Basketball' FROM DUAL;
CREATE TABLE UserResults ( UserID, ScoreTypeID, ScoreNum ) AS
SELECT 12345, 1, 89 FROM DUAL UNION ALL
SELECT 12345, 2, 45 FROM DUAL UNION ALL
SELECT 54321, 1, 95 FROM DUAL UNION ALL
SELECT 54321, 2, 60 FROM DUAL UNION ALL
SELECT 98765, 1, 100 FROM DUAL UNION ALL
SELECT 98765, 3, 35 FROM DUAL;
CREATE TABLE ScorePolicyPasses ( ID, ScoreTypeID, PassScore ) AS
SELECT 1, 1, 90 FROM DUAL UNION ALL
SELECT 2, 2, 50 FROM DUAL;
<强>查询强>:
SELECT ur.UserID AS "Users.UserID",
MAX( Name ) AS "Users.Name",
LISTAGG( Title || ': ' || ScoreNum, ', ' )
WITHIN GROUP ( ORDER BY ur.ScoreTypeID ) AS "Comment"
FROM UserResults ur
INNER JOIN Users u
ON ( ur.UserId = u.UserID )
INNER JOIN ScoreTypes s
ON ( ur.ScoreTypeID = s.ScoreTypeID )
INNER JOIN ScorePolicyPasses p
ON ( ur.ScoreTypeID = p.ScoreTypeID )
GROUP BY ur.UserID
HAVING COUNT( CASE WHEN ScoreNum < PassScore THEN 1 END ) > 0;
<强>输出强>:
Users.UserID Users.Name Comment
------------ ---------- ----------------------------------------------------
12345 Scott Football: 89, Volleyball: 45