我有2个表在sql中有一列,我想将数据合并到一个有2列的新表中,
我需要在第一个表的新表328中合并700个记录,从第二个表合并372个记录。应该有2套350,每套350条,第一张表记录164条,第二张表记录186条,记录随机性好。
我写了这段代码第一批350条记录具有良好的随机性,但第二批350条记录具有1-164' V86'记录和165-350' V2T' [无随机性]
它运行两次的同一个循环,我试过但无法弄明白。任何人都可以帮助我吗?
private void QUEMerge()
{
DataTable ae50braQUE = aeBL.AE50BraQUE();
DataTable ae50SwimQUE = aeBL.AE50SwimQUE();
int rowAE50BraQue = 0;
int rowAE50SwimQue = 0;
int a = 0;
int b = 0;
int ttlque = 0;
for (int i = 0; i < 2; i++)
{
int temp1 = 0;
int temp2 = 0;
for (int j = 0; j < 350; j++)
{
rowAE50BraQue = rowAE50BraQue + 3;
rowAE50SwimQue = rowAE50SwimQue + 4;
for (; a < rowAE50BraQue; a++)
{
if (temp1 >= 164 || ttlque >= 700)
{
break;
}
DataRow dr = ae50braQUE.Rows[a];
aeBL.MergeQUE(dr["Coupon"].ToString(), "V86");
temp1++;
ttlque++;
}
for (; b < rowAE50SwimQue; b++)
{
if (temp2 >= 186 || ttlque >= 700)
{
break;
}
DataRow dr = ae50SwimQUE.Rows[b];
aeBL.MergeCouponsQUE(dr["Coupon"].ToString(), "V2T");
temp2++;
ttlque++;
}
}
}
}
答案 0 :(得分:0)
您只能使用t-sql执行此操作:
WITH cte as
(
SELECT a.a, b.b, ROW_NUMBER() OVER(PARTITION BY a.a ORDER BY NEWID()) as rn
FROM (VALUES ('x'),('y'),('z')) AS a(a)
CROSS JOIN (VALUES ('a'),('b'),('c'),('d')) as b(b)
)
--INSERT INTO NewTable(a, b)
SELECT TOP(5) a, b
FROM cte
WHERE rn in (1,2)
ORDER BY NEWID();
其中a
- table1
,b
- table2
,TOP(5)
- 在您的情况下TOP(700)
P.S。此查询完全独立,因此您可以运行它来测试结果
答案 1 :(得分:0)
这是我固定的工作代码..
private void QUEMerge()
{
DataTable ae50braQUE = aeBL.AE50BraQUE();
DataTable ae50SwimQUE = aeBL.AE50SwimQUE();
int x = 0;
int y = 0;
//int ttlque = 0;
for (int i = 0; i < 2; i++)
{
int rowAE50BraQue = 0;
int rowAE50SwimQue = 0;
int a = 0;
int b = 0;
int temp1 = 0;
int temp2 = 0;
for (int j = 0; j < 350; j++)
{
rowAE50BraQue = rowAE50BraQue + 3;
rowAE50SwimQue = rowAE50SwimQue + 4;
for (; a < rowAE50BraQue; a++)
{
if (temp1 >= 164)
{
break;
}
DataRow dr = ae50braQUE.Rows[x];
aeBL.MergeCouponsQUE(dr["Coupon"].ToString(), "V86");
temp1++;
x++;
//ttlque++;
}
for (; b < rowAE50SwimQue; b++)
{
if (temp2 >=186)
{
break;
}
DataRow dr = ae50SwimQUE.Rows[y];
aeBL.MergeCouponsQUE(dr["Coupon"].ToString(), "V2T");
temp2++;
y++;
// ttlque++;
}
}
}