Results 1 to 2 of 2

Thread: Insert as Zero

  1. #1
    Join Date
    Jan 2008
    Posts
    1

    Smile Insert as Zero

    Hi,

    I have a table with columns
    distributor_id ,
    brand_id,
    year,
    month,
    total_sales


    distributor_id ,
    brand_id,
    year,
    month are my primary keys.

    I have records for last 3 years of data.

    I have distinct records till last year as
    Some are till last month (Current month - 1)
    some are till feb 2007.......
    some are till jan 2007..


    Now i want to insert records to all distinct
    distributor_id ,
    brand_id,
    year,
    month
    'Zero ' for sales

    where i have to take max month + 1 and hardcode as zero till current month + 10 months.

    ( I need to insert zero for sales for all records till current month + 10 months)

    i appreciate if you get me a insert script without cursors.

    Rishi

  2. #2
    Join Date
    Dec 2007
    Posts
    7
    Code:
    insert into yourtable
    select distributor_id , brand_id 
    ,year(dateadd(mm,number,maxm)), month(dateadd(mm,number,maxm)) ,0
    from (
    select distributor_id , brand_id 
    ,convert(char(8),max((year*100+month)*100+1)) maxm
    ,datediff(mm
     ,convert(char(8),max((year*100+month)*100+1))
     ,dateadd(mm,10,getdate())) months
    from yourtable
    group by distributor_id , brand_id
    )a
    join
    master..spt_values 
    on type='P'
      and number between 1 and months

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •