Topic: Transfer of values between cells with a condition
How transfer of cell values with a condition is possible?
Was more specific:
There are 2 tables:
1) books - in it is, besides other, 2 columns - b_id (PK) and b_quantity - id books and their general amount in library accordingly;
2) subscriptions - in it there are columns sb_subscriber - the reader who took this or that book, sb_book (FK, it is connected with b_id) - what book took from a warehouse and a field sb_is_active - whether returned this book or not (Y - did not return/N - returned).
That I achieve:
Taking into account that, how many books took and did not return, to count their amount in a warehouse at present.
At present gave birth to such request:
select b_id, b_quantity, non, b_quantity - non as quantity from books left join (select sb_book, count (sb_book) as non from subscriptions where sb_is_active = ' Y' group by sb_book) as qu on books.b_id = qu.sb_book;
On an output I receive here such here:
b_id, b_quantity, non, quantity
2,3, null, null
6,12, null, null
7,7, null, null
How probably to set such condition, what if quantity for given b_id = null to appropriate to this cell value b_quantity the initial? That is that the field quantity would be filled completely.
Or it is the initially irregularly selected way of the decision of the given task?