简单的SQL语句

时间:2013-05-22 00:46:35

标签: sql

我一直在过度思考这个问题。假设我有一个表TEST(refnum VARCHAR(5))

    |refnum|
    --------
    | 12345|
    | 56873|
    | 63423|
    | 12345|
    | 56873|
    | 12345|

我希望我的“观点”看起来像这个

    |refnum| count|
    ---------------
    | 12345|   3  |
    | 56873|   2  |

因此要求每个参考文献的计数必须> 1。 我绕着这个缠绕我有点麻烦。提前感谢您的帮助。

3 个答案:

答案 0 :(得分:11)

除非我遗漏了某些内容,否则这看起来很简单

select refnum, count(*) from test group by refnum having count(*) > 1

答案 1 :(得分:0)

select refnum, count(*)
from table
group by refnum

答案 2 :(得分:0)

这是SQL Server版本:

CREATE VIEW vRefnumCounts AS
SELECT refnum, 
count(1) as [count] 
FROM test 
GROUP BY refnum 
HAVING count(1) > 1

SELECT *
FROM vRefnumCounts 
ORDER BY refnum

你说“观点”,但现在我认为你的意思是结果集......