数量大于1时不显示NULL值
Don't Display NULL value When quantity is greater than 1
我对 sql 很陌生,如果这看起来很简单,我很抱歉。
我正在努力详细说明这个问题,我不确定要使用的正确功能。
我有一个包含产品 ID(A 列)的 table,如果该产品 ID 有不同的选项(小号、中号、大号或 10cm、20cm、30cm 或蓝色、黑绿色等)填充了另一列(b 列),它提供属性产品 ID。不幸的是,即使您必须选择一个属性(即您不能说您不想要尺寸或颜色),该列有时也会提供空值。
我要创建的是一个视图,如果只有 1 个选项(b 列将始终为 NULL),则显示该行,如果有要显示的选项(不包括任何 NULL 值),也显示该行在 B 列中)
这是我正在使用的数据示例,您可以在底部附近看到 1964 和 1980 具有属性 ID,但没有其他 NULL 值,而 7487 和 7880 具有 NULL 和属性 ID。
Products_ID
AttributeID
7487
7487
7487-19341
7880
7880-19347
7880
1954
1954
1954-9318
7246
7246
7246-18205
2313
4861
6474
6756
6960
6960
6960-18463
5919
5919
5919-14569
947
2320
2320
2320-3561
7742
3070
3070
3070-17697
7702
1964
1964-2469-2475
2869
2869
2869-14506
1980
1980-2412-5795
6783
6783
6783-18310
7816
1815
1815
1815-1667
这就是我想要显示的内容
Products_ID
AttributeID
7487
7487-19341
7880
7880-19347
1954
1954-9318
7246
7246-18205
2313
4861
6474
6756
6960
6960-18463
5919
5919-14569
947
2320
2320-3561
7742
3070
3070-17697
7702
1964
1964-2469-2475
2869
2869-14506
1980
1980-2412-5795
6783
6783-18310
7816
1815
1815-1667
如果我理解正确,如果 table 中的产品没有其他值,您只需要 NULL
个值。如果是:
select t.*
from t
where t.attributeid is not null or
not exists (select 1
from t tt2
where t2.productid = t.productid and
t2.attributeid is not null
);
如果每个产品最多有一个非空属性,您还可以使用聚合:
select productid, max(attributeid)
from t
group by productid;
SELECT T.*
FROM [test].[dbo].[Stock] as t
where t.[product_attribute_combo] <> '' or
not exists (select 1
from [test].[dbo].[Stock] as t2
where t2.[products_id] = t.[products_id] and
t2.[product_attribute_combo] <> ''
);
GO
我对 sql 很陌生,如果这看起来很简单,我很抱歉。
我正在努力详细说明这个问题,我不确定要使用的正确功能。
我有一个包含产品 ID(A 列)的 table,如果该产品 ID 有不同的选项(小号、中号、大号或 10cm、20cm、30cm 或蓝色、黑绿色等)填充了另一列(b 列),它提供属性产品 ID。不幸的是,即使您必须选择一个属性(即您不能说您不想要尺寸或颜色),该列有时也会提供空值。
我要创建的是一个视图,如果只有 1 个选项(b 列将始终为 NULL),则显示该行,如果有要显示的选项(不包括任何 NULL 值),也显示该行在 B 列中)
这是我正在使用的数据示例,您可以在底部附近看到 1964 和 1980 具有属性 ID,但没有其他 NULL 值,而 7487 和 7880 具有 NULL 和属性 ID。
Products_ID | AttributeID |
---|---|
7487 | |
7487 | 7487-19341 |
7880 | 7880-19347 |
7880 | |
1954 | |
1954 | 1954-9318 |
7246 | |
7246 | 7246-18205 |
2313 | |
4861 | |
6474 | |
6756 | |
6960 | |
6960 | 6960-18463 |
5919 | |
5919 | 5919-14569 |
947 | |
2320 | |
2320 | 2320-3561 |
7742 | |
3070 | |
3070 | 3070-17697 |
7702 | |
1964 | 1964-2469-2475 |
2869 | |
2869 | 2869-14506 |
1980 | 1980-2412-5795 |
6783 | |
6783 | 6783-18310 |
7816 | |
1815 | |
1815 | 1815-1667 |
这就是我想要显示的内容
Products_ID | AttributeID |
---|---|
7487 | 7487-19341 |
7880 | 7880-19347 |
1954 | 1954-9318 |
7246 | 7246-18205 |
2313 | |
4861 | |
6474 | |
6756 | |
6960 | 6960-18463 |
5919 | 5919-14569 |
947 | |
2320 | 2320-3561 |
7742 | |
3070 | 3070-17697 |
7702 | |
1964 | 1964-2469-2475 |
2869 | 2869-14506 |
1980 | 1980-2412-5795 |
6783 | 6783-18310 |
7816 | |
1815 | 1815-1667 |
如果我理解正确,如果 table 中的产品没有其他值,您只需要 NULL
个值。如果是:
select t.*
from t
where t.attributeid is not null or
not exists (select 1
from t tt2
where t2.productid = t.productid and
t2.attributeid is not null
);
如果每个产品最多有一个非空属性,您还可以使用聚合:
select productid, max(attributeid)
from t
group by productid;
SELECT T.*
FROM [test].[dbo].[Stock] as t
where t.[product_attribute_combo] <> '' or
not exists (select 1
from [test].[dbo].[Stock] as t2
where t2.[products_id] = t.[products_id] and
t2.[product_attribute_combo] <> ''
);
GO