Different Chassis no against same registration no [message #345348] |
Wed, 03 September 2008 02:58  |
gozuhair
Messages: 206 Registered: January 2008
|
Senior Member |
|
|
Dear All
My table contains different chassis no’s or same chassis no’s against same registration no, how can I make that type of query through which I can view only those type of chassis no’s that are different from each other but there registration no are same
For example
Create table ro_mst
(ro_chassis_no varchar2(17),
Ro_reg_no varchar2(10));
Insert into ro_mst
(Ro_chassis_no,ro_reg_no)
Values
(500901,aak786);
Insert into ro_mst
(Ro_chassis_no,ro_reg_no)
Values
(500902,aak786);
Insert into ro_mst
(Ro_chassis_no,ro_reg_no)
Values
(500902,aak787);
Insert into ro_mst
(Ro_chassis_no,ro_reg_no)
Values
(500902,aak787);
Required output is
Ro_chassis_no ro_reg_no
500901 aak786
500902 aak786
|
|
|
Re: Different Chassis no against same registration no [message #345355 is a reply to message #345348] |
Wed, 03 September 2008 03:29   |
ThomasG
Messages: 3212 Registered: April 2005 Location: Heilbronn, Germany
|
Senior Member |
|
|
Thanks for the test case.
You could use a sub-select with analytical counts with different partitions clauses and then a where-clause on that counts:
SQL> SELECT * FROM ro_mst;
RO_CHASSIS_NO RO_REG_NO
----------------- ----------
500901 aak786
500902 aak786
500902 aak787
500902 aak787
SQL>
SQL> SELECT RO_REG_NO,
2 RO_CHASSIS_NO,
3 Count(*) over (PARTITION BY RO_REG_NO,RO_CHASSIS_NO ) cnt_dis,
4 Count(*) over (PARTITION BY RO_REG_NO ) cnt_reg
5 FROM ro_mst;
RO_REG_NO RO_CHASSIS_NO CNT_DIS CNT_REG
---------- ----------------- ---------- ----------
aak786 500901 1 2
aak786 500902 1 2
aak787 500902 2 2
aak787 500902 2 2
SQL>
SQL>
SQL> SELECT RO_CHASSIS_NO, RO_REG_NO FROM (
2 SELECT RO_REG_NO,
3 RO_CHASSIS_NO,
4 Count(*) over (PARTITION BY RO_REG_NO,RO_CHASSIS_NO ) cnt_dis,
5 Count(*) over (PARTITION BY RO_REG_NO ) cnt_reg
6 FROM ro_mst)
7 WHERE cnt_dis =1 AND cnt_reg > 1;
RO_CHASSIS_NO RO_REG_NO
----------------- ----------
500901 aak786
500902 aak786
SQL>
SQL>
You might have to tweak the partition by and where clause a little depending on the entire data.
|
|
|
|
|
Re: Different Chassis no against same registration no [message #345371 is a reply to message #345362] |
Wed, 03 September 2008 04:36   |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
For a non-analytic solution, try this:with src as (select 500901 chassis_no, 'aak786' reg_no from dual union all
select 500902, 'aak786' from dual union all
select 500902, 'aak787' from dual union all
select 500902, 'aak787' from dual)
select * from src
where reg_no in (select reg_no
from src
having count(distinct chassis_no) > 1
group by reg_no);
10/10 for providing a test case by the way. Makes things a lot easier
|
|
|
|
|
|
|