SQL PIVOT如何根据字段的条件格式化结果?

时间:2019-06-01 23:42:35

标签: sql-server pivot

我在查询中有以下数据需要进一步处理

Id          User                    Application           CompanyId         ApplicationId Restricted
----------- ----------------------- --------------------- ----------------- ------------- -----------
1           yes@way.com             members               4027              1             0           
2           dg@spw.com              members               1104              1             0
3           dg@spw.com              reports               1104              2             0 
4           dg@spw.com              identity              1104              3             0
5           dg@spw.com              landing page          1104              4             0
6           dg@spw.com              authorization         1104              7             0
7           jtize@sssssss.com       members               1104              1             0
8           jtize@sssssss.com       reports               1104              2             0
9           jtize@sssssss.com       identity              1104              3             0
10          jtize@sssssss.com       landing page          1104              4             1

我可以使用SQL PIVOT获得以下内容,但它不能完全满足应用程序的需求。

PIVOT查询(请注意,为了简化起见,结果已被截断)

WITH UserData AS
(
 SELECT ISNULL(CAST((ROW_NUMBER() OVER (order by u.Id, a.Id)) as int), 0) 
        as Id, u.UserName AS [User], 
        a.Description AS Application, ca.CompanyId, u.Id AS UserId, a.Id AS ApplicationId
    FROM  dbo.Application AS a LEFT OUTER JOIN
        dbo.CompanyApplication AS ca ON ca.ApplicationId = a.Id RIGHT OUTER JOIN
        dbo.AspNetUsers AS u ON u.CompanyId = ca.CompanyId
)
SELECT *

  FROM (
  SELECT [User], [CompanyId], [ApplicationId], [Application]
  FROM  (SElect * from UserData where id < 13) x
) as s
PIVOT (
  MIN([ApplicationId])
  FOR [Application] in (
    [members], [identity], [admin], [contractor qualification], [audits], 
         [landing page], [sitetracker], [reports], [authorization]
  )
) 
as pvt

结果:

User                members  identity  landing page  reports     authorization
------------------- -------- --------- ------------  ----------- -------------
dg@spw.com          1        3         4             2           7
jtize@sssssss.com   1        3         4             2           0
yes@way.com         1        NULL      NULL          NULL        NULL

实际输出必须是三个值之一(applicationid,0和-1)。 所需结果如下

User                members  identity  landing page  reports     authorization
------------------- -------- --------- ------------  ----------- -------------
dg@spw.com          1        3         4             2           7
jtize@sssssss.com   1        3        -1             2           0
yes@way.com         1        0         0             0           0

请注意-1用于限制列

1 个答案:

答案 0 :(得分:0)

如果我猜对了,那么当Restricted = 1时,您想将[ApplicationId]替换为-1,那么以下脚本将提供所需的输出-

SELECT [user],
members,
ISNULL([identity],0) AS [identity],
ISNULL([landing page],0) AS [landing page],
ISNULL([reports],0) AS [reports],
ISNULL([authorization],0) AS [authorization]
FROM 
(
    SELECT [User], 
    [CompanyId], 
    CASE WHEN Restricted = 1 THEN -1 ELSE [ApplicationId] END [ApplicationId], 
    [Application]
    FROM  UserData
    WHERE id < 13
) AS s
PIVOT
(
    SUM([ApplicationId])
    FOR [Application] in 
    (
        [members], [identity], [admin], [contractor qualification], [audits], 
        [landing page], [sitetracker], [reports], [authorization]
    )
) AS pvt

输出为-

user                members identity    landing page    reports authorization
dg@spw.com          1       3           4               2       7
jtize@sssssss.com   1       3           -1              2       0
yes@way.com         1       0           0               0       0