透视sql来创建自定义数据表?

发布于 2024-09-24 19:22:48 字数 974 浏览 2 评论 0原文

我目前正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乜一 2024-10-01 19:22:48

试试这个sql代码。我重新创建了一个类似于您原始表格的虚拟表格。还需要创建一个 UDF。剩余的代码可以作为存储过程的一部分进入。

set xact_abort on
begin tran
CREATE TABLE TestTable
(
TestId int not null,
CellId int not null,
valveid int not null,
op1 numeric(5,2) not null,
op2 numeric (5,2) not null
)
go

CREATE FUNCTION GetOutputString
(
@TestId INT,
@CellId INT
)
RETURNS VARCHAR(1000)
AS
BEGIN
    DECLARE @output VARCHAR(1000)
    SELECT @output = ISNULL(@output + ';','')
                     +
                     CAST(valveid AS VARCHAR(5)) + '#'
                     +
                     CAST(op1 as VARCHAR(10))+ ','
                     +
                     CAST(op2 as VARCHAR(10))
      FROM TestTable
     WHERE TestId = @testid
       AND cellid = @cellid

     RETURN @output
END
go
-- insert dummy data
INSERT INTO TestTable
SELECT 1, 1, 1, 2.5, .12
UNION
SELECT 1, 1, 2, 2.8, .13
UNION
SELECT 1, 2, 1, 3.2, .12
UNION
SELECT 1, 2, 2, 3.5, .12
UNION
SELECT 1, 2, 4, 4.5, .13

-- create temp table/table variable depending on the size of the actual table.
DECLARE @tmp TABLE (
TestId int not null,
CellId int not null,
OutputString VARCHAR(1000) null
)

INSERT INTO @tmp (TestId, CellId)
SELECT TestId, CellId
FROM TestTable
GROUP BY TestId,CellId

UPDATE @tmp
   SET OutputString = dbo.GetOutputString(TestId, CellId)

SELECT * FROM @tmp
rollback

它的作用是返回每个 Valveid 的输出值的分号分隔值。
当然,在前端,您必须编写代码将 ValveID 标识为分号 (;) 和哈希 (#) 之间的数字。
以下是上述 SQL 产生的输出:

1   1   1#2.50,0.12;2#2.80,0.13
1   2   1#3.20,0.12;2#3.50,0.12;4#4.50,0.13

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.

set xact_abort on
begin tran
CREATE TABLE TestTable
(
TestId int not null,
CellId int not null,
valveid int not null,
op1 numeric(5,2) not null,
op2 numeric (5,2) not null
)
go

CREATE FUNCTION GetOutputString
(
@TestId INT,
@CellId INT
)
RETURNS VARCHAR(1000)
AS
BEGIN
    DECLARE @output VARCHAR(1000)
    SELECT @output = ISNULL(@output + ';','')
                     +
                     CAST(valveid AS VARCHAR(5)) + '#'
                     +
                     CAST(op1 as VARCHAR(10))+ ','
                     +
                     CAST(op2 as VARCHAR(10))
      FROM TestTable
     WHERE TestId = @testid
       AND cellid = @cellid

     RETURN @output
END
go
-- insert dummy data
INSERT INTO TestTable
SELECT 1, 1, 1, 2.5, .12
UNION
SELECT 1, 1, 2, 2.8, .13
UNION
SELECT 1, 2, 1, 3.2, .12
UNION
SELECT 1, 2, 2, 3.5, .12
UNION
SELECT 1, 2, 4, 4.5, .13

-- create temp table/table variable depending on the size of the actual table.
DECLARE @tmp TABLE (
TestId int not null,
CellId int not null,
OutputString VARCHAR(1000) null
)

INSERT INTO @tmp (TestId, CellId)
SELECT TestId, CellId
FROM TestTable
GROUP BY TestId,CellId

UPDATE @tmp
   SET OutputString = dbo.GetOutputString(TestId, CellId)

SELECT * FROM @tmp
rollback

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:

1   1   1#2.50,0.12;2#2.80,0.13
1   2   1#3.20,0.12;2#3.50,0.12;4#4.50,0.13
找个人就嫁了吧 2024-10-01 19:22:48

我根本没有测试过这一点,但我认为在 Sql 中对您提供的数据执行类似的操作。

Select s.CellId,
   (select s1.OutputValue1 From Sample s1 Where s1.CellId=s.CellId and s1.ValveId=1 and s1.TestId=s.TestId) as Valve1_Out1,
   (select s1.OutputValue2 From Sample s1 Where s1.CellId=s.CellId and s1.ValveId=1 and s1.TestId=s.TestId) as Valve1_Out2,
   (select s1.OutputValue1 From Sample s1 Where s1.CellId=s.CellId and s1.ValveId=2 and s1.TestId=s.TestId) as Valve2_Out1,
   (select s1.OutputValue2 From Sample s1 Where s1.CellId=s.CellId and s1.ValveId=2 and s1.TestId=s.TestId) as Valve2_Out2,
   (select s1.OutputValue1 From Sample s1 Where s1.CellId=s.CellId and s1.ValveId=4 and s1.TestId=s.TestId) as Valve4_Out1,
   (select s1.OutputValue2 From Sample s1 Where s1.CellId=s.CellId and s1.ValveId=4 and s1.TestId=s.TestId) as Valve4_Out2
 From Sample s
 Where s.TestId=1
 Group By s.CellId, s.TestId
 Order By s.CellId

一旦数据加载到 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.

Select s.CellId,
   (select s1.OutputValue1 From Sample s1 Where s1.CellId=s.CellId and s1.ValveId=1 and s1.TestId=s.TestId) as Valve1_Out1,
   (select s1.OutputValue2 From Sample s1 Where s1.CellId=s.CellId and s1.ValveId=1 and s1.TestId=s.TestId) as Valve1_Out2,
   (select s1.OutputValue1 From Sample s1 Where s1.CellId=s.CellId and s1.ValveId=2 and s1.TestId=s.TestId) as Valve2_Out1,
   (select s1.OutputValue2 From Sample s1 Where s1.CellId=s.CellId and s1.ValveId=2 and s1.TestId=s.TestId) as Valve2_Out2,
   (select s1.OutputValue1 From Sample s1 Where s1.CellId=s.CellId and s1.ValveId=4 and s1.TestId=s.TestId) as Valve4_Out1,
   (select s1.OutputValue2 From Sample s1 Where s1.CellId=s.CellId and s1.ValveId=4 and s1.TestId=s.TestId) as Valve4_Out2
 From Sample s
 Where s.TestId=1
 Group By s.CellId, s.TestId
 Order By s.CellId

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文