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 9196841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:56:20+00:00 2026-06-17T21:56:20+00:00

I am trying to make a query whereby I can generate a list for

  • 0

I am trying to make a query whereby I can generate a list for the top 10 items per item group per.
It is for a supermarket chain whereby all items are divided into item groups. E.g Rice, Salt etc. There will be different brands of Rice and Different brands for Salt.

I need to get the data from a SAP Business One Table (MS SQL Server 2008)

Sample results from the Item Group table, OITB

SELECT * FROM OITB (showing first two columns for item groups)

ItmsGrpCod  ItmsGrpNam
101         RICE
102         SALT
103         SUGAR
104         FROZEN VEGETABLE

This returns 224 results.

SELECT * FROM OITB (showing first two columns for items)

ItemCode    ItemName          ItmsGrpCod    
2001        A1 GRAIN RICE     101
2001        ASHA BRAND RICE   101
2003        PISHORI RICE      101
2004        B7 GRADE RICE     101
2019        JIM SALT          102
2020        KAYKAY SALT       102

I also have this query below that gets the top 10 items but I have to specify the Item group code (field ItmsGrpCod)

SELECT TOP 100 T0.ItemCode, T0.ItemName, T1.DocDate, T6.Price AS COST,P3.Price AS POS,
((P3.Price-T6.Price)/T6.Price)*100 AS [Markup %], T2.OnHand, SUM(T1.Quantity) 
AS Quantity, SUM(T1.LineTotal) AS SALES,T6.Price *SUM(T1.Quantity) AS [Sales 
Cost],SUM(T1.LineTotal) - T6.Price *SUM(T1.Quantity) AS [GP Amount],
(SUM(T1.LineTotal) - T6.Price *SUM(T1.Quantity))/(T6.Price *SUM(T1.Quantity))
*100 as [GP %],T3.WhsName FROM OITM T0
INNER JOIN INV1 T1 ON T0.ItemCode = T1.ItemCode 
INNER JOIN OITW T2 ON T0.ItemCode = T2.ItemCode 
INNER JOIN OWHS T3 ON T1.WhsCode = T3.WhsCode 
INNER JOIN OINV T4 ON T1.DocEntry = T4.DocEntry 
INNER JOIN OITB T5 ON T0.ItmsGrpCod = T5.ItmsGrpCod 
INNER JOIN ITM1 T6 ON T0.ItemCode = T6.ItemCode  
INNER JOIN (SELECT P1.ItemCode, P2.Price FROM OITM P1 
INNER JOIN ITM1 P2 ON P1.ItemCode=P2.ItemCode WHERE 
P2.PriceList='1') P3 ON P3.ItemCode=T0.ItemCode WHERE T6.PriceList ='2' 
AND T2.WhsCode = '01' AND T1.WhsCode = '01' AND T4.DocDate 
= CONVERT(VARCHAR, GETDATE() -1, 101)
AND T0.ItmsGrpCod = '103'  --(Item Group Code)
GROUP BY T0.ItemCode, T0.ItmsGrpCod, T0.ItemName, T1.DocDate, T6.Price, P3.Price, ((P3.Price-T6.Price)
/NULLIF(T6.Price, 0))* 100, T2.OnHand, T3.WhsName ORDER BY Quantity DESC

What I need is a query that will first get all the Item Group Codes from OITB and store them temporarily in an array, and then run the 2nd query for each Item Group Code. Assuming that there are at least 10 items in each item group, the query should return 2240 results. However, some item group have less than 10 items. I have really not used arrays in SQl so how would I build a query to get what I want? Any help appreciated.

  • 1 1 Answer
  • 0 Views
  • 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-06-17T21:56:21+00:00Added an answer on June 17, 2026 at 9:56 pm

    I think this statement will help:

    Create table OITB (itmsGrpCod int ,ItmsGrpNam varchar(30) )
    
    insert into OITB select 101,'RICE'
    insert into OITB select 102,'SALT'
    insert into OITB select 103,'SUGAR'
    insert into OITB select 104,'FROZEN VEGETABLE'
    
    
    
    Create table Item (ItemCode int ,ItemName varchar(20),ItmsGrpCod int )
    
    insert into Item select 1011,'A RICE',101
    insert into Item select 1012,'B RICE',101
    insert into Item select 1013,'C RICE',101
    insert into Item select 1014,'D RICE',101
    insert into Item select 1015,'E RICE',101
    insert into Item select 1016,'F RICE',101
    insert into Item select 1017,'G RICE',101
    insert into Item select 1018,'H RICE',101
    insert into Item select 1019,'I RICE',101
    insert into Item select 10111,'J RICE',101
    insert into Item select 10112,'K RICE',101
    insert into Item select 10113,'L RICE',101
    insert into Item select 10114,'M RICE',101
    insert into Item select 1020,'A SALT',102
    insert into Item select 1021,'B SALT',102
    insert into Item select 1042,'C SALT',102
    
    
    
    
    
    WITH recordsList
    AS
    (
        SELECT  Item.*,
                ROW_NUMBER() OVER (PARTITION BY ItmsGrpNam
                                    ORDER BY Item.itmsGrpCod ) rn
        FROM    OITB
      inner join 
      Item on OITB.ItmsGrpCod=Item.ItmsGrpCod
    )
    SELECT  *
    FROM    recordsList
    WHERE   rn <= 10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a query to an SQL database and I can't
I'm trying to make a query, but I can't find a way to do
I've been trying to figure out how I can make a query with MySQL
I'm trying to make a query where my LIKE can match any field in
I am trying to make an MDX query that can tell me how many
I'm trying to make a query that selects all messages from a specific user
I am trying to make a query that will take the inputted items and
I'm trying to make a prepared query that can be used to select any
I'm trying to make a query to get rush hours for everyday on a
I am trying to make a query that will insert multiple values from my

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.