我生成了一个查询,列出了我的源数据中的一些记录(在底部)
我的表格设置由 3 个表格组成,看起来像
tu_unit_tbl
| unit_id | unit_name | unit_subject |
sp_student_tbl
| st_id | st_fname | st_lname |
sp_test_tbl
| ts_id | ts_st_id | ts_unit_id | ts_session |
到目前为止,我的查询运行良好,但我需要在其上添加一个计算字段:
SELECT U.unit_subject,
U.unit_name,
S.st_fname + ' ' + S.st_lname as sname,
T.ts_st_id,
T.ts_session
FROM sp_test_tbl T
JOIN sp_student_tbl S on T.ts_st_id = st_id
JOIN tu_unit_tbl U on unit_id = ts_unit_id
WHERE ts_st_id = 184318
GROUP BY unit_name, U.unit_subject, ts_session, st_lname, st_fname, ts_st_id
它产生这个:
如果你注意到,有 3 行。前两个具有相同的“单位名称”。我正在寻找添加一个字段来计算这些行的出现次数
unit_subject | unit_name | sname | ts_st_id | ts_session
--------------------------------------
Mathematics | Algebra, Patterns, and Relationships | Frog Man | 184318 | 2012-07-31 15:22:42.000
Mathematics | Algebra, Patterns, and Relationships | Frog Man | 184318 | 2012-07-31 15:25:47.000
Mathematics | Data, Statistics, and Probability | Frog Man | 184318 | 2012-07-31 15:25:59.000
我想得到什么
unit_subject | unit_name | sname | **COUNT OF UNIT_NAME**
---------------------------------------------------------------------------------------
Mathematics | Algebra, Patterns, and Relationships | Frog Man | **2**
Mathematics | Data, Statistics, and Probability | Frog Man | **1**
请注意,现在只有 2 条记录,因为前 2 条已合并计算。
当然,这是一个汇总结果,我想在一个新字段中计算这些汇总项(称为单元名称计数)
我尝试放入一个 Count(*) 字段,甚至JOIN
是尝试对这些字段进行计数的子查询并没有给我一个聚合计数,而是计算我聚合的每个项目!
如何计算结果中的重复单位名称?
这是原始数据:(重复数据表示学生的测试会话。每条记录都是会话的一部分)
select ts_st_id,
ts_session
from sp_test_tbl
where ts_st_id = 184318
ts_st_id | ts_session
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:22:42.000
184318 2012-07-31 15:25:47.000
184318 2012-07-31 15:25:47.000
184318 2012-07-31 15:25:47.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
184318 2012-07-31 15:25:59.000
我最近的尝试(由 praveen 推荐)似乎可行,但会出错。
WITH CTE (unit_subject,
unitName,
sname,
ts_st_id,
ts_session)
AS
(
SELECT U.unit_subject,
U.unit_name as unitName,
S.st_fname + ' ' + S.st_lname as sname,
T.ts_st_id,
T.ts_session
FROM sp_test_tbl T
JOIN sp_student_tbl S on T.ts_st_id = st_id
JOIN tu_unit_tbl U on unit_id = ts_unit_id
WHERE ts_st_id = 1234
GROUP BY unit_name, U.unit_subject, ts_session, st_lname, st_fname, ts_st_id
)
select count(unitName) over (partition by unit_name)
Go
生成以下错误消息:
Msg 207, Level 16, State 1, Line 22
Invalid column name 'unit_name'.
Msg 207, Level 16, State 1, Line 22
Invalid column name 'unitName'.