久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在動態查詢中將行值連接到列名

How to join row values to column names in a dynamic query(如何在動態查詢中將行值連接到列名)
本文介紹了如何在動態查詢中將行值連接到列名的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在開發一個允許配置問題和答案的應用程序.目前最多可以有 20 個答案,但可能更少.

I am developing an application that allows configurable questions and answers. Currently there can be up to 20 answers, but possibly less.

我的結構如下:

+----+--------+--------------+-------------+
| ID | FormId | QuestionText | AnswerField |
+----+--------+--------------+-------------+
|  1 |      1 | Name         | Answer01    |
|  2 |      1 | Address      | Answer02    |
|  3 |      1 | Phone        | Answer03    |
|  4 |      1 | Email        | Answer04    |
|  5 |      2 | First Name   | Answer01    |
|  6 |      2 | Surname      | Answer02    |
+----+--------+--------------+-------------+

答案

+----+--------+----------+------------+--------------+--------------+----------------+----------+----------+
| ID | FormId | RecordId |  Answer01  |   Answer02   |   Answer03   |    Answer04    | Answer05 | Answer06 |
+----+--------+----------+------------+--------------+--------------+----------------+----------+----------+
|  1 |      1 |        1 | Bob Smith  | Bobs Address | 01234 111222 | bob@smith.com  | Null     | Null     |
|  2 |      1 |        2 | Joe Bloggs | Joes Address | 04321 333444 | joe@bloggs.com | Null     | Null     |
|  3 |      2 |        3 | David      | Jones        | Null         | Null           | Null     |          |
+----+--------+----------+------------+--------------+--------------+----------------+----------+----------+

因此在問題表中的 AnswerField Answer01 映射到 Answers 表中的 Answer01 列

So in the Questions table AnswerField Answer01 maps to the Answer01 column in the Answers table

我想做的是得到一個看起來像這樣的結果集:

What I would like to do is get a result set that looks something like:

對于表單 ID 1 &記錄 ID 1:

For form ID 1 & record ID 1:

+--------------+---------------+
| QuestionText |    Answer     |
+--------------+---------------+
| Name         | Bob Smith     |
| Address      | Bobs Address  |
| Phone        | 01234 111222  |
| Email        | bob@smith.com |
+--------------+---------------+

然后對于表單 id 2 &記錄 ID 3:

Then for form id 2 & record id 3:

+--------------+---------+
| QuestionText | Answer  |
+--------------+---------+
| First Name   | David   |
| Surname      | Jones   |
+--------------+---------+

我曾嘗試使用數據透視表:

I have tried using a pivot table:

SELECT QuestionText, Answer01, Answer02, Answer03, Answer04
FROM (
    SELECT DISTINCT Q.AnswerField, Q.QuestionText, Q.ID, A.Answer01, A.Answer02, A.Answer03, A.Answer04
    FROM Questions Q
    INNER JOIN Answers A ON A.FormId= Q.FormId
    WHERE A.ID = 17
) 
AS src
PIVOT (MAX(question_id) FOR Answer IN(answer_01, answer_02, answer_03, answer_04)) AS pvt

但這會重復所有列中的答案:

But this repeats the answers in all columns:

+--------------+-----------+--------------+--------------+---------------+
| QuestionText | Answer01  |   Answer02   |   Answer03   |   Answer04    |
+--------------+-----------+--------------+--------------+---------------+
| Name         | Bob smith | Bobs Address | 01234 111222 | bob@smith.com |
| Address      | Bob smith | Bobs Address | 01234 111222 | bob@smith.com |
| Phone        | Bob smith | Bobs Address | 01234 111222 | bob@smith.com |
| Email        | Bob smith | Bobs Address | 01234 111222 | bob@smith.com |
+--------------+-----------+--------------+--------------+---------------+

這顯然是不對的.

誰能建議如何在 SQL Server 存儲過程中完成此操作?

Can anyone suggest how this might be done in a SQL Server stored procedure please?

推薦答案

首先,您的 Answers 表設計得非常糟糕.該表未規范化,這會在您想要返回數據時給您帶來問題.如果可能,您需要重構該表.

First things first, your Answers table is terribly designed. That table is not normalized which is going to cause you problems when you want to return data. If possible, you need to restructure that table.

如果您無法重新設計表格,那么您將不得不取消旋轉答案表格,以便能夠輕松地加入問題的答案.

If you cannot redesign the table, then you will have to unpivot the answers table to be able to easily join the answers to the the questions.

UNPIVOT 會將您的列轉換為行.逆透視代碼將是:

An UNPIVOT will take your columns and convert them into rows. The unpivot code will be:

select formid, RecordId, answer, answercol
from answers a
unpivot
(
  answer
  for answerCol in ([Answer01], [Answer02], [Answer03], 
                    [Answer04], [Answer05], [Answer06])
) unpiv;

請參閱SQL Fiddle with Demo.這給出了一個結果:

See SQL Fiddle with Demo. This gives a result:

| FORMID | RECORDID |         ANSWER | ANSWERCOL |
--------------------------------------------------
|      1 |        1 |      Bob Smith |  Answer01 |
|      1 |        1 |   Bobs Address |  Answer02 |
|      1 |        1 |   01234 111222 |  Answer03 |
|      1 |        1 |  bob@smith.com |  Answer04 |

一旦數據成行,就可以加入問題表,返回你想要的結果:

Once the data is in rows, then you can join the questions table to return the result that you want:

select q.questiontext, d.answer
from questions q
inner join
(
  select formid, RecordId, answer, answercol
  from answers a
  unpivot
  (
    answer
    for answerCol in ([Answer01], [Answer02], [Answer03], 
                      [Answer04], [Answer05], [Answer06])
  ) unpiv
) d
  on q.AnswerField = d.answercol
  and q.formid = d.formid
where d.recordid = 1;

請參閱SQL Fiddle with Demo.這給出了一個結果:

See SQL Fiddle with Demo. This gives a result:

| QUESTIONTEXT |        ANSWER |
--------------------------------
|         Name |     Bob Smith |
|      Address |  Bobs Address |
|        Phone |  01234 111222 |
|        Email | bob@smith.com |

這篇關于如何在動態查詢中將行值連接到列名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數據)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何將“2016-07-01 01:12:22 PM轉換為“2016-07-01 13:12:22小時格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
主站蜘蛛池模板: 欧美成人手机在线 | 欧美福利三区 | 国产精品欧美精品 | 特黄级国产片 | 亚洲一区精品在线 | 亚洲国产激情 | 成人午夜影院 | 少妇一区在线观看 | 精品国产一区二区三区日日嗨 | 国产精品久久久久久久7电影 | 亚洲成人免费 | 精品麻豆剧传媒av国产九九九 | 国产精品高潮呻吟久久 | 黄色网址免费看 | 亚洲精品乱码久久久久久9色 | 狠狠av | 宅男噜噜噜66一区二区 | 亚洲夜夜爽 | 在线观看视频一区二区三区 | 日本免费一区二区三区 | 日韩精品在线看 | 国外成人在线视频网站 | 国产中文字幕在线 | 成人一级片在线观看 | 婷婷国产一区 | 色毛片 | 国产精品免费一区二区 | 日本一级淫片免费啪啪3 | 国产中文字幕av | 在线观看成人 | 久久r免费视频 | 国产精品99久久久久久大便 | 超碰免费观看 | 午夜免费看 | 久久久久久亚洲精品 | 日本成人在线网址 | a成人| 日韩成人国产 | www.99久久.com| 欧美性久久| 国产一区三区视频 |