我继承了一个数据库,我试图从中提取一些信息。我可以在没有任何问题的情况下完成我想要的SQL,但我真的不知道从哪个查询开始。
有四个与查询相关的表格如下:
Clients
ID | Client Name | Client Reference
-------------------------------------
1 | Acme Inc | ACM
2 | Sprocket Inc | SPR
Customers_Table
ID | Client | Customer Name
----------------------------
1 | 1 | Fred
2 | 1 | Sarah
3 | 2 | Mary
4 | 2 | Dave
Custom_Questions
ID | Client | Question
---------------------------------------------------
1 | 1 | What is the name of your dog?
2 | 1 | How old is your dog?
3 | 2 | What is your cat's name?
4 | 2 | When was the cat's last vet visit?
Answers
ID | Customer | Question | Answer
---------------------------------
1 | 1 | 1 | Rover
2 | 1 | 2 | 10
3 | 2 | 1 | Bob
4 | 2 | 2 | 1
5 | 3 | 3 | Trixie
6 | 3 | 4 | Never
7 | 4 | 3 | Furball
8 | 4 | 4 | Last year
系统拥有可以拥有多个客户的客户。客户可以向客户询问可变数量的自定义问题。自定义问题在一个表中,答案在另一个表中。
基本上,我需要为每个客户创建一份报告,列出客户,问题和答案,如下所示:
Client 1 Report:
Customer Name | What is the name of your dog? | How old is your dog?
====================================================================
Fred | Rover | 10
Sarah | Bob | 1
它似乎有点像excel数据透视表,但我无法在Google上找到任何适合的内容。谁能给我一些关于我应该搜索什么或者我应该看什么命令的指示?
由于
答案 0 :(得分:0)
IF OBJECT_ID ('tempdb..#Clients') is not null drop table #Clients
IF OBJECT_ID ('tempdb..#Answers') is not null drop table #Answers
IF OBJECT_ID ('tempdb..#CustomQuestions') is not null drop table #CustomQuestions
IF OBJECT_ID ('tempdb..#CustomersTable') is not null drop table #CustomersTable
IF OBJECT_ID ('tempdb..#Holding') is not null drop table #Holding
DECLARE @ID int --this is the client ID of interest
SET @ID = 1
--create some work tables for testing
create table #Clients(
ID int,
ClientName varchar(128),
ClientReference varchar(3))
insert into #Clients (ID, ClientName, ClientReference) values
(1,'Acme Inc','ACM'),
(2,'Sproket Inc', 'SPR')
create table #CustomersTable(
ID int,
Client int,
CustomerName varchar(128))
insert into #CustomersTable(ID, Client,CustomerName) values
(1,1,'Fred'),
(2,1,'Sarah'),
(3,2,'Mary'),
(4,2,'Dave')
create table #CustomQuestions(
ID int,
Client int,
Question varchar(max))
insert into #CustomQuestions (ID, Client, Question) values
(1,1,'What is the name of your dog'),
(2,1,'How old is your dog'),
(3,2,'What is your cats name'),
(4,2,'When was the cats last visit')
create table #Answers(
ID int,
Customer int,
Question int,
Answer varchar(max))
insert into #Answers(ID, Customer,Question, Answer) values
(1,1,1,'Rover'),
(2,1,2,'10'),
(3,2,1,'Bob'),
(4,2,2,'1'),
(5,3,3,'Trixie'),
(6,3,4,'Never'),
(7,4,3,'Furball'),
(8,4,4,'Last year')
--get your data for the client you need
select
c.ClientName,
ct.CustomerName,
q.Question,
a.Answer
into #Holding
from
#Clients c
inner join #CustomersTable ct on
ct.Client = c.ID
inner join #Answers a on
a.Customer = ct.ID
inner join #CustomQuestions q on
q.id = a.Question
where
c.ID = @ID
--Now piviot, with a dynamic amount of questions
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
SELECT @ColumnName= ISNULL(@ColumnName + ',','')
+ QUOTENAME(Question)
FROM (SELECT DISTINCT Question FROM #Holding) AS Question
--Prepare the PIVOT query using the dynamic
SET @DynamicPivotQuery =
N'SELECT CustomerName, ' + @ColumnName + '
FROM #Holding
PIVOT(Max(Answer)
FOR Question IN (' + @ColumnName + ')) AS PVTTable'
EXEC sp_executesql @DynamicPivotQuery