码迷,mamicode.com
首页 > 数据库 > 详细

SQL Server 2008 R2——ROW_NUMBER() 去掉不同行中相同列的重复内容

时间:2015-12-28 23:42:24      阅读:685      评论:0      收藏:0      [点我收藏+]

标签:


USE
tempdb GO -------------------------------------------------------------------------- IF OBJECT_ID(Student,U) IS NOT NULL DROP TABLE Student GO CREATE TABLE Student ( StuID NVARCHAR(8) PRIMARY KEY, Name NVARCHAR(5) ) GO INSERT INTO Student(StuID,Name) VALUES(20080001,Lily) INSERT INTO Student(StuID,Name) VALUES(20080002,Lucy) INSERT INTO Student(StuID,Name) VALUES(20080003,Jack) GO -------------------------------------------------------------------------- IF OBJECT_ID(SltCourse,U) IS NOT NULL DROP TABLE SltCourse GO CREATE TABLE SltCourse --SelectiveCourse ( ID INT PRIMARY KEY IDENTITY(1,1), StuID NVARCHAR(8), CourseName NVARCHAR(10), Score INT ) GO INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(20080003,电脑维修,90) INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(20080003,剪纸,80) INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(20080003,市场策划,95) INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(20080003,信息检索,100) INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(20080001,插花,99) INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(20080001,剪纸,96) INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(20080001,刺绣,92) INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(20080002,插花,98) GO -------------------------------------------------------------------------- SELECT * FROM Student GO SELECT * FROM SltCourse GO -------------------------------------------------------------------------- SELECT * FROM Student s,SltCourse sc WHERE s.stuid=sc.stuid GO -------------------------------------------------------------------------- WITH ReportCard AS( SELECT s.StuID,s.Name,sc.CourseName,sc.Score FROM Student s,SltCourse sc WHERE s.stuid=sc.stuid ) SELECT CASE WHEN RowNum=1 THEN StuID ELSE ‘‘ END AS ID, CASE WHEN RowNum=1 THEN Name ELSE ‘‘ END AS Name, CourseName, Score FROM (SELECT *,ROW_NUMBER() OVER (PARTITION BY StuID,Name ORDER BY Score) AS RowNum FROM ReportCard) TBL GO -------------------------------------------------------------------------- DROP TABLE Student GO DROP TABLE SltCourse GO

 技术分享

 

USE tempdb
GO
--------------------------------------------------------------------------
IF OBJECT_ID(‘Student‘,‘U‘) IS NOT NULL
DROP TABLE Student
GO

CREATE TABLE Student
(
    StuID NVARCHAR(8) PRIMARY KEY,
    Name NVARCHAR(5),
    EntranceTime DATETIME
)
GO

INSERT INTO Student(StuID,Name,EntranceTime) VALUES(‘20080001‘,‘Lily‘,‘2008-08-27‘)
INSERT INTO Student(StuID,Name,EntranceTime) VALUES(‘20090002‘,‘Lucy‘,‘2009-08-26‘)
INSERT INTO Student(StuID,Name,EntranceTime) VALUES(‘20070003‘,‘Jack‘,‘2007-08-28‘)
GO
--------------------------------------------------------------------------
IF OBJECT_ID(‘SltCourse‘,‘U‘) IS NOT NULL
DROP TABLE SltCourse
GO

CREATE TABLE SltCourse    --SelectiveCourse
(
    ID INT PRIMARY KEY IDENTITY(1,1),
    StuID NVARCHAR(8),
    CourseName NVARCHAR(10),
    Score    INT 
)
GO

INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(‘20070003‘,‘电脑维修‘,90)
INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(‘20070003‘,‘剪纸‘,80)
INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(‘20070003‘,‘市场策划‘,95)
INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(‘20070003‘,‘信息检索‘,100)

INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(‘20080001‘,‘插花‘,99)
INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(‘20080001‘,‘剪纸‘,96)
INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(‘20080001‘,‘刺绣‘,92)

INSERT INTO SltCourse(StuID,CourseName,Score) VALUES(‘20090002‘,‘插花‘,98)
GO
--------------------------------------------------------------------------
SELECT * FROM Student
GO
SELECT * FROM SltCourse
GO
--------------------------------------------------------------------------
SELECT * FROM Student s,SltCourse sc WHERE s.stuid=sc.stuid
GO
--------------------------------------------------------------------------
WITH ReportCard 
AS(
SELECT s.StuID,s.Name,s.EntranceTime,sc.CourseName,sc.Score FROM Student s,SltCourse sc WHERE s.stuid=sc.stuid
)
SELECT  CASE WHEN RowNum=1 THEN StuID          ELSE ‘‘ END AS ID,
        CASE WHEN RowNum=1 THEN Name            ELSE ‘‘ END AS Name,
        CASE WHEN RowNum=1 THEN EntranceTime    ELSE ‘‘ END AS EntranceTime,
        CourseName,
        Score
FROM (SELECT *,ROW_NUMBER()  OVER (PARTITION BY StuID,Name,EntranceTime ORDER BY Score) AS RowNum FROM ReportCard) TBL
GO
--------------------------------------------------------------------------
DROP TABLE Student
GO
DROP TABLE SltCourse
GO

  技术分享

WITH ReportCard 
AS(
SELECT s.StuID,s.Name,s.EntranceTime,sc.CourseName,sc.Score FROM Student s,SltCourse sc WHERE s.stuid=sc.stuid
)
SELECT  CASE WHEN RowNum=1 THEN StuID          ELSE ‘‘ END AS ID,
        CASE WHEN RowNum=1 THEN Name            ELSE ‘‘ END AS Name,
        CASE WHEN RowNum=1 THEN EntranceTime    ELSE cast(nullif(‘‘,‘‘) as datetime) END AS EntranceTime,
        CourseName,
        Score
FROM (SELECT *,ROW_NUMBER()  OVER (PARTITION BY StuID,Name,EntranceTime ORDER BY Score) AS RowNum FROM ReportCard) TBL
GO

  技术分享

 

SQL Server 2008 R2——ROW_NUMBER() 去掉不同行中相同列的重复内容

标签:

原文地址:http://www.cnblogs.com/amylis_chen/p/5084268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!