Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. T-Sql Distribute data into groups based on existing numbers

T-Sql Distribute data into groups based on existing numbers

Scheduled Pinned Locked Moved Database
helpcssdatabasequestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dreaddan101
    wrote on last edited by
    #1

    Hi, i've been looking at moving one of our processed from excel (+vba) into t-sql to make life easier but am stuck. We have lots of tasks that are assigned to work groups which we want to distribute evenly across the work groups. This is a simple task for ntile.. However when these tasks are no longer required they are removed which leaves the groups uneven. When new tasks are added we want to still try to keep these groups balanced. EG Existing groups :

    GroupName - Task Count
    Group1 - 1000
    Group2 - 999
    Group3 - 998

    If we were to add 6 new tasks they would have more assigned to Group 2 & 3 as they have less than group 1.

    Task 1 - Group3
    Task 2 - Group3
    Task 3 -Group2
    Task 4 - Group1
    Task 5 - Group2
    Task 6 - Group3

    Sample tables

    Create table GroupTable
    (GroupID int, Name varchar(200) )
    Insert into GroupTable values (1,'Group1')
    Insert into GroupTable values (2,'Group2')
    Insert into GroupTable values (3,'Group3')

    Create table Jobs(jobid int identity(1,1), name varchar(100),Groupid int)
    --Existing tasks
    Insert into Jobs(name,Groupid) values ('Task1',1)
    Insert into Jobs(name,Groupid) values ('Task2',1)
    Insert into Jobs(name,Groupid) values ('Task3',1)
    Insert into Jobs(name,Groupid) values ('Task4',1)
    Insert into Jobs(name,Groupid) values ('Task5',2)
    Insert into Jobs(name,Groupid) values ('Task6',2)
    Insert into Jobs(name,Groupid) values ('Task6',2)
    Insert into Jobs(name,Groupid) values ('Task7',3)

    -- New tasks
    Insert into Jobs(name) values ('TaskA')
    Insert into Jobs(name) values ('TaskB')
    Insert into Jobs(name) values ('TaskC')
    Insert into Jobs(name) values ('TaskD')
    Insert into Jobs(name) values ('TaskE')
    Insert into Jobs(name) values ('TaskF')

    This gives us 6 unassigned tasks and a uneven group assignment

    GROUPNAME TASK_COUNT
    6
    Group1 4
    Group2 3
    Group3 2

    This means the new tasks will be assigned like this

    TaskA - Group3
    TaskB - Group3
    TaskC - Group2
    TaskD - Group1
    TaskE - Group2
    TaskF - Group3

    Can anyone help? Thanks Dan

    J 1 Reply Last reply
    0
    • D dreaddan101

      Hi, i've been looking at moving one of our processed from excel (+vba) into t-sql to make life easier but am stuck. We have lots of tasks that are assigned to work groups which we want to distribute evenly across the work groups. This is a simple task for ntile.. However when these tasks are no longer required they are removed which leaves the groups uneven. When new tasks are added we want to still try to keep these groups balanced. EG Existing groups :

      GroupName - Task Count
      Group1 - 1000
      Group2 - 999
      Group3 - 998

      If we were to add 6 new tasks they would have more assigned to Group 2 & 3 as they have less than group 1.

      Task 1 - Group3
      Task 2 - Group3
      Task 3 -Group2
      Task 4 - Group1
      Task 5 - Group2
      Task 6 - Group3

      Sample tables

      Create table GroupTable
      (GroupID int, Name varchar(200) )
      Insert into GroupTable values (1,'Group1')
      Insert into GroupTable values (2,'Group2')
      Insert into GroupTable values (3,'Group3')

      Create table Jobs(jobid int identity(1,1), name varchar(100),Groupid int)
      --Existing tasks
      Insert into Jobs(name,Groupid) values ('Task1',1)
      Insert into Jobs(name,Groupid) values ('Task2',1)
      Insert into Jobs(name,Groupid) values ('Task3',1)
      Insert into Jobs(name,Groupid) values ('Task4',1)
      Insert into Jobs(name,Groupid) values ('Task5',2)
      Insert into Jobs(name,Groupid) values ('Task6',2)
      Insert into Jobs(name,Groupid) values ('Task6',2)
      Insert into Jobs(name,Groupid) values ('Task7',3)

      -- New tasks
      Insert into Jobs(name) values ('TaskA')
      Insert into Jobs(name) values ('TaskB')
      Insert into Jobs(name) values ('TaskC')
      Insert into Jobs(name) values ('TaskD')
      Insert into Jobs(name) values ('TaskE')
      Insert into Jobs(name) values ('TaskF')

      This gives us 6 unassigned tasks and a uneven group assignment

      GROUPNAME TASK_COUNT
      6
      Group1 4
      Group2 3
      Group3 2

      This means the new tasks will be assigned like this

      TaskA - Group3
      TaskB - Group3
      TaskC - Group2
      TaskD - Group1
      TaskE - Group2
      TaskF - Group3

      Can anyone help? Thanks Dan

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      Insert new tasks like this:

      insert into jobs
      select top(1)
      @TaskName,
      groupid
      from jobs
      group by groupid
      order by count(*),groupid

      Wrong is evil and must be defeated. - Jeff Ello[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups