求DB2 SQL脚本一个

lightwood 2006-04-14 10:25:14
有一个明细表,CustomerId,ProductId,Quantity,每个客户都有几种商品,每个客户的商品不同。现在需要为每个客户插入不在这张表中的所有商品的记录,记录的Quantity值为0。
也就是先distinct ProductId,然后distinct CustomerId,循环每个CustomerId,如果distinct ProductId的值不在这个CustomerId的商品中,就为这个CustomerId加入Quantity为0的商品记录。
这个用一个临时表和一个游标能够解决,但是由于不懂DB2脚本,还望高手指教。
...全文
369 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
longfeisoft 2006-05-03
  • 打赏
  • 举报
回复
insert into tableb
select CustomerId,ProductId,Quantity from tablea where (CustomerId,ProductId) not in (select CustomerId,ProductId from tableb) group by CustomerId,ProductId,Quantity having Quantity = 0

5,889

社区成员

发帖
与我相关
我的任务
社区描述
IBM DB2 是美国IBM公司开发的一套关系型数据库管理系统,它主要的运行环境为UNIX(包括IBM自家的AIX)、Linux、IBM i(旧称OS/400)、z/OS,以及Windows服务器版本
社区管理员
  • DB2
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧