简体   繁体   English

sql-为表中的每个客户id插入新行

[英]sql - insert a new row for every customer id that is in the table

I am using SQL server 2008. The following is my table: 我正在使用SQL Server2008。下表是我的表:

Cust_id  Date           rate    type
3579    12/05/2014     0.01256   f
5200    13/05/2014     0.02365   f
1234    18/05/2014     0.05623   m
3456    23/05/2014     0.01254   m
7896    23/05/2014     0.01254   f
2589    21/05/2014     0.01254   m
3698    23/05/2014     0.01254   f

I would like to select all the customers that are type 'm' (that is easy enough) and I would like to insert a new row of Date,rate and type for each customer that is found. 我想选择所有类型为'm'的客户(这很容易),我想为找到的每个客户插入新的Date,rate和type行。 so the result would look something like this: 所以结果看起来像这样:

Cust_id  Date           rate     type
1234    18/05/2014      0.05623   m
1234    16/05/2014      0.02222   f
3456    23/05/2014      0.01254   m
3456    27/05/2014      0.03333   f
2589    21/05/2014      0.01254   m
2589    20/05/2014      0.01111   f

I will not be able to insert rows individually because I have over 5000 customers :-(. Could some experts assist me. 我将无法单独插入行,因为我有5000多个客户:-(。有些专家可以协助我。

Thanks a ton in advance. 在此先感谢一吨。

If you can compute required Date , Rate and Type from selected Date , Rate and Type you can use insert..select construction: 如果可以从选定的 DateRateType计算所需的 DateRateType ,则可以使用insert..select构造:

insert into Customers(
  Cust_id, 
  Date,
  Rate,
  Type)

  select Cust_id, 
         Date, -- TODO:Generate required Date from given one
         Rate, -- TODO:Generate required Rate from given one
         Type  -- TODO:Generate required Type from given one
    from Customers 
   where Type = 'm' 

看看INSERT ... SELECT语法

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

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