January 14, 2014 at 3:32 am
Hello, I am trying to filter results based on stock numbers and intend to display minimum 3-4 suppliers for against each stock number. Below are two queries and there outputs. Seeking advice to improve query.
Query-1
Select (Select Top 4 GR_SUPPLIERCODE) as Supplier,GL_GRN_NO,GL_LOCALPOLICYNO,CT_STOCKNO_P,CT_STOCKSUMMARY,CT_CLASSCODE,CT_GROUPCODE,CT_QUANTITY,PY_AVERAGEUNITCOST,(CT_QUANTITY*PY_AVERAGEUNITCOST) AS TOTAL
From ST_COMMODITYTYPE
Inner Join ST_LOCALPOLICY ON ST_LOCALPOLICY.PY_STOCKNO=ST_COMMODITYTYPE.CT_STOCKNO_P
Inner Join PO_GRN_LINE ON PO_GRN_LINE.GL_LOCALPOLICYNO=ST_LOCALPOLICY.PY_LOCALPOLICYNO
Inner Join PO_GRN ON PO_GRN_LINE.GL_GRN_NO=PO_GRN.GR_GRN_NO_P
Order by CT_STOCKNO_P
Query-2
Select Top 4 GR_SUPPLIERCODE,GL_GRN_NO,GL_LOCALPOLICYNO,CT_STOCKNO_P,CT_STOCKSUMMARY,CT_CLASSCODE,CT_GROUPCODE,CT_QUANTITY,PY_AVERAGEUNITCOST,(CT_QUANTITY*PY_AVERAGEUNITCOST) AS TOTAL
From ST_COMMODITYTYPE
Left Join ST_LOCALPOLICY ON ST_LOCALPOLICY.PY_STOCKNO=ST_COMMODITYTYPE.CT_STOCKNO_P
Left Join PO_GRN_LINE ON PO_GRN_LINE.GL_LOCALPOLICYNO=ST_LOCALPOLICY.PY_LOCALPOLICYNO
Left Join PO_GRN ON PO_GRN_LINE.GL_GRN_NO=PO_GRN.GR_GRN_NO_P
Using Query-2 I am able to get results but it is not providing for all stock numbers and not distinct.
Appreciate your efforts in advance!
January 14, 2014 at 9:42 am
probably the easiest way is to stick the query into a subquery, featuring row number, and then filter the results.
SELECT
*
FROM (SELECT
ROW_NUMBER()
OVER(
PARTITION BY GR_SUPPLIERCODE,CT_STOCKNO_P
ORDER BY GR_SUPPLIERCODE,CT_STOCKNO_P) AS RW,
GR_SUPPLIERCODE,
GL_GRN_NO,
GL_LOCALPOLICYNO,
CT_STOCKNO_P,
CT_STOCKSUMMARY,
CT_CLASSCODE,
CT_GROUPCODE,
CT_QUANTITY,
PY_AVERAGEUNITCOST,
( CT_QUANTITY * PY_AVERAGEUNITCOST ) AS TOTAL
FROM ST_COMMODITYTYPE
LEFT JOIN ST_LOCALPOLICY
ON ST_LOCALPOLICY.PY_STOCKNO = ST_COMMODITYTYPE.CT_STOCKNO_P
LEFT JOIN PO_GRN_LINE
ON PO_GRN_LINE.GL_LOCALPOLICYNO = ST_LOCALPOLICY.PY_LOCALPOLICYNO
LEFT JOIN PO_GRN
ON PO_GRN_LINE.GL_GRN_NO = PO_GRN.GR_GRN_NO_P
)MySubQuery
WHERE RW = 4
Lowell
January 15, 2014 at 10:55 pm
Dear Lowell,
Thank you so much. This gives me good idea and after changing some Parameters, I am able to extract exact report.
Once again, Thank you!
January 16, 2014 at 5:35 am
glad i could help a little bit!
let us know how it goes or if oyu need another pair of eyes to look at the issue.
Lowell
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply