带条件的SSIS行计数

时间:2012-10-04 17:59:57

标签: sql sql-server sql-server-2008 ssis

我有Product表有Productid和Product_No作为主键。现在,当我处理行并将其过滤为黑色,蓝色,红色,白色类别时。我知道如何过滤它,但问题是我必须更新另一个表Product_Category(Product_Id as PK)中的行计数。但我根据Product_DI进行了更新。

说明: - 使用黑色过滤产品表 产品表

Product_ID | Product_No | Price | Color
1          | 1          | 2.50  | Black
1          | 2          | 0.96  | Black
1          | 3          | 0.96  | Black
1          | 4          | 0.96  | Black
2          | 1          | 0.96  | Black
2          | 2          | 0.96  | Black
3          | 1          | 0.96  | Black

所以我必须更新Product_Category

Product_ID | Color_CNT
1          | 4
2          | 2
3          | 1

请分享您的知识。感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

除非我遗漏了您的问题,否则您可以使用第一个表格对第二个表格运行UPDATE

update t2
set Color_CNT = ColorCount
from table2 t2
left join
(
  select count(color) ColorCount, Product_ID
  from table1
  group by Product_ID
) t1
  on t2.product_id = t1.product_id;

请参阅SQL Fiddle With Demo

如果您需要INSERT数据,则可以执行以下操作:

insert into table2 (product_id, color_cnt)
select Product_ID,  count(color) ColorCount
from table1
group by Product_ID;

请参阅SQL Fiddle With Demo

这两个都可以在SSIS中完成,您将创建一个Execute SQL Task并将查询放在SQL语句字段中。这将在程序包运行时执行。

相关问题