【问题标题】:TSQL: Grouping LARGE amounts of dataTSQL:对大量数据进行分组
【发布时间】: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


【解决方案1】:

我想这就是你的想法:

;with types as (
select
distinct formtype,
row_number()over (order by formtype) as GroupID
from 
(select distinct formtype from forms) t1)


select
f.formid,
f.formname,
f.formtype,
types.GroupID
from
forms f
inner join types
on f.formtype = types.formtype

CTE 生成表单类型的组 id,然后您只需将其加入表单表。

SQL Fiddle

【讨论】:

  • 哦,亲爱的,还有一个“SQL Fiddle”!好的!这几乎是我需要的——它没有考虑到这些领域。表单之间的字段数需要匹配以及特定列数据(即:FieldLabel、IsRequired、OtherFields 等) - 感谢 Andrew。
  • 当,是的。重新阅读后,不完全确定我是否理解您对字段表的要求。
  • 基本上和你对表单所做的一样,但是一些需要匹配的列也在Fields表中。我想知道我是否只是在 cte 中添加一个 INNER JOIN 是否会这样做。
  • formid 4 会与 formid 1 在同一个 groupid 中吗?
  • 否,因为 FormName 不同。我更新了你的小提琴,这样你就不必为字段表输入所有这些插入 =)
猜你喜欢
  • 2015-05-02
  • 2012-08-04
  • 2011-05-11
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多