如何根据 table 属性填充布尔列?
How do you populate boolean columns based on a table of properties?
我有一个名为 Product 的 table,它包含一个产品标识号和一个与该产品标识号关联的 ID。
pin id
11 10
12 11
13 12
我还有一个 属性 table 和 property_id
id property_id
10, 108
11, 109
12, 200
108 指的是 属性 isNew,109 指的是 属性 isPremium,200 指的是 属性 isExclusive。
有了这些,我想创建这个临时的 table:
pin id isNew isPremium isExclusive
11 10 1 0 0
12 11 0 1 0
13 12 0 0 1
我该怎么做?您可以轻松地创建临时 table,但我不确定如何映射这些值。
您可以进行条件聚合:
select pt.pin, pt.id,
max(case when py.property_id = 108 then 1 else 0 end) as is_new,
max(case when py.property_id = 109 then 1 else 0 end) as is_premium,
max(case when py.property_id = 200 then 1 else 0 end) as is_exclusive
from product pt
inner join property py on py.id = pt.id
group by pt.pin, pt.id
这假设每个产品可能有多个属性 - 与示例数据中显示的不同。如果不是这种情况,则不需要聚合:
select pt.pin, pt.id,
case when py.property_id = 108 then 1 else 0 end as is_new,
case when py.property_id = 109 then 1 else 0 end as is_premium,
case when py.property_id = 200 then 1 else 0 end as is_exclusive
from product pt
inner join property py on py.id = pt.id
您可以使用 PIVOT 函数来获得您想要的结果。
DECLARE @product table(pin int, id int)
DECLARE @property table(id int, property_id int)
insert into @product
values
(11 ,10),
(12 ,11),
(13 ,12);
insert into @property
values
(10, 108),
(11, 109),
(12, 200);
SELECT pvt.pin, pvt.product_id, [108] as is_new,[109] as is_premium, [200] as is_exclusive
FROM
(SELECT p.pin, p.id, pr.id as product_id, pr.property_id
FROM @Product as p
INNER JOIN @Property as pr
ON pr.id = p.id) as t
PIVOT
(
COUNT(t.id)
for t.Property_Id in ([108],[109],[200])
) as pvt
+-----+------------+--------+------------+--------------+
| pin | product_id | is_new | is_premium | is_exclusive |
+-----+------------+--------+------------+--------------+
| 11 | 10 | 1 | 0 | 0 |
| 12 | 11 | 0 | 1 | 0 |
| 13 | 12 | 0 | 0 | 1 |
+-----+------------+--------+------------+--------------+
我有一个名为 Product 的 table,它包含一个产品标识号和一个与该产品标识号关联的 ID。
pin id
11 10
12 11
13 12
我还有一个 属性 table 和 property_id
id property_id
10, 108
11, 109
12, 200
108 指的是 属性 isNew,109 指的是 属性 isPremium,200 指的是 属性 isExclusive。
有了这些,我想创建这个临时的 table:
pin id isNew isPremium isExclusive
11 10 1 0 0
12 11 0 1 0
13 12 0 0 1
我该怎么做?您可以轻松地创建临时 table,但我不确定如何映射这些值。
您可以进行条件聚合:
select pt.pin, pt.id,
max(case when py.property_id = 108 then 1 else 0 end) as is_new,
max(case when py.property_id = 109 then 1 else 0 end) as is_premium,
max(case when py.property_id = 200 then 1 else 0 end) as is_exclusive
from product pt
inner join property py on py.id = pt.id
group by pt.pin, pt.id
这假设每个产品可能有多个属性 - 与示例数据中显示的不同。如果不是这种情况,则不需要聚合:
select pt.pin, pt.id,
case when py.property_id = 108 then 1 else 0 end as is_new,
case when py.property_id = 109 then 1 else 0 end as is_premium,
case when py.property_id = 200 then 1 else 0 end as is_exclusive
from product pt
inner join property py on py.id = pt.id
您可以使用 PIVOT 函数来获得您想要的结果。
DECLARE @product table(pin int, id int)
DECLARE @property table(id int, property_id int)
insert into @product
values
(11 ,10),
(12 ,11),
(13 ,12);
insert into @property
values
(10, 108),
(11, 109),
(12, 200);
SELECT pvt.pin, pvt.product_id, [108] as is_new,[109] as is_premium, [200] as is_exclusive
FROM
(SELECT p.pin, p.id, pr.id as product_id, pr.property_id
FROM @Product as p
INNER JOIN @Property as pr
ON pr.id = p.id) as t
PIVOT
(
COUNT(t.id)
for t.Property_Id in ([108],[109],[200])
) as pvt
+-----+------------+--------+------------+--------------+
| pin | product_id | is_new | is_premium | is_exclusive |
+-----+------------+--------+------------+--------------+
| 11 | 10 | 1 | 0 | 0 |
| 12 | 11 | 0 | 1 | 0 |
| 13 | 12 | 0 | 0 | 1 |
+-----+------------+--------+------------+--------------+