简体   繁体   English

(SQL) 我想要给定表的特定行输出

[英](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. some_user_id 与相同的用户 ID 不匹配。
  3. phn_id sould be 1322 phn_id 应该是 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.在满足上述两个条件后,我想检查是否有任何日期列对于同一用户 ID 彼此不匹配。 Result: In the example above I am expecting output for userId 4 as date2 and date3 are not matching.结果:在上面的示例中,我期待 userId 4 的输出,因为 date2 和 date3 不匹配。

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.

相关问题 编辑表格中的特定行(SQL和PHP) - Edit specific row from table (SQL & PHP) 我想在 sql 中输出 - i want output in sql 我想以特定顺序将表格的每一行与另一行连接起来 - i want to join every row of a table with another one in a specific order 在rails中,如何从表中选择一个特定列的数据? (只有一行,所以我只想要一个特定的结果,但不是一个数组) - In rails, how to select one specific column's data from a table? (there is only 1 row so I want only a specific result but not an array) 我想列出 SQL 中的 output 表中所示的数据 - I want to list data as shown in output table in SQL 我想确保Microsoft SQL表中的每一行都是唯一的 - I want to make sure that every row in a Microsoft SQL table is unique PHP SQL我想在表的1行中显示2组数据 - PHP SQL I want to show 2 sets of data in 1 row of a table 我希望用户仅在sql表中看到特定的行 - I want user to see only a particular row in sql table 在 Notepad++ 和 Informatica 中转换以下源文件。 查看给定的源表和我想要的 Output 表 - Convert Below Source File in Notepad++ & Informatica. See the Given Source Table & Output Table I Want 我想在 SQL Server 中以特定组大小划分表中的记录 - I want to divide records in table with specific group size in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM