日期:2014-05-17 浏览次数:20528 次
/****** Script for SelectTopNRows command from SSMS ******/
SELECT [id]
,[column1]
,[column2]
,[column3]
FROM [dbo].[tb]
ORDER BY id DESC
id column1 column2 column3
----------- ----------------------- -------------- -------
6 ccc mm ttt <------以第一行内容做条件
5 ccc mm ttt
4 ccc mm ddd
3 ccc bbb ddd
2 aaa bbb ddd
1 aaa mm ccc
--------我想得到的结果
列名 |内容 |次数
---------------------
column1 | CCC | 4
column2 | mm | 3
column3 | ttt | 2
----测试脚本
CREATE TABLE [dbo].[tb](
[id] [int] IDENTITY(1,1) NOT NULL,
[column1] [nvarchar](50) NULL,
[column2] [nvarchar](50) NULL,
[column3] [nvarchar](50) NULL,
CONSTRAINT [PK_tb] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[tb] ON
INSERT [dbo].[tb] ([id], [column1], [column2], [column3])&n