我在Postgres
上运行以下查询:
SELECT a.testscoreid,
COUNT(a.*) AS Questions,
a.created,
ROW_NUMBER() OVER (PARTITION BY Questions) AS Number,
b.irt_tlevel
FROM asmt.testscores a
JOIN asmt.questions b ON a.questionid = b.questionid
WHERE a.answered = TRUE
AND a.testscoreid = '53bd6ed5da2cfe7baf0d97ed'
AND irt_tlevel IS NOT NULL
GROUP BY a.testscoreid,
a.created,
b.irt_tlevel
ORDER BY Questions DESC,
a.created ASC
给了我以下结果:
Testscoreid Questions Created Number irt_tlevel
53bd6ed5da2cfe7baf0d97ed 272 2014-07-09 09:33:25.0 1 easy
53bd6ed5da2cfe7baf0d97ed 38 2014-07-09 09:33:25.0 1 hard
53bd6ed5da2cfe7baf0d97ed 34 2014-07-09 09:33:25.0 1 medium
对于所有Number
,我希望每次都有questionid
列增量。我想我在row_number()
中遗漏了一些东西。
我希望所有questionid的列数增加。例如 - 我希望在上面的例子中有272行(增加数字)以便于提问,硬行为38行,中等行为34行。总的来说,我希望有(272 + 38 + 34)行,Number
按Created
递增。
任何建议都将不胜感激。
答案 0 :(得分:1)
使用ORDER BY
代替PARTITION BY
:
SELECT a.testscoreid,
COUNT(*) AS Questions,
a.created,
ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) AS Number,
b.irt_tlevel
答案 1 :(得分:1)
您想要显示(272 + 38 + 34)行吗?您的期望并不清楚。 如果你想要这样的行,你的主要错误就是用irt_tlevel级别进行分组。
但是如果您这样做以获得特定级别的总计数,则可能必须使用子查询或函数调用来获取总计。
例如 - (未测试语法错误)
SELECT a.testscoreid,
a.created,
ROW_NUMBER() OVER (ORDER BY created ) AS Number,
b.irt_tlevel irt_level
FROM asmt.testscores a, asmt.questions b
WHERE a.answered = TRUE
AND b.questionid = a.questionid
AND a.testscoreid = '53bd6ed5da2cfe7baf0d97ed'
ORDER BY a.created ASC