2016-06-29 4 views
0

私はいくつかの情報を抽出しようとしているデータベースを継承しました。私は何の問題もなくSQLを使って欲しいことを多くすることができますが、このクエリをどこから始めるべきかはわかりません。テーブルヘッダーとしての行数を可変にする

次のようにクエリに関連する4つのテーブルがあります。

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

あなたはSSRSを使用していますか? –

+0

いいえ。マシンは実際はAWS RDSインスタンスです。私がそれを使うことができるかどうか分からない?私はほとんどのもののために純粋なSQLを使用する傾向がありました。 – Hazzanet

答えて

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 
+0

それは絶対に素晴らしいです - ありがとう。私は今、ピボットコマンドをよく見ていきます。 – Hazzanet

関連する問題