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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:26:01+00:00 2026-05-22T02:26:01+00:00

select tt.threshold_id from (select sum(amount) over (partition by tt.threshold_type order by tt.threshold_type ) amt

  • 0
select tt.threshold_id
from   (select sum(amount) over (partition by tt.threshold_type 
                                     order by tt.threshold_type ) amt
        from   cash_transactions) cash,
       thresholds tt
where  tt.threshold_amount < cash.amt  

the rdms involved is oracle the error is

" ORA-00904: "TT"."THRESHOLD_TYPE": invalid identifier"

what i want to do with this query is :

  1. threshold table contains a column threshold type which contain column name of the cash transactions table
  2. And for each record from the threshold table we need to compare the sum(amount) group by the threshold type from cash transactions table .
  3. and the amount fetched is compared with the threshold_amount of the the threshold table
  4. and i need to select the threshold_id

Thresholds Table:

Threshold_id        Threshold_type          Threshold_amount
============================================================
threshold_1         p_id                    450
threshold_2         p_id,to_acc_main_num    100 

Cash Transactions Table:

Tran_inst_id    p_id    amount    to_acc_main_num
=================================================
1               E1      100       123
2               E2      200       5765  
3               E1      200       687
4               E2      300       890
5               E1      100       462

DESIRED OUTPUT :

Lets take the first fetch :the first record from the threshold table

Threshold_id        Threshold_type          Threshold_amount
============================================================
threshold_1         p_id                    100000

1.now the threshold_type is p_id ok
2.So i need to group by pid from cash_transactions table.
3.so the desired result from this is (but i have to take sum on basis of p_id only)
not tran_inst_id in this case

Tran_inst_id  p_id    sum(amount)
======================================
1             E1        400
2             E2        500
3             E1        400
4             E2        500
5             E1        400

1.now each records amount above is compared with the amount of threshold_1 record.
2.so 450 threshold_amount for threshold_1 is compared with all the above record
3.so the required output will be

theshold_id   Tran_inst_id
==================================
thresold_1      2
threshold_1     4 
- the above result is for first record of threshold table ,now the same continues for the second record.         

EDIT:Suppose if the threshold_type is null ,then we not need to include partition by part in the query ,then how it can be obtain?

  • 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-05-22T02:26:01+00:00Added an answer on May 22, 2026 at 2:26 am

    It is only possible with dynamic sql, since the number of columns in the group by clause is variable.
    For example with a function:

        create or replace
        function sum_cash_transactions ( p_threshold_type varchar2) return number
        is
          v_result NUMBER;
        begin
          execute immediate ' select max( sum_amount) 
                              from( select sum(amount) as sum_amount
                                    from   cash_transactions
                                    group by ' || p_threshold_type || ' )'
         into v_result;
         return v_result;
         end;
    /
    

    and then

    select threshold_id
     from thresholds
     where threshold_amount < sum_cash_transactions(threshold_type);
    

    EDIT due to new requirements:

    CREATE OR REPLACE package pkg AS
      TYPE res_rec_type IS RECORD (
        threshold_id  VARCHAR2(200)
      , Tran_inst_id  NUMBER(10,0)
      , sum_amount    NUMBER(22)
      );
      TYPE res_tab_type IS TABLE of res_rec_type;
      FUNCTION f1 RETURN  res_tab_type PIPELINED;
    END;
    /
    
    CREATE OR REPLACE PACKAGE BODY pkg AS
    
      FUNCTION f1 RETURN  res_tab_type PIPELINED
      IS
        CUR    SYS_REFCURSOR;
        v_rec  res_rec_type;
      BEGIN
        FOR treshold in ( SELECT Threshold_id,  Threshold_type,   Threshold_amount FROM thresholds)
        LOOP
          OPEN CUR FOR 'SELECT ' || threshold.Threshold_id || ', tTran_inst_id,  s FROM (SELECT  tTran_inst_id, SUM(AMOUNT) OVER (PARTITION BY ' || p_Threshold_type || ') as s from cash_transactions ) WHERE s > ' || treshold.Threshold_amount ;
          LOOP
            FETCH cur INTO v_rec;
            EXIT WHEN cur%NOTFOUND;
            pipe row(v_rec);
          END LOOP;
        END LOOP;
        CLOSE cur;
        RETURN;
      END;
    END;
    /
    
    SELECT * form table(pkg.f1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY hrl.Frn) as Row, hrl.unq, hrl.LcnsId, hc.Business,hc.Name,hc.Phone,
Select user_name [User Name], first_name [First Name], last_name [Last Name] From tab_user ORDER BY
SELECT username, (SUM(rating)/count(*)) as TheAverage, count(*) as TheCount FROM ratings WHERE month ='Aug' AND
SELECT a.id AS supplier, sum( processed_weight ) AS total_qty FROM supplier_inward a INNER JOIN
select distinct constraint_type from user_constraints; C - C P R U Seems P means
select max(DELIVERY_TIMESTAMP) from DOCUMENTS; will return the time that the latest document was delivered.
SELECT `name` , COUNT(*) AS `count` FROM `t1`, `t2` WHERE `t2`.`id` = `t1`.`id` GROUP
SELECT avg(con_hits) as avg_hits FROM content WHERE con_type = 1 AND con_posttime < $twelve_hrs_ago
SELECT NR_DZIALU, COUNT (NR_DZIALU) AS LICZ_PRAC_DZIALU FROM PRACOWNICY GROUP BY NR_DZIALU HAVING NR_DZIALU =
SELECT pe.prodtree_element_id prodID, pe.prodtree_element_name_s, li.line_name, av2.value FROM prodtree_element pe LEFT JOIN prodtree_link pl ON

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.