Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 820519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:29:17+00:00 2026-05-15T02:29:17+00:00

I have an sql scenario as follows which I have been trying to improve.

  • 0

I have an sql scenario as follows which I have been trying to improve.

There is a table ‘Returns’ which is having ids of the returned goods against a shop for an item. Its structure is as below.

Returns
-------------------------
Return ID | Shop | Item
-------------------------
  1         Shop1  Item1
  2         Shop1  Item1
  3         Shop1  Item1
  4         Shop1  Item1
  5         Shop1  Item1

There is one more table Supplier with Shop, supplier and Item as shown below.

Supplier
---------------------------------
Supplier | Shop | Item  | Volume
---------------------------------
  supp1    Shop1   Item1    20%
  supp2    Shop1   Item1    80%

Now as you see supp1 is supplying 20 % of total item1 volume and supp2 is supplying 80% of Item1 to shop1. And there were 5 return of items against the same Item1 for same Shop1.
Now I need to allocate any four return IDs to Supp1 and remaining one return Id to supp2. This allocation of numbers is based on the ratio of the supplied volume percentage of the supplier. This allocation varies depending on the ratio of volume of supplied items.

Now I have tried a method of using RANKs as shown below by use of temp tables.

temp table 1 will have Shop, Return Id, Item, Total count of return IDs and Rank of the return id.

temp table 2 will have shop, Supplier, Item and his proportion and rank of proportion.

Now I am facing the difficulty in allocating top return ids to top supplier as illustrated above. As SQL doesnt have loops how can I achieve this. I have been tying several ways of doing this.

My environment is Teradata (ANSI SQL is enough).

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:29:18+00:00Added an answer on May 15, 2026 at 2:29 am

    UPDATE:
    You need to loop, here is some SQL code you can use as a starting point.
    Basically I use temporary tabes and ROW_NUMBER().
    For my sample I used SQL SERVER 2008.

    Try the following:

    -- gather suppliers in temp table
    DECLARE @SupplierTemp table  
            (  [RowId] int
              ,[Supplier] nvarchar (50)
              ,[ReturnCount] int ) 
    
    -- gather supplier with return count
    INSERT INTO @SupplierTemp
      SELECT ROW_NUMBER() OVER(ORDER BY [Supplier].[Supplier] DESC, [Supplier].[Supplier]) 
            ,[Supplier].[Supplier]
            , COUNT([Supplier].[Supplier])*[Supplier].[Volume]/100 AS ReturnCount 
      FROM [Supplier] 
      INNER JOIN [Returns] ON (([Returns].[Item] = [Supplier].[Item])  
                             AND ([Returns].[Shop] = [Supplier].[Shop])) 
      GROUP BY [Supplier].[Supplier], [Supplier].[Volume]
      ORDER BY [Supplier].[Supplier] 
    
    -- gather returns in temp table
    DECLARE @ReturnsTemp table  
           (  [RowId] int
             ,[Id] int) 
    
    -- gather returns
    INSERT INTO @ReturnsTemp
      SELECT ROW_NUMBER() OVER(ORDER BY [Returns].[Id] DESC, [Returns].[Id]) 
            ,[Returns].[Id]
      FROM [Returns] 
    
    -- gather results in temp table
    DECLARE @ResultsTemp table  
           (  [Supplier] nvarchar(50)
             ,[Id] int) 
    
    DECLARE @rrowid as int
    DECLARE @rid as int
    
    -- loop over all suppliers
    -- loop once for each [ReturnCount]
    -- find the next avialable Id
    DECLARE @srowid as int
    DECLARE @loopCnt as int
    DECLARE @supplier as nvarchar(50)
    
    -- get first supplier   
    SELECT @srowid = (SELECT MIN([RowId]) FROM @SupplierTemp)
    SELECT @loopCnt = [ReturnCount] FROM @SupplierTemp WHERE [RowId] = @srowid
    SELECT @supplier = [Supplier] FROM @SupplierTemp WHERE [RowId] = @srowid
    
    -- loop over suppliers
    WHILE @srowid IS NOT NULL
      BEGIN
        -- loop of number of returns    
        WHILE @loopCnt > 0
          BEGIN
            -- find the Id to return
            SELECT @rrowid = (SELECT MIN([RowId]) FROM @ReturnsTemp)
            SELECT @rid = [Id] FROM @ReturnsTemp WHERE [RowId] = @rrowid
    
            INSERT INTO @ResultsTemp VALUES (@supplier, @rid)
    
            DELETE FROM @ReturnsTemp WHERE [RowId] = @rrowid
    
            SELECT @loopCnt = @loopCnt - 1
          END
    
        -- delete current item from table to keep loop moving forward...      
        DELETE FROM @SupplierTemp WHERE [RowId] = @srowid
    
        -- get next supplier.
        SELECT @srowid = (SELECT MIN([RowId]) FROM @SupplierTemp)
        SELECT @loopCnt = [ReturnCount] FROM @SupplierTemp WHERE [RowId] = @srowid
        SELECT @supplier = [Supplier] FROM @SupplierTemp WHERE [RowId] = @srowid
      END
    
    SELECT * FROM @ResultsTemp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a following scenario in my SQL Server 2005 database. zipcodes table has
There is a scenario, I have a SQL Server with tones of data in
I have the following scenario: An SQL 2000 database with a table containing the
My scenario is as follows: I have a table of data (handful of fields,
I am trying to write a SQL query that returns rows from a table
Problem Assumption: I have a table in SQL Server, with the structure as follows;
I have a table in SQL Server 2005, as follows, say, fields A, B,
I have a typical dev scenario: I have a SQL 2008 database that I
I have a particular scenario where I wrote my code using LINQ-SQL but I
Here's the scenario: You have an ASP.Net application supported by a Microsoft SQL Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.