Note that there are some explanatory texts on larger screens.

plurals
  1. POQuery to get top 10 items per item group
    primarykey
    data
    text
    <p>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.</p> <p>I need to get the data from a SAP Business One Table (MS SQL Server 2008) </p> <p>Sample results from the Item Group table, OITB</p> <p><code>SELECT * FROM OITB</code> (showing first two columns for item groups)</p> <pre><code>ItmsGrpCod ItmsGrpNam 101 RICE 102 SALT 103 SUGAR 104 FROZEN VEGETABLE </code></pre> <p>This returns 224 results.</p> <p><code>SELECT * FROM OITB</code> (showing first two columns for items)</p> <pre><code>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 </code></pre> <p>I also have this query below that gets the top 10 items but I have to specify the Item group code (field ItmsGrpCod)</p> <pre><code>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 </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload