没有聚合的动态SQL数据透视查询

时间:2016-05-12 20:59:20

标签: sql sql-server

我正在设置一个简单的数据库来审核备份。来自特定职位的用户将在审核中“签字”以确认系统是否需要且保留是否正确。现在我只会有两个职位进行签核,但将来还会有几个职位,因此我需要动态提升。我似乎无法弄清楚如何得出每个职位已经签署审计中的哪些项目的摘要。我试图使用一个枢轴,但我不需要聚合,所以我无法使它工作。

我使用了以下查询来获取我想要显示的内容的列表,但我似乎无法将它们正确地组合在一起。

select BA.ID, A.Audit_Date, BA.Server_Name, BA.Group_Name, BA.Backup_Retention, BA.Backup_Type
from Backup_Audit BA
left join Audit A on BA.Audit_ID=A.ID
order by A.Audit_Date asc

select BS.Backup_Audit_ID,SP.SPDescription,ST.STDescription
from Backup_Signoff BS
inner join Signoff S on BS.Signoff_ID=S.ID
inner join Signoff_Position SP on S.Signoff_Position_ID=SP.ID
inner join Signoff_Type ST on BS.Signoff_Type_ID=ST.ID

结果:

ID          Audit Date             Server Name                    Group Name                     Backup_Retention     Backup Type
----------- ---------------------- ------------------------------ ------------------------------ -------------------- --------------------
1           2016-05-12 19:03:59    server1                        NDMP_DD                        6 Months             NDMP
2           2016-05-12 19:03:59    server2                        Windows_DD                     1 Month              OS
4           2016-05-12 19:03:59    server3                        SQL_DD                         3 Months             SQL
3           2016-05-12 19:03:59    server3                        Windows_DD                     1 Month              OS
5           2016-05-12 19:03:59    server4                        SQL_DD                         3 Months             SQL
6           2016-05-12 19:05:59    server1                        NDMP_DD                        6 Months             NDMP
7           2016-05-12 19:05:59    server2                        Windows_DD                     1 Month              OS
9           2016-05-12 19:05:59    server3                        SQL_DD                         3 Months             SQL
8           2016-05-12 19:05:59    server3                        Windows_DD                     1 Month              OS

(9 row(s) affected)

Backup_Audit_ID SPDescription                  STDescription
--------------- ------------------------------ --------------------
1               Local IT                       Accepted
1               Controller                     Accepted
2               Local IT                       Not Accepted

(3 row(s) affected)

我希望最终产品展示如下:

ID  Audit Date           Server Name  Group Name   Backup_Retention  Backup Type  Local IT      Controller
--- -------------------- ------------ ------------ ----------------- ------------ ------------- -----------
1   2016-05-12 14:20:10  server1      NDMP_DD      6 Months          NDMP         Accepted      Accepted
2   2016-05-12 14:20:10  server2      Windows_DD   1 Month           OS           Rejected      NULL
4   2016-05-12 14:20:10  server3      SQL_DD       3 Months          SQL          NULL          NULL
3   2016-05-12 14:20:10  server3      Windows_DD   1 Month           OS           NULL          NULL
5   2016-05-12 14:20:10  server4      SQL_DD       3 Months          SQL          NULL          NULL
6   2016-05-12 14:22:10  server1      NDMP_DD      6 Months          NDMP         NULL          NULL
7   2016-05-12 14:22:10  server2      Windows_DD   1 Month           OS           NULL          NULL
9   2016-05-12 14:22:10  server3      SQL_DD       3 Months          SQL          NULL          NULL
8   2016-05-12 14:22:10  server3      Windows_DD   1 Month           OS           NULL          NULL

任何人都能提供的任何帮助都会有很大帮助。当我回到家时,我可以上传脚本来构建数据库并填充数据(如果需要)。我有一种感觉,结果很容易到达,目前只是我的头脑。再次感谢。

指向dml和ddl的链接 Create DB
Populate some data

0 个答案:

没有答案