2014-07-18 71 views
1

我有一個查詢拉取用戶信息。從另一個表中添加基於列的值

SELECT UserID, CompanyID, FName, LName FROM tblUsers Where OrgType = 'business' 

我有另一個查詢拉用戶信息。

SELECT u.UserID, u.AccessID, t.AccessName FROM tblUserAccess as u Inner Join tblAccessType as t on u.AccessID = t.AccessID 

最後還有第三個查詢拉取存取類型。

SELECT AccessID, AccessName, OrgType FROM tblAccessType WHERE OrgType = 'business' 

tblAccessType中可以有X個不同的訪問類型。不同的訪問類型將隨着時間而增長。另外,用戶可以擁有X個訪問類型。

如何編寫將以下列格式返回數據的查詢?

UserID CompanyID FName LName SALES MARKET ADMIN FIN 

100 200  Jane Doe  *     * 

101 200  John Doe     * 

120 205  Mary Smith *    * 

121 205  Mark Smith *     * 

他是下面的示例數據。

tblUsers 

UserID CompanyID FName LName OrgType 

100  200  Jane Doe business 

101  200  John Doe business 

120  205  Mary Smith business 

121  205  Mark Smith business 

122  259  Fred Wilson charity 

tblUserAccess 

UserID AccessID 

100 1 

100 4 

101 3 

120 1 

120 3 

121 1 

121 4 


tblAccessType 

AccessID AccessName OrgType 

1  SALES  business 

2  MARKET  business 

3  ADMIN  business 

4  FIN  business 

5  NOTAX  charity 

6  SECURITY government 
+0

你需要通過Userid來轉換訪問類型,然後將其加入到用戶中......您能否張貼一些示例源數據以及您可能已經嘗試過的任何內容? – JiggsJedi

回答

0

你需要的是一個PIVOT ...我有一個動態PIVOT去,所以如果你添加新的AccessTypes,它們會自動添加。

如果你想省略NULL數據,然後更改''*'' AS AccessVal1 AS AccessVal,然後改變MAX([AccessVal])COUNT([AccessVal]) - 這會給你1/0,而不是*/NULL

IF OBJECT_ID('tempdb..#tblUsers') IS NOT NULL DROP TABLE #tblUsers 
IF OBJECT_ID('tempdb..#tblUserAccess') IS NOT NULL DROP TABLE #tblUserAccess 
IF OBJECT_ID('tempdb..#tblAccessType') IS NOT NULL DROP TABLE #tblAccessType 
IF OBJECT_ID('tempdb..##tblAccessPivot') IS NOT NULL DROP TABLE ##tblAccessPivot 

CREATE TABLE #tblUsers (UserID INT, CompanyID INT, FName VARCHAR(255), LName VARCHAR(255), OrgType VARCHAR(255)) 
CREATE TABLE #tblUserAccess (UserID INT, AccessID INT) 
CREATE TABLE #tblAccessType (AccessID INT, AccessName VARCHAR(255), OrgType VARCHAR(255)) 

INSERT INTO #tblUsers (UserID, CompanyID, FName, LName, OrgType) 
VALUES (100, 200, 'Jane', 'Doe', 'Business'), 
     (101, 200, 'John', 'Doe', 'Business'), 
     (120, 205, 'Mary', 'Smith', 'Business'), 
     (121, 205, 'Mark', 'Smith', 'Business'), 
     (122, 259, 'Fred', 'Wilson', 'Charity') 

INSERT INTO #tblAccessType (AccessID, AccessName, OrgType) 
VALUES (1, 'Sales', 'Business'), 
     (2, 'Market', 'Business'), 
     (3, 'Admin', 'Business'), 
     (4, 'Fin', 'Business'), 
     (5, 'NoTax', 'Charity'), 
     (6, 'Security', 'Government') 

INSERT INTO #tblUserAccess (UserID, AccessID) 
VALUES (100,1),(100,4), -- JANE 
     (101,3), -- JOHN 
     (120,1),(120,3), -- MARY 
     (121,1),(121,4) -- MARK 

DECLARE @cols AS NVARCHAR(MAX), 
     @sql AS NVARCHAR(MAX) 

SET @cols = STUFF(
(SELECT N',' + QUOTENAME(y) AS [text()] 
FROM (SELECT DISTINCT AccessName AS y FROM #tblAccessType) AS Y 
ORDER BY y 
FOR XML PATH('')), 
1, 1, N'');   

SET @sql = 'SELECT UserID, CompanyID, Fname, LName, '[email protected]+' 
      INTO ##tblAccessPivot 
      FROM (SELECT U.UserID, U.CompanyID, U.Fname, U.LName, AT.AccessName, ''*'' AS AccessVal 
        FROM #tblUsers U 
        LEFT OUTER JOIN #tblUserAccess UA ON U.UserID=UA.UserID 
        LEFT OUTER JOIN #tblAccessType AT ON UA.AccessID=AT.AccessID) SUB 
      PIVOT (MAX([AccessVal]) FOR AccessName IN ('[email protected]+')) AS P' 
PRINT @SQL 
EXEC (@SQL) 

SELECT * 
FROM ##tblAccessPivot 
+0

謝謝!很棒! – crjunk

0

這裏是要對這個如果你的訪問類型保持(主要)靜態的方式。它不涉及旋轉,這總是一個加號。

DECLARE @tblUsers TABLE 
(
    UserID INT, 
    Name VARCHAR(50) 
) 

INSERT INTO @tblUsers SELECT 100, 'John Doe' 
INSERT INTO @tblUsers SELECT 101, 'Jane Doe' 

DECLARE @tblUserAccess TABLE 
(
    UserID INT, 
    AccessID INT 
) 

INSERT INTO @tblUserAccess SELECT 100, 1 
INSERT INTO @tblUserAccess SELECT 100, 4 
INSERT INTO @tblUserAccess SELECT 101, 3 

DECLARE @tblAccessType TABLE 
(
    AccessID INT, 
    AccessName VARCHAR(50) 
) 

INSERT INTO @tblAccessType SELECT 1, 'SALES' 
INSERT INTO @tblAccessType SELECT 2, 'MARKET' 
INSERT INTO @tblAccessType SELECT 3, 'ADMIN' 
INSERT INTO @tblAccessType SELECT 4, 'FIN' 


SELECT u.UserID 
    , u.Name 
    , [SALES] = MAX(CASE WHEN at.AccessName = 'SALES' THEN '*' ELSE ' ' END) 
    , [MARKET] = MAX(CASE WHEN at.AccessName = 'MARKET' THEN '*' ELSE ' ' END) 
    , [ADMIN] = MAX(CASE WHEN at.AccessName = 'ADMIN' THEN '*' ELSE ' ' END) 
    , [FIN] = MAX(CASE WHEN at.AccessName = 'FIN' THEN '*' ELSE ' ' END) 
FROM @tblUsers u 
JOIN @tblUserAccess ua 
    ON u.UserID = ua.UserID 
JOIN @tblAccessType at 
    ON ua.AccessID = at.AccessID 
GROUP BY u.UserID, u.Name 

和結果:

UserID Name SALES MARKET ADMIN FIN 
100 John Doe *      * 
101 Jane Doe     *  
相關問題