gpt4 book ai didi

Need to find SQL results based on grouping of single column to find if there is any different value for columns(需要根据单个列的分组来查找SQL结果,以查找列是否有任何不同的值)

转载 作者:bug小助手 更新时间:2023-10-24 23:33:26 25 4
gpt4 key购买 nike



enter code hereI have one table like below in oracle.

在这里输入代码我在Oracle中有一个类似下面的表。






































department_id col1 col2 col3
490201 P4520 ABC1 8000643
490201 P4520 ABC1 7000640
490201 P4520 ABC1 8000643
490202 P4520 ZYZ2 8000643


Now if we see above table, for department_id 490201 has three entries, we have mismatch value in col3 (700640), there can be many rows with department_id, but their col1, col2, col3 can't have different value. So for each department_id all col1, col2 and col3 must be consistent.

现在,如果我们看到上面的表,因为Department_id 490201有三个条目,我们在col3(700640)中有不匹配的值,可能有很多行带有Department_id,但它们的col1、col2、col3不能有不同的值。因此,对于每个Department_id,所有的col1、col2和col3必须一致。


Now I want to fetch all those department_id using SQL, where any difference found for col1, col2 and col3. So final output should look like this because in above sample data only one department_id having mismatch records.

现在,我想使用SQL获取所有这些Department_id,其中col1、col2和col3有什么不同。因此,最终输出应该如下所示,因为在上面的示例数据中,只有一个Department_id具有不匹配的记录。


enter image description here


Edited: Not a SQL guy to write complex query, I am writing simple query to pull all records - select department_id, col1, col2, col3 from meta_test

编辑:我不是一个编写复杂查询的SQL人,我正在编写简单的查询来提取所有记录-从meta_test中选择Department_id、col1、col2、col3


And running loop in Python to do work around but data may increased in future so avoiding loop technique with SQL.

并在Python中运行循环来绕过工作,但未来数据可能会增加,因此避免了使用SQL的循环技术。


更多回答

You forgot to include your current query and explain what is not working.

您忘记了包括您当前的查询并解释什么不起作用。

1) please don't use images, use editable text so that someone can copy your data and try out a solution 2) please show the SQL you’ve managed to write on your own and then ask a specific question about an issue you are facing

1)请不要使用图片,使用可编辑的文本,这样别人就可以复制您的数据并尝试解决方案2)请展示您自己编写的SQL,然后就您面临的问题提出特定的问题

Updated RAW data into table format and added "Edited" section to highlight what currently being implemented.

将原始数据更新为表格格式,并增加了“已编辑”部分,以突出当前正在实施的内容。

优秀答案推荐

This is how I understood the question:

我是这样理解这个问题的:


Sample data:

样本数据:


SQL> with test (department_id, col1, col2, col3) as
2 (select 201, 4520, 'abc1', 643 from dual union all
3 select 201, 4520, 'abc1', 640 from dual union all
4 select 201, 4520, 'abc1', 643 from dual union all
5 --
6 select 202, 4520, 'zyz2', 643 from dual union all
7 --
8 select 203, 1234, 'abc1', 200 from dual union all
9 select 203, 1234, 'abc1', 200 from dual union all
10 select 203, 1234, 'abc1', 200 from dual
11 )

Query: number of distinct values in col1, col2 and col3 has to be 1; otherwise, return that department_id:

查询:col 1,col 2和col 3中的非重复值的数量必须为1;否则,返回department_id:


 12  select department_id
13 from test
14 group by department_id
15 having count(distinct col1) <> 1
16 or count(distinct col2) <> 1
17 or count(distinct col3) <> 1;

DEPARTMENT_ID
-------------
201

SQL>

更多回答

Or... having not (count(distinct col1) = 1 and count(distinct col2) = 1 and count(distinct col3) = 1). I would prefer this because I prefer to use the = over the <> operator, but of course, this doesn't really matter, just kind of taste.

或者..。不具有(COUNT(DISTINCT COL1)=1并且COUNT(DISTINCT COL2)=1和COUNT(DISTINCT COL3)=1)。我更喜欢这样,因为我更喜欢使用=而不是<>运算符,但当然,这并不重要,只是有点味道。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com