[英](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:我想要的是:
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.
我仍然期待结果 userid 4 因为其余的都不满足条件。
Explanation:-解释:-
Userid 3:dates are matching.(I want different dates for different some_other_id).用户 ID 3:日期匹配。(我想要不同的 some_other_id 的不同日期)。
Userid 5: some_other_id is same.用户 ID 5:some_other_id 相同。
Userid 6: phn_id is different. Userid 6:phn_id 不同。
Sorry, if I got you guys confused with the question.对不起,如果我让你们对这个问题感到困惑。 Let me know if any other details required.
如果需要任何其他详细信息,请告诉我。
You could use not exists
clause and write your criteria there or some analytic function to count occurences and then filter them.您可以使用
not exists
子句并在那里写下您的标准或一些分析函数来计算出现次数然后过滤它们。 Sample SQL with not exists
: not exists
示例 SQL:
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.如果您只运行内部查询,您可以看到,对于每个 user_id:
cnt_phn
电话号码为 1322, cnt_id
- 此用户的不同 some_other_id 和cnt_dt
- 计算每个元组的日期。 Outer query filters data according to your criteria.外部查询根据您的条件过滤数据。 With your updated sample data it shows only user 4, as expected.
使用您更新的示例数据,它只显示用户 4,正如预期的那样。 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.如果您发现任何问题,此查询可能需要调整,但在这里您有两种方法可以实现您的目标:
exist
和分析功能。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.