日期:2014-05-18  浏览次数:20554 次

使用Union联接两种表后再进行分页,谢谢!
使用Union联接两种表后,这两种表都有ROW_NUMBER()对其进行了排序,查询出来结果如图,所以再想用ROW_NUMBER() 对这个查询出来的表进行排序分页.有木有方法啊!
SQL code

SELECT * FROM (
SELECT ROW_NUMBER() OVER (ORDER BY app.Processingtime DESC) AS pos,
app.userid,app.Id AS aid,app.Type,app.Loanamount,app.LoanUse,app.Locationarea,
app.Collateralname,app.LoanTitle,app.LoanContent,app.Addtime,app.Processingtime,app.IsIndex,app.LoanCity,
app.CountHit,app.CountLeave,pro.ProvinceName,city.CityName,men.Id AS mid,men.TrueName,men.HeadInphoto,men.Headphoto 
FROM dbo.tb_Apply AS app
INNER JOIN RegionalismProvinceCode pro ON app.Locationarea=pro.ProvinceID 
INNER JOIN RegionalismCityCode city ON city.CityID=app.LoanCity 
INNER JOIN dbo.tb_Menbermation AS men ON app.Userid=men.Userid 
UNION 
SELECT ROW_NUMBER() OVER (ORDER BY app.Processingtime DESC) AS pos,
app.userid,app.Id AS aid,app.Type,app.Loanamount,app.LoanUse,app.Locationarea,
app.Collateralname,app.LoanTitle,app.LoanContent,app.Addtime,app.Processingtime,app.IsIndex,app.LoanCity,app.CountHit,app.CountLeave,
pro.ProvinceName,city.CityName,ent.Id AS eid,ent.TrueName,ent.HeadInphoto,ent.Headphoto 
FROM dbo.tb_Apply AS app 
INNER JOIN RegionalismProvinceCode pro ON app.Locationarea=pro.ProvinceID 
INNER JOIN RegionalismCityCode city ON city.CityID=app.LoanCity 
INNER JOIN dbo.tb_MenberEnterprise AS ent ON app.Userid=ent.Userid) AS sp 




------解决方案--------------------
SQL code
;WITH  t AS (
SELECT ROW_NUMBER() OVER ( ORDER BY Processingtime DESC ) AS row_id, *
FROM   (
      SELECT  app.userid, app.Id AS aid, app.Type, app.Loanamount, app.LoanUse, app.Locationarea, app.Collateralname, app.LoanTitle,
              app.LoanContent, app.Addtime, app.Processingtime, app.IsIndex, app.LoanCity, app.CountHit, app.CountLeave, pro.ProvinceName,
              city.CityName, men.Id AS mid, men.TrueName, men.HeadInphoto, men.Headphoto
      FROM    dbo.tb_Apply AS app
      INNER JOIN RegionalismProvinceCode pro
      ON      app.Locationarea = pro.ProvinceID
      INNER JOIN RegionalismCityCode city
      ON      city.CityID = app.LoanCity
      INNER JOIN dbo.tb_Menbermation AS men
      ON      app.Userid = men.Userid
      UNION
      SELECT  app.userid, app.Id AS aid, app.Type, app.Loanamount, app.LoanUse, app.Locationarea, app.Collateralname, app.LoanTitle,
              app.LoanContent, app.Addtime, app.Processingtime, app.IsIndex, app.LoanCity, app.CountHit, app.CountLeave, pro.ProvinceName,
              city.CityName, ent.Id AS eid, ent.TrueName, ent.HeadInphoto, ent.Headphoto
      FROM    dbo.tb_Apply AS app
      INNER JOIN RegionalismProvinceCode pro
      ON      app.Locationarea = pro.ProvinceID
      INNER JOIN RegionalismCityCode city
      ON      city.CityID = app.LoanCity
      INNER JOIN dbo.tb_MenberEnterprise AS ent
      ON      app.Userid = ent.Userid
    ) AS sp
)
SELECT *
FROM   t
WHERE  row_id BETWEEN 1 AND 10