2015-03-02 20 views
1

我有一張表,可以存儲對幸福問題的回答。結構是:獲得調查答案矩陣的回覆數

Wellbeing(WellbeingId, WellbeingDate, Q1, Q2, Q3, Q4, Q5, Q6, Q7...) 

+------------+---------------+----+----+----+ 
|WellbeingId | WellbeingDate | Q1 | Q2 | Q3 |... 
+============+===============+====+====+====+ 
|1   | 01/01/2015 | 1 | 1 | 5 | 
+------------+---------------+----+----+----+ 
|2   | 10/01/2015 | 3 | 3 | 2 | 
+------------+---------------+----+----+----+ 
|3   | 18/01/2015 | 2 | 4 | 1 | 
+------------+---------------+----+----+----+ 

WellbeingResponses(ResponseId,responseText的)

+-----------+---------------------+ 
|ResponseId | ResponseText  | 
+===========+=====================+ 
|1   | 'None of the Time' | 
+-----------+---------------------+ 
|2   | 'Rarely'   | 
+-----------+---------------------+ 
|3   | 'Sometimes'   | 
+-----------+---------------------+ 
|4   | 'Most of the time' | 
+-----------+---------------------+ 
|5   | 'All of the time' | 
+-----------+---------------------+ 

每個Q列具有15(含)之間的值。這些值鏈接到關聯文本的另一個表(即1 =不是很多,5 =所有時間等)。

我想從SQL Server獲取數據作爲每個數據庫的總數可能響應值,如下所示:

+-------------------+----------+----------+-------------+ 
|Response   | Q1 Total | Q2 Total | Q3 Total ...| 
+===================+==========+==========+=============+ 
|'None of the Time' | 500  | 256  | 546   | 
+-------------------+----------+----------+-------------+ 
|'Rarely'   | 500  | 256  | 546   | 
+-------------------+----------+----------+-------------+ 
|'Sometimes'  | 500  | 256  | 546   | 
+-------------------+----------+----------+-------------+ 

我試圖選擇出的數據的各個位,並使用UNION ALL它但只是堆疊的數據作爲從各7「Q的每一個的5個值的計數「列,也嘗試調整我在某處找到的數據透視查詢,但在嘗試使用動態數據查找從WellbeingResponses表中選擇列的數據時無法計算出該數據。

任何幫助將不勝感激。

編輯:增加幸福感反應表。

回答

2

您的Wellbeing表有點混亂。我真的建議重寫這張表進行規範化處理,這樣你就可以避免執行多個連接,甚至不需要轉換數據來獲得你想要的結果。

由於您的當前表格是非規範化的,因此您必須轉換數據以使其可行,然後將其聚合,最後將其重新轉換爲您的最終期望結果。這會有點混亂,但有幾種方法可以做到這一點。

您可以得到結果的一種方法是取消轉換您的wellbeing表,以使數據標準化。由於您使用的是SQL Server,因此可以使用UNPIVOT函數,或者根據版本,可以使用CROSS APPLY。將多列轉換爲多行的代碼將爲:

select col, value 
from wellbeing 
cross apply 
(
    select 'Q1', Q1 union all 
    select 'Q2', Q2 union all 
    select 'Q3', Q3 
) c (col, value); 

請參閱Demo。這得到你的數據格式:

| COL | VALUE | 
|-----|-------| 
| Q1 |  1 | 
| Q2 |  1 | 
| Q3 |  5 | 
| Q1 |  3 | 
| Q2 |  3 | 

現在的數據可以很容易地與其它表連接上:

select r.ResponseText, d.col 
from WellbeingResponses r 
left join 
(
    select col, value 
    from wellbeing 
    cross apply 
    (
    select 'Q1', Q1 union all 
    select 'Q2', Q2 union all 
    select 'Q3', Q3 
) c (col, value) 
) d 
    on r.responseid = d.value 

Demo。一旦你得到了每一個問題和響應的列表,你可以聚合它,並轉動總計:

select ResponseText, q1, q2, q3 
from 
(
    select r.ResponseText, d.col 
    from WellbeingResponses r 
    left join 
    (
    select col, value 
    from wellbeing 
    cross apply 
    (
     select 'Q1', Q1 union all 
     select 'Q2', Q2 union all 
     select 'Q3', Q3 
    ) c (col, value) 
) d 
    on r.responseid = d.value 
) s 
pivot 
(
    count(col) 
    for col in (Q1, Q2, Q3) 
) piv 

SQL Fiddle with Demo

獲得結果的另一種方法是在wellbeing表上執行多個連接。每個加入將是對question列:

select r.responsetext, 
    Q1Total = count(w1.q1), 
    Q2Total = count(w2.q2), 
    Q3Total = count(w3.q3) 
from WellbeingResponses r 
left join wellbeing w1 
    on r.responseid = w1.q1 
left join wellbeing w2 
    on r.responseid = w2.q2 
left join wellbeing w3 
    on r.responseid = w3.q3 
group by r.responsetext; 

Demo。這兩個版本都會給出結果:

|  RESPONSETEXT | Q1TOTAL | Q2TOTAL | Q3TOTAL | 
|------------------|---------|---------|---------| 
| All of the time |  0 |  0 |  1 | 
| Most of the time |  0 |  1 |  0 | 
| None of the Time |  1 |  1 |  1 | 
|   Rarely |  1 |  0 |  1 | 
|  Sometimes |  1 |  1 |  0 | 
+0

感謝您的理解,我會研究跨應用(不是用來作爲數據庫查詢的新手)。我可能不得不查看是否可以對數據庫進行重組,以便更輕鬆地進行操作......儘管這意味着要在網站上更改一些內容。 – bowfinger 2015-03-02 17:31:29

+0

@bowfinger我添加了一個替代解決方案,使用多個連接。最大的問題是你目前的結構,很難合作。 – Taryn 2015-03-02 17:32:47

+0

我認爲這是我最初嘗試這樣做的方式,但是我先從Wellbeing表中選擇,然後將內部聯接(因爲沒有值永遠不能爲null)到WellbeingResponses表。它看起來很骯髒和低效......可能是重組的原因。在您的原始答案中,我將在哪裏添加ORDER BY子句以獲得1至5的響應文本? – bowfinger 2015-03-02 17:46:54