create table orders
(
id int,
orderName varchar()
)
go
create table cars
(
id int identity(,) ,
ordersid int,
pname varchar(),
pprice varchar()
)
go
insert into orders values(,'订单1')
insert into cars values(,'商品1','价格1')
insert into cars values(,'商品2','价格2')
insert into cars values(,'商品3','价格3')
insert into orders values(,'订单2')
insert into cars values(,'商品21','价格21')
insert into cars values(,'商品22','价格22')
insert into cars values(,'商品23','价格23')
go
select * from orders
select * from cars
go
select
case x.AId when '' then x.orderName else '' end 订单名称,
x.pname,x.pprice
from
(
SELECT t.*, dense_rank() OVER (partition BY t.id
ORDER BY t.ids ASC) AS AId
from
(
select a.id as ids,b.id,b.orderName,a.pname,a.pprice from cars a inner join orders b on a.ordersid=b.id
) as t
) as x
go
CREATE TABLE [dbo].[testTable]
(
[id] [int] NOT NULL IDENTITY(, ),
[name] [nchar] () collate Chinese_PRC_CI_AS NULL,
[counts] [int] NULL,
[createDate] [datetime] NULL
)
GO
-- Constraints and Indexes
ALTER TABLE [dbo].[testTable] ADD CONSTRAINT [PK_testTable] PRIMARY KEY CLUSTERED ([id])
GO
insert into testTable(id,name,counts,createDate) values(,'A ',,'01 14 2011 10:52PM')
insert into testTable(id,name,counts,createDate) values(,'A ',,'02 14 2011 10:52PM')
insert into testTable(id,name,counts,createDate) values(,'B ',,'03 14 2011 10:52PM')
insert into testTable(id,name,counts,createDate) values(,'B ',,'04 14 2011 10:52PM')
insert into testTable(id,name,counts,createDate) values(,'B ',,'05 14 2011 10:52PM')
insert into testTable(id,name,counts,createDate) values(,'C ',,'06 14 2011 10:52PM')
insert into testTable(id,name,counts,createDate) values(,'C ',,'07 14 2011 10:52PM')
select * from (
select id,name,counts,createDate,row_number() over(partition by name order by createDate desc) rn
from testTable
) t where t.rn <=