Summing Selected Rows in Column Depending on Value in Another Column
I'd like to sum only the values in selected rows in a given column depending on the value of another column in the same row. For example, suppose I have a table (please disregard the underscores, needed for correct alignment):
___A____B____C___D
1__5___10___15___0
2_20___25___30___1
3_35___40___45___1
4_50___55___60___0
5__sum(D=1)
In cell B5, I'd like to compute the sum of only rows in column B for which the value of the corresponding column D is 1. In this case B5 would be 65.
How can I do this using functions? Is it possible to do it for a variable range of rows without specifying each row individually?
Thanks,
Dave
You should place your formula to other collumn then calculated ones or in another table. You will be able to calculate whole collumns with: =SUMIF(D;“=1”;B)
Formula for your example is: =SUMIF(D1:D4;“=1”;B1:B4)
VB
Similar Messages
-
Color a column based on value in another column in tableview
I am using a tableview iterator to display data in a bsp page. I want to color a cell in particular column (column 4), when a value in another column (column 2) on that same row is greater than 20. I can color the cell in column 2 but am not able to color the cell in column 4. How can I accomplish this?
This is what I have. Looks like val1 is losing it's value.
CASE p_column_index.
WHEN 2.
ASSIGN p_row_data_ref->* TO <row>.
ASSIGN COMPONENT 'ZTGT' OF STRUCTURE <row> TO <col>.
VAL = <col>.
IF VAL GT '20'.
val1 = p_row_index.
p_style = 'celldesign:CRITICALVALUE_DARK'.
ENDIF.
WHEN 4.
if p_row_index = val1.
p_style = 'celldesign:CRITICALVALUE_DARK'.
endif.
WHEN OTHERS.
do nothing
ENDCASE.The reason val1 is "loosing its' value" is presumably because you have defined it in the method as a local variable. So each time you call the RENDER_CELL_START method it is is newly initialised. So if you make this an instance attribute it will retain its' contents across method calls.
Cheers
Graham Robbo -
Insert value into a column based on value of another column
Hi,
I am trying to insert a value into a record based on a column in
the record, using a trigger. The complication arises because
the new value is selected from the same table. For example:
SELECT COL1, COL2, COL3, COL4 from TABLE1
I want to set COL2 and COL3 based on the value of COL4. And to
get the value of COL2 and COL3, I will go back to TABLE1 and set
the condition to TABLE1.COL1 = :NEW.COL4
I cannot seem to execute the trigger as I get the message "ORA-
04091: table SYSTEM.TABLE1 is mutating, trigger/function may not
see it" everytime.
Is this the correct way to achieve what I wanted? Or is there
another way?
Appreciate your feedback. Thank you in advance.Hi,
I am trying to insert a value into a record based on a column in
the record, using a trigger. The complication arises because
the new value is selected from the same table. For example:
SELECT COL1, COL2, COL3, COL4 from TABLE1
I want to set COL2 and COL3 based on the value of COL4. And to
get the value of COL2 and COL3, I will go back to TABLE1 and set
the condition to TABLE1.COL1 = :NEW.COL4
I cannot seem to execute the trigger as I get the message "ORA-
04091: table SYSTEM.TABLE1 is mutating, trigger/function may not
see it" everytime.
Is this the correct way to achieve what I wanted? Or is there
another way?
Appreciate your feedback. Thank you in advance. I'm not sure what you mean when you insert a value into a
record, but if you are setting a value in a column of the same
record using a trigger, then it's easy.
:new.COL2 := ....:new.COL4...
:new.COL3 := ....:new.COL4...
The trigger must be 'INSERT or UPDATE' and 'FOR EACH RECORD'.
If you are setting a different record in the same table, the
solution is much more difficult. -
Making whole row coloured red based on value in one column in BI Answers.
Hi
Would anyone know how to make a whole row red (eg) based on the value from one column within the row in
a BI Answers report in either a pivot table or a table view. I know it should be a case of setting up the
conditional formatting in the Conditional Tab on the Properties of the column, but all the columns apart from the
one with the value being determined ignore the value in another column.
Thanks,
- Pete
Edited by: user1636556 on 09-Dec-2011 07:58Hi
Would anyone know how to make a whole row red (eg) based on the value from one column within the row in
a BI Answers report in either a pivot table or a table view. I know it should be a case of setting up the
conditional formatting in the Conditional Tab on the Properties of the column, but all the columns apart from the
one with the value being determined ignore the value in another column.
Thanks,
- Pete
Edited by: user1636556 on 09-Dec-2011 07:58 -
Update one column while getting the value in another column
Is it possible to in one single SQL statement update two columns in a table while at the same time obtaining the value of another column, on the same row, in the same table, and independently (that is, the update of the columns has nothing to do with the data that I want from another column)*?* Of course, I can do this in two operations, one being a "select" and the other one being an "update", but since I am in the same table and even on the same row, is it really necessary to make TWO database calls? That's why I want to combine the the two SQL-statements, for reasons of presumed effiency.
jsun wrote:
Is it possible to in one single SQL statement update two columns in a table while at the same time obtaining the value of another column, on the same row, in the same table, and independently (that is, the update of the columns has nothing to do with the data that I want from another column)*?* Of course, I can do this in two operations, one being a "select" and the other one being an "update", but since I am in the same table and even on the same row, is it really necessary to make TWO database calls? That's why I want to combine the the two SQL-statements, for reasons of presumed effiency.Two statements != two database calls.
At least not in terms of SQL.
JDBC requires a 'statement' but in SQL (depending on the data source) that can include multiple statements. An obvious example of this is a stored proc but there are other ways as well. -
Need to get sequence value in another column in oracle
Hi ALL,
I have sql query as below
select header_id,order_number from oe_order_headers_all.
and data it is displaying as
heder_id order_number
111 500001
121 500400
I need to display another field with some sequence value like as below
id heder_id order_number
1 111 500001
2 121 500400
so how to get sequence value in another column please help me on this.
ThnaksYou can just use ROWNUM Pseudocolumn
select rownum id, header_id,order_number from oe_order_headers_all -
How to copy one column BLOB value into another column of another database.
How to copy one column BLOB value into another column of another database.
BLOB value contains word document.
I thought of copy the BLOB value into a text file and then update the new column value by the same text in textfile. Will this work?
Is there any other better way to do this?You're welcome
BLOB fields contains binary data. I don't think you can do this
Also if I view the BLOB as text. Can I copy it and insert into the new database.
I think your options are as I said. Datapump or CTAS
Best Regards -
Is it possible to create a Column with Conditional Mandatory with another Column?
Is it possible to create a Column with Conditional Mandatory with another Column?
For example
In a Table we have column A, B, C.
A is Primary Column.
B is Optional
C is Conditional Mandatory.
A B
C
12345 ABC
OK
12346 NULL
NULL
12347 ABC
OK
Only if the B Column has the value then only C column should be mandatoryI guess you can't create a condtional mandatory column directly. However, you can use check constraint to on the column
create table YourTable
A int primary key,
B char(3),
C int,
constraint ch_con check(
B
is not null
or C is null -
How to merge values in a row, based on distinct values in another column
I have a table like
updatedby updateddate text
John 01-May-2009 Approval record 1 added
John 01-May-2009 Approval record 2 added
David 02-May-2009 Approval record 1 removed
I need the values of text column to be concatenated and displayed for unique records of (updatedby updateddate). The output is something like
updatedby updateddate text
John 01-May-2009 Approval record 1 added, Approval record 2 added
David 02-May-2009 Approval record 1 removed
I had planned to do it using PLSQL. Is there a way to get this done by SQl?.. Kindly suggestPrior to 10g:
with t as (
select 'John' updatedby,to_date('01-May-2009','dd-mon-yyyy') updateddate,'Approval record 1 added' text from dual union all
select 'John',to_date('01-May-2009','dd-mon-yyyy'),'Approval record 2 added' from dual union all
select 'David',to_date('02-May-2009','dd-mon-yyyy'),'Approval record 1 removed' from dual
select updatedby,
updateddate,
ltrim(sys_connect_by_path(text,','),',') text
from (
select t.*,
row_number() over(partition by updatedby,updateddate order by text) rn,
count(*) over(partition by updatedby,updateddate) cnt
from t
where rn = cnt
start with rn = 1
connect by updatedby = prior updatedby
and updateddate = prior updateddate
and rn = prior rn + 1
order by updateddate,
updatedby
UPDAT UPDATEDDA TEXT
John 01-MAY-09 Approval record 1 added,Approval record 2 added
David 02-MAY-09 Approval record 1 removed
SQL> 10g and up:
with t as (
select 'John' updatedby,to_date('01-May-2009','dd-mon-yyyy') updateddate,'Approval record 1 added' text from dual union all
select 'John',to_date('01-May-2009','dd-mon-yyyy'),'Approval record 2 added' from dual union all
select 'David',to_date('02-May-2009','dd-mon-yyyy'),'Approval record 1 removed' from dual
select updatedby,
updateddate,
ltrim(sys_connect_by_path(text,','),',') text
from (
select t.*,
row_number() over(partition by updatedby,updateddate order by text) rn
from t
where connect_by_isleaf = 1
start with rn = 1
connect by updatedby = prior updatedby
and updateddate = prior updateddate
and rn = prior rn + 1
order by updatedby,
updateddate
with t as (
select 'John' updatedby,to_date('01-May-2009','dd-mon-yyyy') updateddate,'Approval record 1 added' text from dual union all
select 'John',to_date('01-May-2009','dd-mon-yyyy'),'Approval record 2 added' from dual union all
select 'David',to_date('02-May-2009','dd-mon-yyyy'),'Approval record 1 removed' from dual
select updatedby,
updateddate,
ltrim(sys_connect_by_path(text,','),',') text
from (
select t.*,
row_number() over(partition by updatedby,updateddate order by text) rn
from t
where connect_by_isleaf = 1
start with rn = 1
connect by updatedby = prior updatedby
and updateddate = prior updateddate
and rn = prior rn + 1
order by updateddate,
updatedby
UPDAT UPDATEDDA TEXT
John 01-MAY-09 Approval record 1 added,Approval record 2 added
David 02-MAY-09 Approval record 1 removed
SQL> SY. -
Get Min value and another column value from the same row
hi all - for each customer and product, I need to grab the row that has the min value for field Value1. I could've just done group by on customer and product and min on Value1, but I also need to grab Vlaue2 from the same row where the minimum value for
Value1 lies.
DECLARE @Temp TABLE (CustomerID INT, ProductID VARCHAR(10), Trans_Date Date, Value1 INT, Value2 INT)
INSERT INTO @Temp VALUES (123, 'ABC', '1/1/2013', 10, 100)
INSERT INTO @Temp VALUES (456, 'ASD', '1/1/2013', 40, 500)
INSERT INTO @Temp VALUES (456, 'ASD', '2/1/2013', 90, 700)
INSERT INTO @Temp VALUES (123, 'ABC', '2/1/2013', 20, 700)
SELECT * FROM @Temp
The output should be
123, ABC, 10, 100
456, ASD, 40, 500
I know that I can just join the table to itself and get the desired output but I am dealing with a table that has millions of rows and I was hoping there is more efficient way to do this. any help would be highly appreciated...Here is a correction on your DDL to make it into nearly valid table.
CREATE TABLE Sales
(customer_id INTEGER NOT NULL,
product_id CHAR(10) NOT NULL,
transaction_date DATE NOT NULL,
PRIMARY KEY (customer_id, product_id, transaction_date),
value1 INTEGER NOT NULL,
value2 INTEGER NOT NULL);
Here is the current syntax for insertion:
INSERT INTO Sales
VALUES (123, 'abc', '2013-01-01', 10, 100),
(456, 'asd', '2013-01-01', 40, 500),
(456, 'asd', '2013-02-01', 90, 700),
(123, 'abc', '2013-02-01', 20, 700);
WITH
X
AS
(SELECT customer_id, product_id, transaction_date, value1, value2,
MIN(value1) OVER () AS value1_min
FROM Sales)
SELECT X.*
FROM X
WHERE X.value1_min = X.value1;
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Conditional inclusion of column cells based on value in another column?
I was wondering if something like the following is possible -- I've looked through the forums and Numbers user guide but can't figure it out:
Suppose I have a column A of sales figures, and columns B and C which basically have different attributes (say, color (yellow or green), size (small or large), and material (cotton or polyester)).
I want to run basic statistics on various combinations: e.g. all yellow items regardless of size, all green cotton items regardless of size, all large yellow items regardless of material, etc.
I want to be able to specify something like this:
'Compute the average of every cell in column A where column B=="green" and column C=="large"'
Is there a good way to go about doing this? Would I want to attack the problem using categories, sorting, and multiple tables? Or is there a way to conditionally include cell values from a range based on the value of that row's cell in another column?
Thanks for any help.All I can think of to solve the quartile and median functions is to have another table of the same size as your original table that gets populated with just the items that match. Then you can do the quartile and median (and average) on that table. It's a one-at-a-time affair; you can only do one selection of size, color, and/or material at a time but you can get the median, mean, and quartiles of it. Of course, you could have multiple copies of this second table if you want to do more than one comparison at a time.
In the attached file, unhide the hidden rows in Table 2 to see the formulas that do all the work.
[files.me.com/pwb3/6hobt0.numbers.zip]
Message was edited by: Badunit
Message was edited by: Badunit -
PARTITION BY ? Return an ID based on the minimum value of another column
Hi guys
I want to return the ID column of a row, where 2 other columns = a set value, and a 3rd column has the minimum value matching the other criteria.
so in effect :
with t as (
select 1 dst_id, 'abc' dst_title, 1 edition_from, 1 media_item from dual
union all
select 2 dst_id, 'abc' dst_title, 1 edition_from, 2 media_item from dual
union all
select 3 dst_id, 'abc' dst_title, 2 edition_from, 1 media_item from dual
union all
select 4 dst_id, 'abc' dst_title, 2 edition_from, 2 media_item from dual
union all
select 5 dst_id, 'def' dst_title, 1 edition_from, 1 media_item from dual
union all
select 6 dst_id, 'def' dst_title, 1 edition_from, 2 media_item from dual
select dst_id
from t
where dst_title = 'abc'
and edition_from = 1
and media_item = minimum value of media item using above where clauseWould return dst_id = 1 because it has the minimum media item value of 1 where dst_title = abc and edition_from = 1
Can this be done in 1 query ? I've been trying it using partition by and over dst_title, edition_from but that doesn't work.
I'm currently doing it using a sub query or 2 queries, but thought there must be a way to do it in one ?
Thanks all
ScottLike this?
SQL> with t as (
2 select 1 dst_id, 'abc' dst_title, 1 edition_from, 1 media_item from dual
3 union all
4 select 2 dst_id, 'abc' dst_title, 1 edition_from, 2 media_item from dual
5 union all
6 select 3 dst_id, 'abc' dst_title, 2 edition_from, 1 media_item from dual
7 union all
8 select 4 dst_id, 'abc' dst_title, 2 edition_from, 2 media_item from dual
9 union all
10 select 5 dst_id, 'def' dst_title, 1 edition_from, 1 media_item from dual
11 union all
12 select 6 dst_id, 'def' dst_title, 1 edition_from, 2 media_item from dual
13 )
14 select dst_id
15 from (select t.*
16 , row_number() over (partition by dst_title, edition_from order by media_item) rn
17 from t
18 where dst_title = 'abc'
19 and edition_from = 1)
20 where rn = 1;
DST_ID
1
SQL> -
Pop-up menu in one column determines the value in another. How?
I'm setting up a company report, and one column has a pop-up menu. I want the cell in another column to automatically enter a certain value depending on which option is selected in that pop-up menu. How is that done?
m,
You can use any of the functions that look for a match in one column and pull data from another. As a class, they're called Lookup functions. I wouldn't plan on having your design survive an export to Excel, so you should consider who will be using your work and what platform they will have available.
Jerry -
Search one column for value in another column
Hello,
I am trying to take the whole value from one column, and see if that string appears anywhere in another colmn. So if one row in one column has the value "cat", I want to see if that string appears anywhere in any row in another column: so a row with the value "The cat sat on the mat" would return true.
I have tried:
select term from pic_lib_keywords, csv
where contains (csv.nhm_field_asset_description,pic_lib_keywords.term) >0;
But I get the error:
ORA-20000: Oracle Text error:
DRG-50901: text query parser syntax error on line 1, column 32
20000. 00000 - "%s"
*Cause: The stored procedure 'raise_application_error'
was called which causes this error to be generated.
*Action: Correct the problem as described in the error message or contact
the application administrator or DBA for more information.
Can you use 2 columns in this way, or does the search term have to be a literal? Any suggestions would be appreciated.
Thank you.It works for me, as shown below.
SCOTT@orcl_11gR2> select * from v$version
2 /
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 64-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
5 rows selected.
SCOTT@orcl_11gR2> create table csv
2 (nhm_field_asset_description clob)
3 /
Table created.
SCOTT@orcl_11gR2> insert all
2 into csv values ('The cat sat on the mat.')
3 into csv values ('The dog sat on the mat.')
4 select * from dual
5 /
2 rows created.
SCOTT@orcl_11gR2> create table pic_lib_keywords
2 (term varchar2(30))
3 /
Table created.
SCOTT@orcl_11gR2> insert all
2 into pic_lib_keywords values ('cat')
3 into pic_lib_keywords values ('mouse')
4 select * from dual
5 /
2 rows created.
SCOTT@orcl_11gR2> create index test_idx
2 on csv (nhm_field_asset_description)
3 indextype is ctxsys.context
4 /
Index created.
SCOTT@orcl_11gR2> select term
2 from pic_lib_keywords, csv
3 where contains
4 (csv.nhm_field_asset_description,
5 pic_lib_keywords.term) >0
6 /
TERM
cat
1 row selected.
SCOTT@orcl_11gR2> -
How to populate corresponding values in another columns
product (table having below mentioned with data also)
product_name product_code product_id lob_id product_desc
DUMMY DM 208182324072008 31
Fleet FL 196870715012008 31 Fleet
Commercial Vehicle CV 195147926122007 31 Commercial Vehicle
Private Car PC 194398713122007 31 Private Car
Two Wheeler TW 194731914122007 31 Two Wheeler
cause_of claim ( table below mention fields with data)
cause_of_claim_id product_id cause_of_claim_code cause_of_claim_desc
198035026022008 194398713122007 TP Third Party
198028726022008 194731914122007 TP Third Party
198065826022008 196870715012008 TP Third Party
198065726022008 196870715012008 OD Own Damage
198045226022008 195147926122007 OD Own Damage
198045326022008 195147926122007 TP Third Party
i have mentioned cause_of_claim data based on below query cause_of_claim_code having different type of values. currently we are focusing on "OD" and "TP" only. thats why i have reduced unnessary columns.
select cause_of_claim_id,product_id,cause_of_claim_code,cause_of_claim_desc from cause_of_claim
where cause_of_claim_code='OD' or cause_of_claim_code='TP'
product and cause_of_claim we can join based on product_id.
in product table product_desc having
Two Wheeler
Private Car
Commercial Vehicle
Fleet
like that values...........
requirement is
where ever cause_of_claim_code ='OD' and
product_desc in (Two Wheeler,
Private Car,
Commercial Vehicle,
Fleet) need to populate one column dept_code and value should be value '31'
where ever cause_of_claim_code ='TP' and
product_desc in (Two Wheeler,
Private Car,
Fleet ) need to populate same column dept_code and value should be value '32'
where ever cause_of_claim_code ='TP' and
product_desc ='Commercial Vehicle'
need to populate same column dept_code and value should be value '39'
expected result
product_desc dept_code
Two Wheeler 31
Private Car 32
Two Wheeler 32
Commercial Vehicle 31
Fleet 32
Fleet 31
Private Car 32
Commercial Vehicle 39
Fleet 32
Private Car 31This is school work. Please do the best you can then post it along with information about your version number (3 decimal places) and the issue you are experiencing with your code.
Please everyone ... I know you want to demonstrate your skill ... but students need to work these things out for themselves or they do not learn.
Maybe you are looking for
-
Two step import, downsizing image file sizes and/or keeping copies?
hi all, leonie was kind enough on another thread to suggest a two step import from the iphone in order to make sure that there is no accidental deletion or data loss. my understanding (i'm repeating it here myself in case i am misunderstanding) is th
-
Helix Fans in keyboard portion ALWAYS running at full speed
Hi, I was wondering if anyone from Lenovo support (or a fellow user) could recommend a Lenovo sponsored solution for the problem regarding the fan in the keyboard tray always running at full speed on the Helix? I know there are a bunch of 3rd party f
-
Have you heard about the latest addition to our SAP Microsoft Interoperability Suite?
As you have seen from the coverage we did at the Microsoft SharePoint Conference earlier this month we introduced a new interoperability solution with the name Power BI Connectivity to SAP BusinessObjects BI. This is a great solution that makes it po
-
Hi, here is a link to FLA files www.fulek.com/bug/BUG_report.rar (This is another X-file's example, FLA's are actually empty but they have 2MB each when extracted). Short description of problem: StartFile.swf has one big button which is instance of B
-
How can I increase the C partition space?
Hello! I have an internal hard drive used on my Windows 7computer and also has divided it into four partitions, C, D, E and F. Under Disk Management, I was able to shrink the E partition well. But when I went to extend the C partition space there, it