通过查询检查分区中列值的所有值是否相同
Check if all the values of a column values are same in a partition by query
我想在分区查询中查找列的所有值是否都相同。
+----------------------------------------+
|b_name |category|indicator |amount|id |
+-------|--------|----------|------|-----|
|BUCKET1|CAT1 |Y | 211.8|21006|
|BUCKET2|CAT1 |N |275.88|21006|
|BUCKET1|CAT2 |Y | 265.4|21008|
|BUCKET2|CAT2 |Y | 289.8|21008|
|BUCKET1|CAT3 |N | 180.6|21011|
|BUCKET2|CAT3 |N |180.36|21011|
+----------------------------------------+
我想获取基于指标列的数据,其中类别和 ID 列组的所有值都相同。
指标值与'Y'相同的数据
+------------------------------------+
|b_name | category | amount | id |
+------------------------------------+
|BUCKET1| CAT1 | 211.8 | 21006 |
|BUCKET2| CAT1 | 275.88 | 21006 |
|BUCKET1| CAT3 | 180.6 | 21011 |
|BUCKET2| CAT3 | 180.36 | 21011 |
+------------------------------------+
指标列不同值的数据和(指标列相同值仅'N')
+------------------------------------+
|b_name | category | amount | id |
+------------------------------------+
|BUCKET2| CAT2 | 275.88 | 21008 |
+------------------------------------+
请帮助我获取格式数据的方法。我想要相应行的正确 b_name 列值。
对于第一种情况,可以使用如下解析函数:
SELECT * FROM
(SELECT T.*,
COUNT(1) OVER (PARTITION BY ID) AS TOTAL_COUNT_ID,
SUM(CASE WHEN INDICATOR = 'Y' THEN 1 ELSE 0 END) AS Y_COUNT
FROM YOUR_TABLE T) T
WHERE TOTAL_COUNT_ID > Y_COUNT;
一个选项使用 window 函数:
select *
from (
select t.*,
min(indicator) over(partition by category, id) min_indicator,
max(indicator) over(partition by category, id) max_indicator
from mytable t
) t
where min_indicator = max_indicator
这为您提供了一组行,其中只有一个不同的指标值。您可以轻松地调整它以过滤给定的指标值:
where min_indicator = max_indicator and min_indicator = 'Y'
或者您可以过滤具有两个不同值的组:
where min_indicator <> max_indicator
drop table bucket_data;
create table bucket_data(b_name varchar2(100),category varchar2(100),
indicator VARCHAR2(2),amount number(10),id number(10));
insert into bucket_data values('BUCKET1','CAT1','Y',211.8,21006);
insert into bucket_data values('BUCKET2','CAT1','N',275.88,21006);
insert into bucket_data values('BUCKET1','CAT2','Y',265.4,21008);
insert into bucket_data values('BUCKET2','CAT2','Y',289.8,21008);
insert into bucket_data values('BUCKET1','CAT3','N',180.6,21011);
insert into bucket_data values('BUCKET2','CAT3','N',180.36,21011);
commit;
+------------------------------------+
|b_name | category | amount | id |
+------------------------------------+
|BUCKET1| CAT1 | 211.8 | 21006 |
|BUCKET2| CAT1 | 275.88 | 21006 |
|BUCKET1| CAT3 | 180.6 | 21011 |
|BUCKET2| CAT3 | 180.36 | 21011 |
+------------------------------------+
with d as(select t.*,count(1) over(partition by CATEGORY,INDICATOR) cnt from bucket_data t
where INDICATOR <='Y') ,
d1 as(select t.*,count(1) over(partition by CATEGORY,INDICATOR) cnt from bucket_data t
where INDICATOR <='N')
select * from d1,d
where d1.id<>d.id
order by 1
fetch first 2 rows only;
我想在分区查询中查找列的所有值是否都相同。
+----------------------------------------+
|b_name |category|indicator |amount|id |
+-------|--------|----------|------|-----|
|BUCKET1|CAT1 |Y | 211.8|21006|
|BUCKET2|CAT1 |N |275.88|21006|
|BUCKET1|CAT2 |Y | 265.4|21008|
|BUCKET2|CAT2 |Y | 289.8|21008|
|BUCKET1|CAT3 |N | 180.6|21011|
|BUCKET2|CAT3 |N |180.36|21011|
+----------------------------------------+
我想获取基于指标列的数据,其中类别和 ID 列组的所有值都相同。
指标值与'Y'相同的数据
+------------------------------------+
|b_name | category | amount | id |
+------------------------------------+
|BUCKET1| CAT1 | 211.8 | 21006 |
|BUCKET2| CAT1 | 275.88 | 21006 |
|BUCKET1| CAT3 | 180.6 | 21011 |
|BUCKET2| CAT3 | 180.36 | 21011 |
+------------------------------------+
指标列不同值的数据和(指标列相同值仅'N')
+------------------------------------+
|b_name | category | amount | id |
+------------------------------------+
|BUCKET2| CAT2 | 275.88 | 21008 |
+------------------------------------+
请帮助我获取格式数据的方法。我想要相应行的正确 b_name 列值。
对于第一种情况,可以使用如下解析函数:
SELECT * FROM
(SELECT T.*,
COUNT(1) OVER (PARTITION BY ID) AS TOTAL_COUNT_ID,
SUM(CASE WHEN INDICATOR = 'Y' THEN 1 ELSE 0 END) AS Y_COUNT
FROM YOUR_TABLE T) T
WHERE TOTAL_COUNT_ID > Y_COUNT;
一个选项使用 window 函数:
select *
from (
select t.*,
min(indicator) over(partition by category, id) min_indicator,
max(indicator) over(partition by category, id) max_indicator
from mytable t
) t
where min_indicator = max_indicator
这为您提供了一组行,其中只有一个不同的指标值。您可以轻松地调整它以过滤给定的指标值:
where min_indicator = max_indicator and min_indicator = 'Y'
或者您可以过滤具有两个不同值的组:
where min_indicator <> max_indicator
drop table bucket_data;
create table bucket_data(b_name varchar2(100),category varchar2(100),
indicator VARCHAR2(2),amount number(10),id number(10));
insert into bucket_data values('BUCKET1','CAT1','Y',211.8,21006);
insert into bucket_data values('BUCKET2','CAT1','N',275.88,21006);
insert into bucket_data values('BUCKET1','CAT2','Y',265.4,21008);
insert into bucket_data values('BUCKET2','CAT2','Y',289.8,21008);
insert into bucket_data values('BUCKET1','CAT3','N',180.6,21011);
insert into bucket_data values('BUCKET2','CAT3','N',180.36,21011);
commit;
+------------------------------------+
|b_name | category | amount | id |
+------------------------------------+
|BUCKET1| CAT1 | 211.8 | 21006 |
|BUCKET2| CAT1 | 275.88 | 21006 |
|BUCKET1| CAT3 | 180.6 | 21011 |
|BUCKET2| CAT3 | 180.36 | 21011 |
+------------------------------------+
with d as(select t.*,count(1) over(partition by CATEGORY,INDICATOR) cnt from bucket_data t
where INDICATOR <='Y') ,
d1 as(select t.*,count(1) over(partition by CATEGORY,INDICATOR) cnt from bucket_data t
where INDICATOR <='N')
select * from d1,d
where d1.id<>d.id
order by 1
fetch first 2 rows only;