透视sql来创建自定义数据表?
我目前正在开发一个 C# 项目,并使用 MySql 作为数据库。我目前在创建要在报告中显示的自定义数据表时遇到问题。 额外信息:测试可以有任何单元格和单元格的组合。用于测试的阀门 但对于一项测试,它使用在每次测试之前预定义的一组值和单元格范围。
结果表(示例)
TestID | CellID | ValveID | OutputValue1| OutputValue2 |
1 | 1 | 1 | 2.5 | 0.12 |
1 | 1 | 1 | 2.5 | 0.12 |
1 | 1 | 2 | 2.8 | 0.13 |
1 | 2 | 1 | 3.2 | 0.12 |
1 | 2 | 2 | 3.5 | 0.12 |
1 | 2 | 4 | 4.1 | 0.14 |
因此,对于示例,在此特定示例中未测试阀门 3。但如果需要的话可以使用。 基本上我的表行需要根据此结果表动态生成。
报告预期
Test ID = 1
Valves
| 1 | 2 | 4 |
CellID |Out1 | Out2| Out1| Out2|Out1 | Out2|
1 | 2.5 | 0.12| 2.8 | 0.13| 2.9 | 0.12|
2 | 3.2 | 0.12| 3.5 | 0.12| 4.1 | 0.14|
输出 1 = 输出值 1 Out2 = 输出值2 任何人都可以帮助我引导我找到一个好的链接或解释如何获取这种动态数据表
I am currently working on a project in C# and use MySql as database. I am currently having a problem with creating a custom DataTable I want to show in my Report.
Extra Info : A test can have any no combinations of Cells & Valves for a test
But for one test it uses set range of Vales and Cells are predefined before each test.
Results Table (Sample)
TestID | CellID | ValveID | OutputValue1| OutputValue2 |
1 | 1 | 1 | 2.5 | 0.12 |
1 | 1 | 1 | 2.5 | 0.12 |
1 | 1 | 2 | 2.8 | 0.13 |
1 | 2 | 1 | 3.2 | 0.12 |
1 | 2 | 2 | 3.5 | 0.12 |
1 | 2 | 4 | 4.1 | 0.14 |
So for a example Valve 3 is not tested at this particular example. But can use if wanted.
Basically my table rows are dynamically need generate based on this results table.
Report Expected
Test ID = 1
Valves
| 1 | 2 | 4 |
CellID |Out1 | Out2| Out1| Out2|Out1 | Out2|
1 | 2.5 | 0.12| 2.8 | 0.13| 2.9 | 0.12|
2 | 3.2 | 0.12| 3.5 | 0.12| 4.1 | 0.14|
Out1 = OutputValue1
Out2 = OutputValue2
Can any one help me by direct me to a good link or explanation how to get this kind dynamic Data Table
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个sql代码。我重新创建了一个类似于您原始表格的虚拟表格。还需要创建一个 UDF。剩余的代码可以作为存储过程的一部分进入。
它的作用是返回每个 Valveid 的输出值的分号分隔值。
当然,在前端,您必须编写代码将 ValveID 标识为分号 (;) 和哈希 (#) 之间的数字。
以下是上述 SQL 产生的输出:
Try this sql code. I have recreated a dummy table resembling your original table. There is a UDF that needs to be created as well. The remaining code can go in as a part of stored proc.
What it does is it returns a semicolon-separated values of output values for each valveid.
Of course in the front end, you'll have to write code that identifies valveid as the number between a semicolon (;) and a hash(#).
Here is the output that the above SQL produces:
我根本没有测试过这一点,但我认为在 Sql 中对您提供的数据执行类似的操作。
一旦数据加载到 DataTable 中,Can 还可以用 C# 操作服务器上的数据。 这里(快速谷歌)有一个类似的示例,它展示了如何在代码中创建数据表。显然你的要求不仅仅是直枢轴,但原理是一样的。
I haven't tested this at all, but I think it's something like this to do it in Sql for the data you provided.
Can could also manipulate the data on the server in C# once it's loaded into a DataTable. There's an example of something similar here (quick google), which shows how to create DataTables in code. Obviously your requirement is more than a straight pivot, but it's the same principle.