Duplicate rows based upon condition
Oracle 11.2.0.1
Windows
create table regsales (billno number,itemno number,paymode varchar2(10));
insert into regsales values (12345,10,'cash');
insert into regsales values (12345,11,'cash');
insert into regsales values (12346,11,'cash');
insert into regsales values (12347,10,'credit');
insert into regsales values (12348,14,'cash');
insert into regsales values (12348,15,'cash');
insert into regsales values (12345,12,'cash');
insert into regsales values (12349,10,'cash');
insert into regsales values (12345,10,'credit');
insert into regsales values (12350,11,'cash');
insert into regsales values (12351,12,'cash');
insert into regsales values (12352,11,'cash');
insert into regsales values (12350,11,'credit');
Required Output please :
BILLNO ITEMNO PAYMODE
12345 11 cash
12345 11 credit
12350 10 cash
12350 10 credit
i.e. in which same billnos same itemno has been sold in cash and credit.
Thank you.
Hi,
So, you need to know how many different paymodes there are in a group of rows. That sounds like a job for COUNT (DISTINCT ...)
Here's one way:
WITH got_cnt AS
SELECT billno, itemno, paymode
, COUNT (DISTINCT paymode)
OVER ( PARTITION BY billno
, itemno
) AS cnt
FROM regsales
SELECT billno, itemno, paymode
FROM got_cnt
WHERE cnt = 2
ORDER BY billno, itemno, paymode
Can there be other paymodes besides 'cash' and 'credit'? If so, the same basic idea will still work, but the details may be a little messier, depending on your requirements.
Similar Messages
-
Delete duplicate records based on condition
Hi Friends,
I am scratching my head as how to select one record from a group of duplicate records based upon column condition.
Let's say I have a table with following data :
ID START_DATE END_DATE ITEM_ID MULT RETAIL | RETAIL / MULT
1 10/17/2008 1/1/2009 83 3 7 | 2.3333
2 10/17/2008 1/1/2009 83 2 4 | 2
3 10/17/2008 1/1/2009 83 2 4 | 2
4 10/31/2008 1/1/2009 89 3 6 | 2
5 10/31/2008 1/1/2009 89 4 10 | 2.5
6 10/31/2008 1/1/2009 89 4 10 | 2.5
7 10/31/2008 1/1/2009 89 6 6 | 1
8 10/17/2008 10/23/2008 124 3 6 | 2From the above records the rule to identify duplicates is based on START_DATE,+END_DATE+,+ITEM_ID+.
Hence the duplicate sets are {1,2,3} and {4,5,6,7}.
Now I want to keep one record from each duplicate set which has lowest value for retail/mult(retail divided by mult) and delete rest.
So from the above table data, for duplicate set {1,2,3}, the min(retail/mult) is 2. But records 2 & 3 have same value i.e. 2
In that case pick either of those records and delete the records 1,2 (or 3).
All this while it was pretty straight forward for which I was using the below delete statement.
DELETE FROM table_x a
WHERE ROWID >
(SELECT MIN (ROWID)
FROM table_x b
WHERE a.ID = b.ID
AND a.start_date = b.start_date
AND a.end_date = b.end_date
AND a.item_id = b.item_id);Due to sudden requirement changes I need to change my SQL.
So, experts please throw some light on how to get away from this hurdle.
Thanks,
Raj.Well, it was my mistake that I forgot to mention one more point in my earlier post.
Sentinel,
Your UPDATE perfectly works if I am updating only NEW_ID column.
But I have to update the STATUS_ID as well for these duplicate records.
ID START_DATE END_DATE ITEM_ID MULT RETAIL NEW_ID STATUS_ID | RETAIL / MULT
1 10/17/2008 1/1/2009 83 3 7 2 1 | 2.3333
2 10/17/2008 1/1/2009 83 2 4 | 2
3 10/17/2008 1/1/2009 83 2 4 2 1 | 2
4 10/31/2008 1/1/2009 89 3 6 7 1 | 2
5 10/31/2008 1/1/2009 89 4 10 7 1 | 2.5
6 10/31/2008 1/1/2009 89 4 10 7 1 | 2.5
7 10/31/2008 1/1/2009 89 6 6 | 1
8 10/17/2008 10/23/2008 124 3 6 | 2So if I have to update the status_id then there must be a where clause in the update statement.
WHERE ROW_NUM = 1
AND t2.id != t1.id
AND t2.START_DATE = t1.START_DATE
AND t2.END_DATE = t1.END_DATE
AND t2.ITEM_ID = t1.ITEM_IDInfact the entire where_ clause in the inner select statement must be in the update where clause, which makes it totally impossible as T2 is persistent only with in the first select statement.
Any thoughts please ?
I appreciate your efforts.
Definitely this is a very good learning curve. In all my experience I was always writing straight forward Update statements but not like this one. Very interesting.
Thanks,
Raj. -
Table Control - Input Enabling/Diabling of Rows based on Condition.
Hi,
In the TC, I want to Input Enable/Disable the rows based on Conditions. The First row is input enabled always. But the other rows, (2nd onwards) need to be Input Enabled/Disabled based on some conditions. It is possible to make this working. Can you please provide me a suitable solution for this?
Appreciate Your Help.
Thanks,
KannanHi Kannan...
If we are talking about "Rows"...
then identify based on some conditions the row numbers and in PBO...loop at screen with screen name..set thier input properties and modify screen (make them input only)
If we are taking into consideration "columns"
There is an internal structure COLS where we can identify column number of screen name ...or we can take except for one particular column..
if some condition satisfied....
loop at screen where screen-name <> "Column which is input'.
Loop at screen...and make other columns display only.
modify screen
endif.
Regards
Byju -
Deleting duplicate rows based on three columns in Oracle 8i
Hi all,
The database we use is Oracle 8i.
The query below raises the too_many_rows exception when I launch an application. So I want to delete the duplicated rows :
select polarisation_1, polarisation_2
into v_pol1_tech, v_pol2_tech
from v_cfh_lien_element
where nom_lien = p_nom_lien
AND num_canal_1 = p_num_canal_1
AND freq_emise_1 = p_freq_emise_1;
Notice that with many possible values of the parameters p_nom_lien, p_num_canal_1 and p_freq_emise_1 then the exception is raised.
So how to delete generally the duplicated rows based on the three columns "nom_lien" , "num_canal_1" and "freq_emise_1" ?
Thank you very much indeed.Check the other thread with same question deleting duplicate rows based on three columns in Oracle 8i
-
Delete duplicate rows -- based on 4 columns -- how?
I asked this question on how to delete duplicates recently and received this suggestion which works well -- except --
With CTE AS
SELECT *, ROW_NUMBER() OVER (Partition by fld1 order by fld1) RowNum
FROM #tmpA
DELETE From CTE
WHERE RowNum > 1
-- the actual table I need to delete duplicate rows on is based on 4 columns. The following table contains 14,462 rows of which 14,348 are distinct -- based on the following 4 colums. Below is an image of a sample of the data contained in the
table for my question and to the right of that data is the column structures (data types). Is it possible to do something like the above example suggestion with the table in the image below? How to do that? I need to delete rows so that 14462
goes down to 14348. If I only reference one column for the delete -- this would delete like 7000+ rows. I only need to remove 114 rows.
Rich PAdd the other 3 columns to the partition.
Jason Long -
Selecting duplicate rows based on a combination of columns as key
Hi ,
I have a table with 5 columns.
Code ID S_DATE E_DATE Name
1 23 01012001 null ABC
1 09 01012001 null XYZ
2 81 04022007 null TVU
1 43 03092008 null XXX
Now, I need write a select query to fetch the duplicate rows from the above table having the combination of (Code, S_DATE,E_DATE) as the key.
So from the above example, I need to get Row1 and Row2 as output (but not Row3 as it has a different S_DATE)
Thanks in advance for your suggestions.
Thanks
Edited by: thotaramesh on Mar 9, 2009 4:54 PMOn XE;
WITH sample_data AS (
SELECT 1 code,23 ID, '01012001' s_date, null e_date, 'ABC' NAME FROM dual UNION ALL
SELECT 1, 09, '01012001', null, 'XYZ' FROM dual UNION ALL
SELECT 2, 81, '04022007', null, 'TVU' FROM dual UNION ALL
SELECT 1, 43, '03092008', null, 'XXX' FROM dual)
SELECT code, ID, s_date, e_date, NAME
FROM (
SELECT
sample_data.*,
COUNT(*) over (PARTITION BY code, s_date, e_date) dups
FROM sample_data)
WHERE dups > 1;
CODE ID S_DATE E_DATE NAME
1 23 01012001 ABC
1 9 01012001 XYZ -
Display an image based upon condition A
I am displaying layers based upon windows username. If User B, then Layer B. If Username A, then Layer A. etc. Do you see that is compatible with Acrobat Reader 9? It would appear as if the OCG methods will not work as Reader doesn't support the setIntent OCG object Maybe with another function like backgrounds or watermarks?
The only thing the layer contains is a picture. Can we get the pictures to display using another method other then OCG?
Please advise.You can place a picture as a button icon. You can also show/hide buttons with JavaScript.
George -
How not show duplicate rows based on one field
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0
Hello
I have a query that looks for certain scripts that did not run on a particular day compared to what ran the same day a week ago. We want to include the start_datetime and end_datetime but when I add it to the select statement it brings up all instances of the jobs that run multiple times during the day. Is there a way to exclude the extra rows based on the script_name field?
SELECT instances.script_name,
instances.instance_name,
REGEXP_REPLACE(master.description,
chr(49814), -- em-dash
'-') description
--instances.start_datetime
FROM xxcar.xxcar_abat_instances Instances,
xxcar.xxcar_abatch_master Master
WHERE 1 = 1
AND TRUNC(start_datetime) = TRUNC(TO_DATE(:p_StartDate, 'YYYY/MM/DD HH24:MI:SS')) - (:p_NumOfWeeks * 7)
AND Instances.SCRIPT_NAME = Master.SCRIPT_NAME (+)
MINUS
SELECT script_name,
instance_name,
NULL
--NULL
FROM xxcar.xxcar_abat_instances
WHERE 1 = 1
AND TRUNC(start_datetime) = TRUNC(TO_DATE(:p_StartDate, 'YYYY/MM/DD HH24:MI:SS'))MINUS does a set operation - removing rows from the first set that exactly match the second set.
When you add columns to the first set, you want a more restricted filtering - try a multi-column NOT IN:
To remove multiple runs, group and get min/max
SELECT instances.script_name,
instances.instance_name,
REGEXP_REPLACE(master.description,
chr(49814), -- em-dash
'-') description,
min(instances.start_datetime) start_datetime,
min(instances.end_datetime) end_datetime
FROM xxcar.xxcar_abat_instances Instances,
xxcar.xxcar_abatch_master Master
WHERE 1 = 1
AND TRUNC(start_datetime) = TRUNC(TO_DATE(:p_StartDate, 'YYYY/MM/DD HH24:MI:SS')) - (:p_NumOfWeeks * 7)
AND Instances.SCRIPT_NAME = Master.SCRIPT_NAME (+)
AND (script_name, instance_name) NOT IN
( SELECT script_name,
instance_name
FROM xxcar.xxcar_abat_instances
WHERE 1 = 1
AND TRUNC(start_datetime) = TRUNC(TO_DATE(:p_StartDate, 'YYYY/MM/DD HH24:MI:SS'))
group by instances.script_name, instances.instance_name, master.descriptionYou didn't give table definitions, and the query has schemas in it, so I didn't test it.
regards,
David -
Add different UI in each row based on conditions in ALV
Hell All,
i am working on dynamic ALV in which i have created 3 methods 1. which creates the columns 2. Which sets the data into teh created columns. 3. which does teh alv customizations.
So now my requirement is based on some condition i want to change the UI element
eg
i have
column1 column2 column3
1 link_to_action name
2 link_to_url name2
so based on the condition i would like to display the ui elements as above.
I was able to do till this end so far..
1.in create_columns method i created a column call cell_varaint
2 when i was passing data into the columns
'when column1'
IF colum1 = 1.
<field> = 'link_to_url'
else.
<f_field> = ' '.
endif.
when 'column2'
CREATE OBJECT lr_link_to_actn.
<ls_column>-r_column->set_cell_editor( lr_link_to_actn ).
CREATE OBJECT l_cv.
l_cv->set_key( value = 'LINK_TO_URL').
l_cv->set_editor( value = lr_lnk_to_url ).
<ls_column>-r_column->add_cell_variant( r_cell_variant = l_cv ).
<ls_column>-r_column->set_sel_cell_variant_fieldname( 'cell_varaint).
so now i am able to get only empty column with no data into it ... and my cell varaint column shows me the values like LINK_to_URL or space.
I am sure tht some where i am missing something...
Please tell me the correct procedure to use it...
Regards,
Sana.I hope it is Hello not Hell
if i understand your question correctly, You want the first row of column to LinkToAction UI element and second row LinktoURL.
follow the link where it is nicely explained however he is making one row as text view and other as checkbox.
http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/f0e7461d-5e6c-2b10-dda9-9e99df4d136d?quicklink=index&overridelayout=true -
Color the row based on condition
Hello all,
I am woring on an interactive adobe form being called by a web dynpro application.
The adobe form interface used is XML Schema based. The form displays some data at header level and item level.
The item details are displayed in a table. Now. my requirement is that i have a field 'STATUS' in the item table. I want to display the row with RED color if the STATUS = 'X' . How can i achieve this.. i tried using java script at Row initialze event but that didn' work.
Please let me know if you have some solutions.
Thanks & Regards,
Ravi AswaniTry this code
var fields = xfa.layout.pageContent(xfa.layout.page(this)-1, "field", 0);
var custordertext;
for (var i=0; i <= fields.length-1; i++)
if (fields.item(i).name == "STATUS")
if(fields.item(i).rawValue == "X")
this.fillColor = "50, 50, 255";
use this event in calculate event
Thanks,
Rakesh -
Planning Form: Suppress row based on condition
Dear All,
Is it possible to do this in hyperion planning. for example:
- i have member A
- in the data form, i want to show row only when member A have value equal to 1. other is suppressed.
- in another form, i want to show only when A = 2, other is suppresed.
did anybody have this experience, or is there any other way to do this?
Thanks in advance.
Regards,
FeriHi, i assumed as answered because in fact, the Planning dataform don't have that functionality, but in the new version (11.1.2.2) you might want to try the filter functionality.
It was a long time ago, but if i'm not mistaken, previously i solve this problem by separating the member for each condition.
example:
member A: have all data
(copy the data from member A to member B and C with condition)
member B: contain data for condition 1
member C: contain data for condition 2
then you can select which condition that you need to show. -
Database : SQL Server 2000 Enterprise Edition
OS : Windows Server 2003
Huh? Every one will ask me why I am posting SQL Server question in Oracle forum ? I shall reply something like this :
Actually my friend is SQL Server DBA and he is getting problem to solve the below query. He asked me to solve in Oracle 9i (Because since he is using SQL Server 2000, so I guess it is almost similar level) and/or through pure SQL; but I am really very poor in SQL (need to read docs more times); I failed to help him. He just asked me how to solve in Oracle and if it is solve in Oracle then he will try to solve in by looking similar function in SQL Server's functions etc. Even though, I have posted the question in two SQL Server forums, but probably there is less activity, so I am posting here :
create table student
rollno int,
name varchar(30),
class varchar(20)
insert into student values (1,'Robert','IT');
insert into student values (2,'John','Arts');
insert into student values (2,'Hussain','IT');
insert into student values (1,'David','Science');
insert into student values (3,'Polo','IT');
insert into student values (2,'Jonathan','Science');
insert into student values (4,'Joseph','History');
insert into student values (1,'Richard','History');
insert into student values (1,'Michel','Commerce');
insert into student values (1,'Albert','Geography');
SQL> select * from student;
ROLLNO NAME CLASS
1 Robert IT
2 John Arts
2 Hussain IT
1 David Science
3 Polo IT
2 Jonathan Science
4 Joseph History
1 Richard History
1 Michel Commerce
1 Albert Geography
10 rows selected.
SQL>
Required Output :
rollno name class
1 Robert IT
2 Hussain IT
3 Polo IT
4 Joseph History
Logic behind required ouput : Rollno should be unique from all the classes giving priority to :
1.IT
2.Science
3.Arts
4.Commerce
5.Geography
6.Anyone
means, if suppose we are going to fetch a unique roll number (x), and if it is exists in all the classes then first it should be from IT, and if it is not in IT class then it should search in Science Class, if it is not in IT and Science class, then it should search in Arts class, if it is not in IT, Science,Arts then it should search in Commerce Class; like wise. All existing roll numbers must be unique from all classes giving priority to IT, Science, Arts, Commerce, Geography and if that unique roll number is not from above 5 classes, then it may be of any one class.
Kindly let me know, if I am unclear in my question and/or needs to provide more details. Since my friend is using SQL Server 2000, so I would like to request to please help me/him by using simple SQL please.
Thanks and Regards
Girish SharmaProbably need more test cases:
SELECT ROLLNO, NAME, CLASS
FROM (SELECT a.*,
RANK ()
OVER (
PARTITION BY rollno
ORDER BY
(CASE
WHEN class = 'IT' THEN 1
WHEN class = 'Science' THEN 2
WHEN class = 'Arts' THEN 3
WHEN class = 'Commerce' THEN 4
WHEN class = 'Geography' THEN 5
ELSE 6
END))
rnk
FROM student a)
WHERE rnk = 1;
ROLLNO NAME CLASS
1 Robert IT
2 Hussain IT
3 Polo IT
4 Joseph History
Cheers,
Manik. -
Select rows based on condition of same row
Hello,
I have the following table structure and rows defind here
http://sqlfiddle.com/#!4/3f474/3
I would like to select rows if PRODUCT_NO is null then I need to select rows which are having IS_PAYABLE='Y'. If PRODUCT_NO is not null
then IS_PAYABLE will be 'N' or IS_PAYABLE will be null.
E.g.
For PRODUCT_REG HPO_FABRIC, one PRODUCT_NO null so I need select that row only, not the other two rows.
I can do
select * from SUPPLIER_DETAILS where IS_PAYABLE='Y', but what if there are many records with PRODUCT_NO having null or not null
How can I do this using sql?This simple query!!!
select *
from supplier_details
where (product_no is null
and is_payable = 'Y')
or (product_no is not null and is_payable is null);
PRODUCT_REG PRODUCT_SUPPLIER_CODE PRODUCT_NO START_DATE END_DATE IS_PAYABLE
HPO_FABRIC JP_008 01-AUG-01 31-AUG-01 Y
HPO_FABRIC JP_008 STGA-FABR1 16-AUG-01 31-AUG-01 --Edited:- Modified Query and Results for 2nd Requirement
Edited by: Purvesh K on Sep 17, 2012 4:55 PM
--Removed NVL
Edited by: Purvesh K on Sep 17, 2012 5:04 PM -
To get the sum of column of SAPScript based upon condition
Hi,
I was first asked to get two new columns in ZF140_ACC_STAT_01 script with ZRFKORD10 print program. I did so by the writing the below code. Now, based on Document-Type (RV,DR,DZ,SA), all the amount with same Document Type are needed to be summed and assigned to separate variables.
How to get that done????? Please help me out.
PRINT-PROGRAM:-
FORM sep_amt_open TABLES in_par STRUCTURE itcsy
out_par STRUCTURE itcsy.
DATA: wf_temp_amt_open TYPE string,"rf140-wrshb,
amount_open TYPE string,
amt_shkzg_open TYPE bsid-shkzg,
doc_desc TYPE t003t-ltext.
READ TABLE in_par WITH KEY name = 'RF140-BELEGNUM'. " document no
IF SY-SUBRC = 0.
SELECT SINGLE SHKZG FROM BSID INTO (amt_shkzg_open)
WHERE BELNR = IN_PAR-VALUE.
ENDIF.
READ TABLE in_par WITH KEY name = 'T003T-BLART'. " document description
IF sy-subrc = 0.
SELECT SINGLE LTEXT FROM T003T INTO (doc_desc)
WHERE SPRAS = 'E' AND BLART = IN_PAR-VALUE.
IF sy-subrc = 0.
READ TABLE out_par WITH KEY name = 'DOC_DESC'.
IF SY-SUBRC = 0.
OUT_PAR-VALUE = DOC_DESC.
MODIFY OUT_PAR INDEX SY-TABIX.
ENDIF.
ENDIF.
ENDIF.
READ TABLE in_par WITH KEY name = 'RF140-WRSHB'. " Amount
IF sy-subrc = 0.
wf_temp_amt_open = in_par-value.
ENDIF.
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = wf_temp_amt_open
IMPORTING
OUTPUT = amount_open
IF amt_shkzg_open = 'S'.
READ TABLE out_par WITH KEY name = 'DEBIT_OPEN'.
IF SY-SUBRC = 0.
out_par-value = amount_open.
MODIFY out_par INDEX sy-tabix.
CLEAR out_par.
ENDIF.
ELSEIF amt_shkzg_open = 'H'.
READ TABLE out_par WITH KEY name = 'CREDIT_OPEN'.
IF SY-SUBRC = 0.
out_par-value = amount_open.
MODIFY out_par INDEX sy-tabix.
CLEAR out_par.
ENDIF.
ENDIF.
ENDFORM.
SAPSCRIPT:-
/:DEFINE &DEBIT_OPEN& = ' '
/:DEFINE &CREDIT_CLEAR& = ' '
/:DEFINE &DOC_DESC& = ' '
/:PERFORM SEP_AMT_OPEN IN PROGRAM ZF27_DRIVER_PROGRAM
/:USING &RF140-BELEGNUM&
/:USING &T003T-BLART&
/:USING &RF140-WRSHB&
/:CHANGING &DEBIT_OPEN&
/:CHANGING &CREDIT_OPEN&
/:CHANGING &DOC_DESC&
/:ENDPERFORM
T1 &RF140-BELEGNUM&,,&BSID-BLDAT&,,&T003T-BLART&,,&BSID-WAERS&,,&DOC_DESC&
= ,, &DEBIT_OPEN&,,&CREDIT_OPEN&,,
T1 ,,&BSID-SGTXT&
Thanks & Regards,
Rakesh NairIf you are mentioning about table/advanced table region you can enable totalling in those regions. Please check the Table / Advanced Table section as appropriate in Chapter 4 of the dev guide.
If it is not a table / advancedTable then you will have to programmatically total the column value and display it in the appropriate cell. -
How to disable and enable Check box based upon condition
Hi,
I wants to disable/hide the check box field before the required field is filled. Kindly help meHi Mohammed,
Try to use customize InfoPath form, add rules. Go to list tab, click customize Form, then this opens the list form in InfoPath
format.
1.Select the check box field control, click add rules, select if is blank, show validation error action.
2.Then go to conditions, click column name is blank, change it to the required column you want.
3.Go to rule type, click validation, change it to formatting, select hide this control.
4.Publish the InfoPath form.
Best Regards.
Kelly Chen
TechNet Community Support
Maybe you are looking for
-
Acrobat 8.1.7 WillClose in a Browser
Running 8.1.7 Professional in OSX 10.6 using Safari 4.0.3 (32-bit) I noticed that my WillClose handler no longer runs when I close my PDF document inside of a browser window. Is this a matter of some settings that I need to enable, or has 8.1.7 disa
-
Hello, I've got a simple question, at least I think it's simple... How can you change the image of a jar-file or when you make a shortcut of the jar-file to your desktop? So instead of the usual image (a sheet with the java-logo), a GIF-or JPG image
-
Implementing BC ecommerce in a Muse site
Hello everybody, I have a website built in muse, published and hosted with BC, and I want to implement the ecommerce module. I already updated my account, so I have the ecommerce in my control panel. The thing is that if I want to be able to manage t
-
Connect a camera to media player
I have a NI 1764 camera. Is there any way to connect to media player from web builder? Like an webcam. Thx Solved! Go to Solution.
-
Full Export/Import Errors with Queue tables and ApEx
I'm trying to take a full export of an existing database in order to build an identical copy in another database instance, but the import is failing each time and causing problems with queue tables and Apex tables. I have used both the export utility