select top 1 字段,count(*) from 表 group by 字段 order by count(*) desc
你这个相当于select *,[表] from [表] 当然不行了,另外我不是太明白你的意思
是分组 然后组内排名吗?
SELECT RANK() OVER (PARTITION BY OrderID ORDER BY PurchaseCount) AS POS,OrderID,PurchaseCount,* FROM dbo.TB_PurchaseOrderDetail
还是这么样子的 描述的清楚点
我就想先分组后在组内排序取每组第一个。
@度郎:
CREATE TABLE #Test(
ID INT,
VALUE VARCHAR(50)
)
--插入组号,对应值(执行3 遍)
INSERT INTO #Test
( ID, VALUE )
VALUES ( 0, -- ID - int
NEWID() -- VALUE - varchar(50)
)
--插入组号,对应值(执行3 遍)
INSERT INTO #Test
( ID, VALUE )
VALUES ( 1, -- ID - int
NEWID() -- VALUE - varchar(50)
)
--取排名为1的数据
SELECT * FROM (
SELECT RANK()OVER(PARTITION BY ID ORDER BY VALUE DESC) AS POS,* FROM #Test) b
WHERE b.POS=1
SELECT UserName,MAX(aa) AS age FROM (SELECT * FROM Users WHERE Userid>1) AS t GROUP BY t.UserName
row_number可以满足你的需求
示例:
SELECT *, Row_Number() OVER (partition by deptid ORDER BY salary desc) rank FROM employee
大哥,你这题目太抽象了,
完全不知道你在说什么!
WITH a AS ( SELECT Account , UserName , ROW_NUMBER() OVER ( ORDER BY Account ) AS rowid FROM dbo.frmuser X GROUP BY Account , UserName ) SELECT MAX(rowid) FROM a ORDER BY rowid