枢?案件?我不确定如何进行

时间:2019-01-30 13:15:44

标签: sql oracle

使用Oracle DB。我正在尝试在一个列(ts.name)中获取数据,并使该数据成为列名称,并在另一列(sts.numscore)中使数据成为该列(ts.name)的数据。我正在使用CASE语句,但无法正常工作。 CASE语句每行放置一个测试分数。我需要将所有测试成绩都排成一行。任何帮助将不胜感激。谢谢

const fullName = "       Alireza Dezfoolian     ";
const trimmedFullName = fullName.trim();

console.log(trimmedFullName);

3 个答案:

答案 0 :(得分:1)

您需要使用以下条件聚合-

SELECT schools.name AS School,
  s.lastfirst AS Student,
  s.student_number,
  s.grade_level,
  t.name AS Test_Name,
  max(case when ts.name = 'ACT_Reading' then sts.numscore end) as ACT_Reading,
  max(case when ts.name = 'ACT_Math' then sts.numscore end) as ACT_Math,
  max(case when ts.name = 'ACT_English' then sts.numscore end) as ACT_English,
  max(case when ts.name = 'ACT_Science' then sts.numscore end) as ACT_Science,
  max(case when ts.name = 'ACT_Composite' then sts.numscore end) as ACT_Composite,
  to_char (st.test_date),
  sts.numscore AS Score 
FROM students s join studenttestscore sts on s.id = sts.studentid
join studenttest st on sts.studenttestid = st.id
join test t on sts.testscoreid = ts.id
join testscore ts on ts.testid = t.id
join schools on s.schoolid = schools.school_number
WHERE t.name = 'ACT' AND sts.numscore > 0 
and s.enroll_status=0 AND s.schoolid=10
group by schools.name,
  s.lastfirst ,
  s.student_number,
  s.grade_level,
  t.name, to_char (st.test_date),sts.numscore
ORDER BY s.lastfirst,st.test_date DESC

答案 1 :(得分:1)

要单行获取结果,您需要汇总case表达式的结果;像这样:

SELECT schools.name AS School,
  s.lastfirst AS Student,
  s.student_number,
  s.grade_level,
  t.name AS Test_Name,
  max(case when ts.name = 'ACT_Reading' then sts.numscore end) as ACT_Reading,
  max(case when ts.name = 'ACT_Math' then sts.numscore end) as ACT_Math,
  max(case when ts.name = 'ACT_English' then sts.numscore end) as ACT_English,
  max(case when ts.name = 'ACT_Science' then sts.numscore end) as ACT_Science,
  max(case when ts.name = 'ACT_Composite' then sts.numscore end) as ACT_Composite,
  to_char (st.test_date)
FROM students s,studenttestscore sts,studenttest st,test t,testscore ts,schools
WHERE s.id = sts.studentid
AND sts.studenttestid = st.id
AND sts.testscoreid = ts.id
AND ts.testid = t.id
AND s.schoolid = schools.school_number
AND t.name = 'ACT'
AND sts.numscore > 0 
and s.enroll_status=0
AND s.schoolid=10
GROUP BY schools.name,
  s.lastfirst,
  s.student_number,
  s.grade_level,
  t.name,
  st.test_date
ORDER BY s.lastfirst, st.test_date DESC

在有效调整分数时,您不想在选择列表或分组依据中将其作为自己的列。

最好使用现代连接语法,而不要使用from子句中用逗号分隔的古老表列表;并且您还应该在to_char()调用中提供日期的格式模型:

SELECT schools.name AS school,
  s.lastfirst AS student,
  s.student_number,
  s.grade_level,
  t.name AS test_name,
  MAX(CASE WHEN ts.NAME = 'ACT_Reading' THEN sts.numscore END) AS act_reading,
  MAX(CASE WHEN ts.NAME = 'ACT_Math' THEN sts.numscore END) AS act_math,
  MAX(CASE WHEN ts.NAME = 'ACT_English' THEN sts.numscore END) AS act_english,
  MAX(CASE WHEN ts.NAME = 'ACT_Science' THEN sts.numscore END) AS act_science,
  MAX(CASE WHEN ts.NAME = 'ACT_Composite' THEN sts.numscore END) AS act_composite,
  to_char(st.test_date, 'YYYY-MM-DD') AS test_date
FROM students s
JOIN studenttestscore sts ON s.id = sts.studentid
JOIN studenttest st ON sts.studenttestid = st.id
JOIN testscore ts ON sts.testscoreid = ts.id
JOIN test t ON ts.testid = t.id
JOIN schools ON s.schoolid = schools.school_number
WHERE t.name = 'ACT'
AND sts.numscore > 0 
and s.enroll_status=0
AND s.schoolid=10
GROUP BY schools.name,
  s.lastfirst,
  s.student_number,
  s.grade_level,
  t.name,
  st.test_date
ORDER BY s.lastfirst, st.test_date DESC

使用pivot的等效项将类似于:

SELECT school, student, student_number, grade_level, test_name,
  act_reading, act_math, act_english, act_science, act_composite,
  to_char(test_date, 'YYYY-MM-DD') AS test_date
FROM (
  SELECT schools.name AS school,
    s.lastfirst AS student,
    s.student_number,
    s.grade_level,
    t.name AS test_name,
    ts.name AS test_score_name,
    sts.numscore,
    st.test_date
  FROM students s
  JOIN studenttestscore sts ON s.id = sts.studentid
  JOIN studenttest st ON sts.studenttestid = st.id
  JOIN testscore ts ON sts.testscoreid = ts.id
  JOIN test t ON ts.testid = t.id
  JOIN schools ON s.schoolid = schools.school_number
  WHERE t.name = 'ACT'
  AND sts.numscore > 0 
  AND s.enroll_status=0
  AND s.schoolid=10
)
PIVOT (
  max(numscore)
  FOR test_score_name IN (
    'ACT_Reading' AS act_reading,
    'ACT_Math' AS act_math,
    'ACT_English' AS act_english,
    'ACT_Science' AS act_science,
    'ACT_Composite' AS act_composite
  )
) p
ORDER BY p.student, p.test_date DESC

,但无论如何它都将转换为幕后的汇总/案例版本。

(当然,所有这些都未经测试,因为我们没有适合您使用的架构...)

答案 2 :(得分:1)

您处在正确的轨道上,而您尝试编写的是透视查询。这是更正的版本。它需要每个CASE表达式的 max 来得出所需的单行值。此外,它在所有表之间使用适当的显式联接。这是编写现代SQL查询的首选方式。

SELECT
    sc.name AS School,
    s.lastfirst AS Student,
    s.student_number,
    s.grade_level,
    t.name AS Test_Name,
    MAX(CASE WHEN ts.name = 'ACT_Reading'   THEN sts.numscore end) AS ACT_Reading,
    MAX(CASE WHEN ts.name = 'ACT_Math'      THEN sts.numscore end) AS ACT_Math,
    MAX(CASE WHEN ts.name = 'ACT_English'   THEN sts.numscore end) AS ACT_English,
    MAX(CASE WHEN ts.name = 'ACT_Science'   THEN sts.numscore end) AS ACT_Science,
    MAX(CASE WHEN ts.name = 'ACT_Composite' THEN sts.numscore end) AS ACT_Composite,
    TO_CHAR(st.test_date),
    sts.numscore AS Score
FROM students s
INNER JOIN studenttestscore sts
    ON s.id = sts.studentid
INNER JOIN studenttest st
    ON sts.studenttestid = st.id
INNER JOIN test score ts
    ON sts.testscoreid = ts.id
INNER JOIN test t
    ON ts.testid = t.id
INNER JOIN schools sc
    ON s.schoolid = sc.school_number
WHERE
    t.name = 'ACT' AND
    sts.numscore > 0 AND
    s.enroll_status = 0 AND
    s.schoolid = 10
GROUP BY
    sc.name,
    s.lastfirst,
    s.student_number,
    s.grade_level,
    t.name,
    st.test_date,
    sts.numscore
ORDER BY
    s.lastfirst,
    st.test_date DESC;