SQL Server 2005 中的水平到垂直表转换
我有一个页面,可以从表中动态构建表单输入字段,允许用户将输入输入到表单中。每个输入都是 _info 表中的一个字段。
我想要在一行中显示用户的所有条目的报告。每个字段条目都是一行。所以我需要获取垂直数据并将其水平化。有许多不同的形式和不同的配置,因此必须是动态的。我读过很多关于交叉表数据透视的文章,但它们只适用于聚合/求和。
Registration_Info 表对于一份表单提交有多个条目。我需要列出这些水平,并将每个 form_field.label 作为其字段名称以进行报告。
下面是一个表格布局示例:
FORM
——————————————
FORM_ID|FORM_NAME
500|“Custom Form 1″
FORM_FIELD
——————————————
FORM_FIELD_ID|FORM_ID|LABEL
1|500|“Field 1″
2|500|“Field 2″
REGISTRATION
——————————————
REG_ID|FORM_ID|USER_ID
23|500|45
24|500|98
REGISTRATION_INFO
——————————————
REG_ID|FORM_FIELD_ID|FIELD_VALUE
23|1|“My Name”
24|2|“My City”
Required Report Output:
REG_ID |Field 1 |Field 2 |FORM_ID |USER_ID
———————————————————————————————
23 |My Name |My City |500 |45
24 |Another Name |Another City |500 |98
I have a page that dynamically builds forms input fields from a table allowing users to enter input into the form. Each input is a field in a _info table.
I would like report showing all of the entries of a user in one line. Each field entry is a row. So I need to take the vertical data and make it horizonal. There are many different forms with different configurations so must be dynamic. I’ve come across a lot of articles for cross-tab pivots but they only work for aggragates/sums.
The Registration_Info table has multiple entries for one form submission. I need to list these horazontal with each form_field.label as its fieldname for reporting.
Below is an example table layout:
FORM
——————————————
FORM_ID|FORM_NAME
500|“Custom Form 1″
FORM_FIELD
——————————————
FORM_FIELD_ID|FORM_ID|LABEL
1|500|“Field 1″
2|500|“Field 2″
REGISTRATION
——————————————
REG_ID|FORM_ID|USER_ID
23|500|45
24|500|98
REGISTRATION_INFO
——————————————
REG_ID|FORM_FIELD_ID|FIELD_VALUE
23|1|“My Name”
24|2|“My City”
Required Report Output:
REG_ID |Field 1 |Field 2 |FORM_ID |USER_ID
———————————————————————————————
23 |My Name |My City |500 |45
24 |Another Name |Another City |500 |98
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这会解决您的需求:
STATIC PIVOT
这是一个sqlfiddle 具有工作版本。
动态枢轴
此版本将首先获取字段列表,然后对它们进行透视。
I believe this will solve your needs:
STATIC PIVOT
Here is a sqlfiddle with a working version.
DYNAMIC PIVOT
This version will get the list of the Field first and then pivot them.