WITH List_CTE AS (
SELECT Convert(NVARCHAR(255), NWMGUID) AS Sort
,N.No
,N.NWMGUID
,N.ParentID
,N.Depth
,N.Date
,N.TypeCD
,N.Title
,N.PGGUID
,N.ModifyDT
FROM TB_GIFT_NWMeeting N
WHERE NWMGUID = @NWMGUID
UNION ALL
SELECT Convert(NVARCHAR(255),RTRIM(pf.Sort) + Convert(NVARCHAR(255),ROW_NUMBER() OVER(ORDER BY N.No desc))) AS Sort --정렬할값을 만들어준다.
,N.No
,N.NWMGUID
,N.ParentID
,N.Depth
,N.Date
,N.TypeCD
,N.Title
,N.PGGUID
,N.ModifyDT
FROM TB_GIFT_NWMeeting N
INNER JOIN List_CTE pf
ON pf.NWMGUID = N.ParentID
AND N.Active='Y'
)
SELECT L.Sort
,(Select COUNT(*) from TB_GIFT_NWMeeting N where N.ParentID = L.NWMGUID) as Count
,(SELECT COUNT(*)-1 FROM TB_GIFT_SKAttendant SK WHERE SK.Type ='A' AND SK.ParentID = L.NWMGUID ) as UserCount
,(SELECT TOP 1 tul.Name FROM dbo.TB_GIFT_SKAttendant sk
LEFT OUTER JOIN CmnMgt.dbo.tb_User tu
ON sk.UserID = tu.UserID
AND sk.Type ='A'
LEFT OUTER JOIN CmnMgt.dbo.tb_Userlang tul
ON tu.UserID = tul.UserID AND tul.LangID =@Lang
WHERE SK.ParentID = L.NWMGUID ORDER BY SK.SEQ ASC) AS UserName
,L.No
,L.NWMGUID
,L.ParentID
,L.Depth
,L.Date
,L.TypeCD
,CATEGORY_Name as Type
,L.Title
,L.PGGUID
,P.PGName
,CONVERT(VARCHAR(10), L.ModifyDT, 120) as ModifyDT
FROM List_CTE L
LEFT OUTER JOIN CmnMgt.dbo.tb_Category cg
ON L.TypeCD = cg.CATEGORY_CODE
LEFT OUTER JOIN TB_GIFT_Program P
ON L.PGGUID = P.PGGUID
WHERE ParentID is not null
ORDER BY Sort
ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.ko/s10de_6tsql/html/27cfb819-3e8d-4274-8bbe-cbbe4d9c2e23.htm
6번 참고
'ms-sql' 카테고리의 다른 글
SQL 서버의 잠금에 대한 고찰 (1) | 2010.09.10 |
---|---|
CTE 방식으로 게시판 List Data가져오기 (3) | 2010.09.10 |
ms-sql 2008 설치 (1) | 2010.09.09 |
프로시져에서 Identity값 Return하기 (1) | 2010.09.09 |
value값 구분자 넣어서 자르기 (3) | 2010.09.09 |