我创建了一个临时表(@temptable),其中我将列留空了,因为我将用另一个select语句填充它。
我有一个包含5行的表格:
BookNum | BookDesc | BookDate | ......
---------|----------|----------|--------
00 | A sto... | 6/6/2013 | ......
00 | {null} | {null} | ......
02 | The t... | 6/6/2013 | ......
00 | {null} | 6/6/2013 | ......
02 | {null} | 6/6/2013 | ......
我的temptable有一个包含BookNum的列:
...... | total_books | title | BookCode | CountOfBook
-------|-------------|-------|----------|-------------
...... | 4 | Once | 00 | {null}
...... | 4 | Once | 00 | {null}
...... | 4 | Once | 00 | {null}
...... | 4 | Once | 00 | {null}
...... | 4 | Once | 00 | {null}
...... | 4 | Once | 00 | {null}
...... | 4 | Four | 02 | {null}
...... | 4 | Four | 02 | {null}
我想要做的是从书籍表中获取一本特定书籍的书数,其中日期不为空,并将其放入我的@temptable的CountOfBook列中,但我可以'似乎弄清楚了。它应该是这样的:
...... | total_books | title | BookCode | CountOfBook
-------|-------------|-------|----------|-------------
...... | 4 | Once | 00 | 2
...... | 4 | Once | 00 | 2
...... | 4 | Once | 00 | 2
...... | 4 | Once | 00 | 2
...... | 4 | Once | 00 | 2
...... | 4 | Once | 00 | 2
...... | 4 | Four | 02 | 2
...... | 4 | Four | 02 | 2
因为每本书中有2本日期不为空。
答案 0 :(得分:2)
试试这个,你可以在更新语句中使用子查询 - 这对你有帮助 -
update tempTable t
set CountOfBook = (select count(*) as CountOfBook_2
from Books
where BookData is not null
Group by BookCode
having BookCode=t.BookCode );
sqlfiddle示例 - > 使用oracle 11 g => fiddle
<强>更新强> 尝试使用此:link referred
update tempTable
set CountOfBook = (select count(*) as CountOfBook_2
from Books
where BookData is not null
Group by BookCode
having BookCode=t.BookCode)
FROM tempTable as t;