sql - How to use a total of a computed column as a value in another query? -


background :- have scenario find out value of items have quoted on projects have been named preferred supplier , value of items not named for.

the tables have @ disposal dba.lead -> dba.a_quotelne columns decide whether items specified or not : "dba"."a_quotelne"."altlineref" if altlineref = 0 not named, if = 1 have been named. first line of each group of items contains 1 or 0. rest null.

example

        "leadno"    "lead_desc"     "lineno"    "calc_value"    "altlineref"    "calc_groupingref"         1           canary wharf        1           10          0               1000         1           canary wharf        2           16          null            1000         1           canary wharf        3           12          null            1000         1           canary wharf        4           12          1               1001         1           canary wharf        5           13          null            1001         2           rosente lane        1           14          0               1002         2           rosente lane        2           14          1               1003         2           rosente lane        3           12          null            1003         2           rosente lane        1           15          1               1004         2           rosente lane        2           267         null            1004         2           rosente lane        3           298         null            1004 

i used following code split specified , non specified values apart, need total of each , somehow use computed total in larger report.

  select "dba"."lead"."leadno",         "dba"."lead"."lead_desc",         "dba"."a_quotelne"."quoteno",         "dba"."a_quotelne"."lineno",         "dba"."a_quotelne"."calc_value" * max("dba"."a_quotelne"."altlineref") on (partition "dba"."a_quotelne"."calc_groupingref") spec_value,         "dba"."a_quotelne"."calc_value" - spec_value nonspec_value   "dba"."a_quotelne",         "dba"."lead"   ( "dba"."a_quotelne"."leadno" = "dba"."lead"."leadno" )  group "dba"."a_quotelne"."altlineref",         "dba"."a_quotelne"."calc_groupingref",         "dba"."a_quotelne"."calc_value",         "dba"."a_quotelne"."quoteno",         "dba"."lead"."lead_desc",         "dba"."lead"."leadno",         "dba"."a_quotelne"."lineno"    

the results wish achieve follows

    "leadno"    "lead_desc"     nonspec value   spec value     1           canary wharf            38          25     2           rosente lane            14          606 

as aware little out of depth here, massively appreciated - , if has better suggestion on how split specified , non specified values apart please include that.

many in advance

phil

your situation have quote lines altlineref , calc_groupingref defined on them. several rows might have calc_groupingref same value, 1 has correct value of altlineref (all other have null values).

here alternative way formulate query. inner query aggregates quote lines lead , calc_groupingref. in doing so, finds maximum value , sum of values. outer query combines information lead:

select l.leadno, l.lead_desc,        sum(case when maxref = 0 ql.val end) nonspec_value,        sum(case when maxref = 1 ql.val end) spec_value dba.lead l join      (select ql.leadno, sum(ql.calc_value) val,              max(ql.altlineref) maxref       dba.a_quotelne ql       group ql.leadno, cal_groupingref      ) ql       on ql.leadno = l.leadno group l.leadno, l.lead_desc; 

along way, introduced table aliases make query easier understand.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -