C# 中更新表的 UDF(将所有数据除以查询结果)
有没有办法将表的每个数据(所有列和行)除以
select count(*) from table;
例如
表
a1 a2 a3 a4
-------------------
438 498 3625 3645
500 291 5000 2351
233 263 1298 1687
198 117 1744 5932
438 498 3625 3648
500 291 5000 2637
,然后将每个数据除以 6(行数)
a1 a2 a3 a4
------------------------
73.0 83.0 604.16 607.5
83.33 48.5 833.33 391.83
...
...
...
...
一个问题是我的数据是 INT,但转换后它们会是双重的...
在 c# 中我正在做:
int N = 0;
string sql = @"select count(*) from 'ExampleTable'";
N = (int)cmd.ExecuteScalar();
int Columns = 0;
sql = @"select count(*) from information_schema.COLUMNS WHERE TABLE_NAME = 'ExampleTable'";
Columns = (int)cmd.ExecuteScalar();
cmdLoad.CommandText = "SELECT * FROM [ExampleTable]";
int i,j;
double[,] newTable = new double[N, Columns];
using (SqlDataReader reader = cmdLoad.ExecuteReader())
{
while (reader.Read())
{
for ( i = 1; i <= N; j++)
{
for ( j = 1; j <= Columns; j++)
{
newTable[i,j] = reader.GetDouble((i*j) - 1) / N ;
} //j
} //i
}//reader
}
但我不确定...
Is there a way to divide each data of a table (all columns and rows) by
select count(*) from table;
so for example
table
a1 a2 a3 a4
-------------------
438 498 3625 3645
500 291 5000 2351
233 263 1298 1687
198 117 1744 5932
438 498 3625 3648
500 291 5000 2637
and then divide each of them by 6 which is the number of rows
a1 a2 a3 a4
------------------------
73.0 83.0 604.16 607.5
83.33 48.5 833.33 391.83
...
...
...
...
One problem is that my data are INT, but after transform they would be double...
In c# I was doing:
int N = 0;
string sql = @"select count(*) from 'ExampleTable'";
N = (int)cmd.ExecuteScalar();
int Columns = 0;
sql = @"select count(*) from information_schema.COLUMNS WHERE TABLE_NAME = 'ExampleTable'";
Columns = (int)cmd.ExecuteScalar();
cmdLoad.CommandText = "SELECT * FROM [ExampleTable]";
int i,j;
double[,] newTable = new double[N, Columns];
using (SqlDataReader reader = cmdLoad.ExecuteReader())
{
while (reader.Read())
{
for ( i = 1; i <= N; j++)
{
for ( j = 1; j <= Columns; j++)
{
newTable[i,j] = reader.GetDouble((i*j) - 1) / N ;
} //j
} //i
}//reader
}
but I am not sure...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您提前知道这些列,则非常简单:
如果不知道,则需要有效地执行在 C# 中所做的操作 - 循环遍历列并构建动态查询
If you know the columns in advance, it's pretty easy:
If not, you'll need to do effectively what you're doing in C# - loop through the columns and build a dynamic query