简体   繁体   English

一对多联接,多个记录作为重复的列

[英]One-to-many join with multiple records as repeated columns

Using MS SQL Server. 使用MS SQL Server。 I have to build a view with columns from two entities that have a one-to-many relationship. 我必须使用具有一对多关系的两个实体的列来构建视图。 The primary entity is Performance Reports. 主要实体是绩效报告。 The table should have one performance report per row. 该表每行应有一份绩效报告。

The related entity is Performance Report Logs. 相关实体是性能报告日志。 Each record includes one Problem and an unlimited number of Inputs. 每条记录包括一个问题和无限数量的输入。 This structure was chosen in order to be able to link certain problems with certain inputs, rather than just having a list of all problems and all inputs in the Performance Report entity itself. 选择该结构是为了能够将某些问题与某些输入相关联,而不仅仅是在性能报告实体本身中列出所有问题和所有输入的列表。 Each Performance Report Log is linked to a Performance Report via lookup. 每个性能报告日志都通过查找链接到性能报告。

I would like the view to have the columns from the main Performance Report entity, followed by the columns from all related Performance Report Logs. 我希望视图具有主要绩效报告实体中的列,然后是所有相关绩效报告日志中的列。 So, if a Performance Report has three Logs, the columns from all three logs should be included in the same row, rather than three rows. 因此,如果性能报告具有三个日志,则所有三个日志的列应包括在同一行中,而不是三行中。 They should also be in separate columns, not merged into one as in group_concat. 它们还应该位于单独的列中,而不是像group_concat中那样合并为一列。

Thank you! 谢谢!

Is it truly "an unlimited number of Inputs"? 真的是“无限数量的输入”吗? If this is an overstatement and you might only have 10-20 inputs, you could use a SELECT statement for each Input column you wish to display. 如果这是一个夸大声明,并且您可能只有10到20个输入,则可以对要显示的每个Input列使用SELECT语句。 Ie,

select performance.field1, performance.field2,  
(select field from inputs where problem='x' and inputs.id=performance.pk) as X, 
(select field from inputs where problem='y' and inputs.id=performance.pk) as Y, 
etc...
        from Performance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM