0

I have created a temp table (@temptable) where i left a column blank because I will fill it with another select statement.

I have a table that has 5 rows called books:

 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 | ......

and my temptable has a column that includes the 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}

And what I want to do is get the count of how many books I have of a specific book from the books table where the date is not null, and put it into the CountOfBook column for my @temptable, but I can't seem to figure it out. it should look like this:

...... | 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

as there are 2 of each book where the date is not null.

4

1 回答 1

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

更新: 尝试使用这个: 链接引用

   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;
于 2013-06-07T18:15:01.797 回答