当我写下面的查询时
SELECT Op.MRNO,
M.PatientName,
(
SELECT COUNT(MRNO) AS Visit
FROM HMS_Reg_OPRegister
GROUP BY MRNO
)
FROM HMS_Reg_Patient M,
HMS_Reg_OPRegister Op
WHERE Op.MRNO = M.MRNO
我收到了这个错误:
Msg 512,Level 16,State 1,Line 14 Subquery返回的值超过1 值。当子查询遵循=,!=,<,< =,> =或子查询用作表达式时,不允许这样做。
答案 0 :(得分:1)
SELECT Op.MRNO,
M.PatientName,
Op.Visit
FROM dbo.HMS_Reg_Patient M
JOIN (
SELECT MRNO, COUNT(MRNO) AS Visit
FROM dbo.HMS_Reg_OPRegister
GROUP BY MRNO
) Op ON Op.MRNO = M.MRNO
答案 1 :(得分:1)
看起来好像你想要:
SELECT M.MRNO,
MAX(M.PatientName) AS PatientName,
COUNT(*) AS Visit
FROM HMS_Reg_Patient M
JOIN HMS_Reg_OPRegister Op ON Op.MRNO = M.MRNO
GROUP BY M.MRNO
当您已经拥有在主查询中计算的表时,没有必要使用子查询来计算组中值的数量。