Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Getting partial records based on sum.

DBQuestJan 10 2018 — edited Jan 10 2018

I have a table SALES_STATS in below format (SALES_ORDER is primary key).

pastedImage_2.png

I got a requirement that, I will get a parameter p_threshold_value.

If the value of p_threshold_value is 8, SQL query should get only records where sum of item count is less than or equl to 8 (We have to take records order by sales_order).

In above scenario, it should fetch only records of S1 & S2.

If p_threshold_value is 14, SQL query should fetch only records S1, S2 & S3.

Please help me in writing query for this.

Table creation and data insertion scripts below.

CREATE TABLE sales_stats(sales_order VARCHAR2(10),customer_number VARCHAR2(10),supplier_number VARCHAR2(10),item_count NUMBER);

INSERT INTO sales_stats(sales_order,customer_number,supplier_number,item_count)

VALUES('S1','C1','SUP1',3);

INSERT INTO sales_stats(sales_order,customer_number,supplier_number,item_count)

VALUES('S2','C2','SUP2',5);

INSERT INTO sales_stats(sales_order,customer_number,supplier_number,item_count)

VALUES('S3','C3','SUP3',2);

INSERT INTO sales_stats(sales_order,customer_number,supplier_number,item_count)

VALUES('S4','C4','SUP8',7);

INSERT INTO sales_stats(sales_order,customer_number,supplier_number,item_count)

VALUES('S5','C5','SUP5',1);

This post has been answered by mathguy on Jan 10 2018
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 7 2018
Added on Jan 10 2018
11 comments
473 views