Nitin Nitin - 2 months ago 6
SQL Question

(SQL) I want a specific row output from the given table

Userid some_other_id phn_id date1 date2 date3 date4
3 21 1322 09-DEC-15 31-DEC-99 01-JAN-00 31/12/9999
3 22 1322 09-DEC-15 31-DEC-99 01-JAN-00 31/12/9999
4 23 1322 21-AUG-15 25-AUG-06 01-OCT-03 31/12/9999
4 24 1322 21-AUG-15 31-DEC-99 25-AUG-06 31/12/9999
5 22 1322 09-DEC-15 31-DEC-99 01-JAN-00 31/12/9999
5 22 1322 01-OCT-03 25-AUG-06 01-JAN-00 31/12/9999
6 23 1321 21-AUG-15 25-AUG-06 01-OCT-03 31/12/9999
6 24 1322 21-AUG-15 31-DEC-99 25-AUG-06 31/12/9999


What I want:


  1. I want all userid/row where

  2. some_user_id doesn't match for a same userid.

  3. phn_id sould be 1322

  4. After the above two conditions get satisfied I want to check whether any of the date columns are not matching to each other for a same userid.
    Result: In the example above I am expecting output for userId 4 as date2 and date3 are not matching.



Update: I have updated the sample data for further clarification.
I am still expecting the result userid 4 as rest all are not satisfying the conditions.

Explanation:-

Userid 3:dates are matching.(I want different dates for different some_other_id).

Userid 5: some_other_id is same.

Userid 6: phn_id is different.

Sorry, if I got you guys confused with the question. Let me know if any other details required.

Answer

You could use not exists clause and write your criteria there or some analytic function to count occurences and then filter them. Sample SQL with not exists:

select t1.*
  from t t1
  where userid <> some_other_id and phn_id = 1322
    and not exists (select 1 from t t2 
                     where t1.rowid <> t2.rowid and t1.userid = t2.userid 
                       and t1.date1 = t2.date1 and t1.date2 = t2.date2 
                       and t1.date3 = t2.date3 and t1.date4 = t2.date4 ) 

Edit: After your further clarifications the old query above does not work, my idea is:

select * 
  from (
    select t.*,
           count(1) over (partition by userid) cnt_phn,
           count(distinct some_other_id) over (partition by userid) cnt_id,
           count(1) over (partition by userid, date1, date2, date3, date4) cnt_dt
      from t where phn_id = 1322 )
  where cnt_phn > 1 and cnt_id > 1 and cnt_dt = 1
  order by userid, some_other_id

If you run only inner query you can see, for each user_id: cnt_phn number of phones with number 1322, cnt_id - number of distinct some_other_id for this user and cnt_dt - which counts dates for each tuple. Outer query filters data according to your criteria. With your updated sample data it shows only user 4, as expected. This query may need adjustments if You find any issues, but here You have two ways how to achieve your goals: exist and analytic functions. Hope this helps.

Test data:

create table t (Userid number(3), some_other_id number(3), phn_id number(6), date1 date, date2 date, date3 date, date4 date);
insert into t values (3, 21, 1322, date '2015-12-09', date '1999-12-31', date '2000-01-01', date '9999-12-31');
insert into t values (3, 22, 1322, date '2015-12-09', date '1999-12-31', date '2000-01-01', date '9999-12-31');
insert into t values (4, 23, 1322, date '2015-08-21', date '2006-08-25', date '2003-10-01', date '9999-12-31');
insert into t values (4, 24, 1322, date '2015-08-21', date '1999-12-31', date '2006-08-25', date '9999-12-31');
insert into t values (5, 22, 1322, date '2015-12-09', date '1999-12-31', date '2000-01-01', date '9999-12-31');
insert into t values (5, 22, 1322, date '2003-10-01', date '2006-08-25', date '2000-01-01', date '9999-12-31');
insert into t values (6, 23, 1321, date '2015-08-21', date '2006-08-25', date '2003-10-01', date '9999-12-31');
insert into t values (6, 24, 1322, date '2015-08-21', date '2015-12-31', date '2006-08-25', date '9999-12-31');

Output:

USERID SOME_OTHER_ID  PHN_ID DATE1       DATE2       DATE3       DATE4
------ ------------- ------- ----------- ----------- ----------- -----------
     4            23    1322 2015-08-21  2006-08-25  2003-10-01  9999-12-31
     4            23    1322 2015-08-21  1999-12-31  2006-08-25  9999-12-31
Comments