1. 程式人生 > >row_number() over 兩表連線分頁SQL 一對多的表關係 去重

row_number() over 兩表連線分頁SQL 一對多的表關係 去重

--總條數
SELECT COUNT(1)
FROM  dbo.osaleh   INNER JOIN dbo.osaled 
ON dbo.osaleh.osaleh_osalehID=dbo.osaled.osaled_osalehID WHERE 
--兩表連線分頁
SELECT * FROM(
	SELECT ROW_NUMBER() OVER ( ORDER BY osaleh_osalehID DESC ) AS rownum,
	dbo.osaleh.osaleh_osalehID,
	dbo.osaleh.osaleh_custname,
	dbo.osaleh.osaleh_tel,
	dbo.osaleh.osaleh_Name,
	dbo.osaleh.osaleh_userID,
	(SELECT User_LastName FROM dbo.Users (NOLOCK) WHERE User_UserId=dbo.osaleh.osaleh_userID) AS SalesManName,
	dbo.osaleh.osaleh_mkdate,
	dbo.osaled.osaled_oactvhID
	FROM  dbo.osaleh   INNER JOIN dbo.osaled   
	ON dbo.osaleh.osaleh_osalehID=dbo.osaled.osaled_osalehID
	WHERE dbo.osaled.osaled_oactvhID=2
) AS tb WHERE rownum BETWEEN( (1 - 1) *5+1) AND ( 1 * 5 )

--如果不排序
SELECT * FROM(
	SELECT ROW_NUMBER() OVER ( ORDER BY (SELECT 0) ) AS rownum,
	dbo.osaleh.osaleh_osalehID,
	dbo.osaleh.osaleh_custname,
	dbo.osaleh.osaleh_tel,
	dbo.osaleh.osaleh_Name,
	dbo.osaleh.osaleh_userID,
	(SELECT User_LastName FROM dbo.Users (NOLOCK) WHERE User_UserId=dbo.osaleh.osaleh_userID) AS SalesManName,
	dbo.osaleh.osaleh_mkdate,
	dbo.osaled.osaled_oactvhID
	FROM  dbo.osaleh   INNER JOIN dbo.osaled   
	ON dbo.osaleh.osaleh_osalehID=dbo.osaled.osaled_osalehID
	WHERE dbo.osaled.osaled_oactvhID=2
) AS tb WHERE rownum BETWEEN( (1 - 1) *5+1) AND ( 1 * 5 )


--兩表連線分頁 一對多的表關係  去重
SELECT  * FROM(
SELECT
ROW_NUMBER() OVER ( ORDER BY osaleh_mkdate DESC ) AS rownum,*
FROM
(
select DISTINCT
 dbo.osaleh.osaleh_osalehID,
dbo.osaleh.osaleh_custname,
dbo.osaleh.osaleh_tel,
dbo.osaleh.osaleh_Name,
dbo.osaleh.osaleh_userID,
(SELECT User_LastName FROM dbo.Users (NOLOCK) WHERE User_UserId=dbo.osaleh.osaleh_userID) AS SalesManName,
dbo.osaleh.osaleh_mkdate,
dbo.osaleh.osaleh_Status
FROM  dbo.osaleh   INNER JOIN dbo.osaled   
ON dbo.osaleh.osaleh_osalehID=dbo.osaled.osaled_osalehID
WHERE  1=1 and dbo.osaleh.osaleh_type='01' and (dbo.osaleh.osaleh_Deleted IS NULL OR dbo.osaleh.osaleh_Deleted=0 )
 ) AS tb
)
tc WHERE 
 rownum BETWEEN(0*5+1) AND ( 5 )

******************************************別墅圖紙推薦*************************************************
我想有一棟別墅,面朝大海,春暖花開 龍興科技別墅圖紙設計,這裡有最新最全的別墅圖紙, 這裡有最給力的別墅圖紙折扣活動,圖紙包括建築圖、結構圖、給排水圖、電氣圖 我們致力於為廣大客戶提供別墅設計圖紙,圖紙均由專業、經驗豐富的設計團隊設計,可免費提供施工技術指導