【发布时间】:2015-02-13 07:03:22
【问题描述】:
我正在处理一个项目,我需要对如下所示的记录进行分组,我需要一些帮助...
这是当前的结构:
表格:表格
FormID FormName FormType GroupID <<< New column
----------------------------------------------
1 Form1 1
2 Form1 1
3 Form2 2
4 Form2 2
5 Form2 2
表格:字段
FieldID FormID FieldLabel IsRequired OtherField...
----------------------------------------------------------------------------
1 1 Label1 1 x
2 1 Label2 0 y
3 1 Label3 0 z
4 2 Label1 1 x
5 2 Label2 0 y
6 2 Label3 0 z
7 3 Label1 1 x
8 3 Label2 0 y
9 3 Label3 0 z
10 4 Label1 1 x
11 4 Label2 0 y
12 4 Label3 0 z
13 5 Label1 1 a
14 5 Label2 0 b
15 5 Label3 0 c
所以我需要做的就是将表单和字段组合在一起,看看它们是否完全一样——甚至是数据——这让我很困惑。例如,表格 1 和 2 将被组合在一起,因为 FormName 和 FormType 匹配,并且在字段表中 FieldLabel、IsRequired 和“OtherField”都匹配。
但是,即使 Forms 3、4 和 5 在 forms 表上都匹配,但只有 Forms 3 和 4 最终会在同一个组中,因为 Fields 表中的数据(OtherFields)在这些列上并不相同。
Forms 表的期望结果(特别是“GroupID”列):
FormID FormName FormType GroupID
----------------------------------------------
1 Form1 1 1
2 Form1 1 1
3 Form1 2 2
4 Form2 2 2
5 Form2 2 3
如何做到这一点?我不介意使用游标等,因为这是填充新“GroupID”列的一次性交易。
谢谢大家!
编辑
这是 Andrew 创建的小提琴:http://sqlfiddle.com/#!3/3ec6f/6
【问题讨论】:
-
在表格'Forms'中,FormID = 5 and FormName = Form2的'FormName'与最后一行'FormID' = 5 and 'FormName' =的结果不匹配Form5,报错了吗?
-
您的意思是第一个“表单”表与我的“所需结果”不匹配 - 是的,这是一个错误 - 为了准确起见,我会修复。
标签: sql-server tsql