oracle - UPDATE SQL: how to update several times the same line - oracle

I need to Update quantity of several books, before delete them.
the deleting happen with this code (oracle has a hidden column called ROWNUM)
DELETE FROM project.cart WHERE isbn = ? and ROWNUM=1;
to delete ISBN one at time (there are equal isbn in the table). but the update only works for one isbn. it should update several times the same line for all isbn found.
String sql3= "UPDATE PROject.book SET quantity=quantity +1 WHERE project.book.isbn in "
+ "(SELECT project.cart.isbn FROM project.cart) ";
// this code work perfectly, but for one time.
I hope you can help me. thanks

You can try this:
String sql3= "UPDATE PROject.book SET quantity=quantity + "
+ "(select count(*) FROM project.cart where project.cart.isbn = project.book.isbn) "
+ " WHERE project.book.isbn in (SELECT project.cart.isbn FROM project.cart)";

Do you mean that if a book is in the cart three times it should have 3 added its quantity column?
If so, you need to add the count. When a row is included by the WHERE logic, it's included only once, even if it matched more than one of the criteria. Try a correlated subquery instead:
UPDATE book
SET quantity = quantity + (SELECT COUNT(*) FROM cart WHERE book.isbn = cart.isbn)
This query will run very slowly if there are a lot of book rows, so you may want to add your original WHERE clause to the end to limit the number of book rows:
UPDATE book
SET quantity = quantity + (SELECT COUNT(*) FROM cart WHERE book.isbn = cart.isbn)
WHERE isbn IN (SELECT isbn FROM cart)

Related

Update with count and group by expressions

First, I know there is a common issue in Stack Overflow, but the following solutions are not working well here. So I still need some help.
Oracle - Update COUNT of rows with specific value
Oracle - Update rows with a min value in the group of a column from another table
Oracle update statement with group function
Oracle - Update COUNT of rows with specific value
The problem is: I have a +700k lines table:
REVIEWS (PRODUCT_ID, REVIEW, REVIEW_DATE, RELEASE_DATE, ..., REVIEW_COUNT)
I'm trying to update REVIEW_COUNT by counting the lines with the same PRODUCT_ID (I want just reviews before product release). So the code below works very well for my purpose:
SELECT COUNT(PRODUCT_ID) FROM REVIEWS
WHERE REVIEW_DATE < RELEASE_DATE
GROUP BY PRODUCT_ID
But I'm having a hard time to do the update. First I tried this:
UPDATE REVIEWS R
SET R.REVIEWS_COUNT =
(SELECT COUNT(RR.PRODUCT_ID) FROM REVIEWS RR
WHERE RR.DATA < RR.REL_DATE
GROUP BY RR.PRODUCT_ID)
The error is "more than one row", which is not surprising, but since I'm using the group by statement, it shouldn't occur. So I tried a self-join:
UPDATE REVIEWS R
SET R.REVIEWS_COUNT =
(SELECT COUNT(RR.PRODUCT_ID) FROM REVIEWS RR
WHERE RR.PRODUCT_ID = R.PRODUCT_ID AND RR.DATA < RR.REL_DATE)
But the query is taking forever and I don't think that should take so long, the simple select is pretty normal-fast.
I've also tested some more fancy and more simple stuff, but the outcome remains the same: long time waiting and it seems just wrong.
Please, what I'm missing in such easy update?
Maybe instead of updating you could define view:
select product_id, review_date, release_date,
count(case when review_date < release_date then 1 end)
over (partition by product_id) review_count
from reviews;
You could also try merge instead update:
merge into reviews a
using (select product_id, count(product_id) cnt from reviews
where review_date < release_date
group by product_id ) b
on (a.product_id = b.product_id)
when matched then update set reviews_count = b.cnt
dbfiddle
I think your second update is correct:
UPDATE REVIEWS R
SET R.REVIEWS_COUNT =
(SELECT COUNT(RR.PRODUCT_ID) FROM REVIEWS RR
WHERE RR.PRODUCT_ID = R.PRODUCT_ID AND RR.DATA < RR.REL_DATE)
;
This will update every record in the reviews table. Is that what you wanted?
An index on product_id will make the inner query run faster, but it will still update all 700K or so records.

DELETE SQL: how delete only one value at a time

i have this sql string
"DELETE FROM project.cart WHERE isbn = ?";
If the table there are many isbn same, I can delete a single isbn every time I click on jbutton?
PS: isbn = ? -> takes its value from a txfield and it all works, but if there are equal isbn delete them all of course.
thanks to all.
Oracle inserts a hidden column called ROWNUM
DELETE FROM project.cart WHERE isbn = ? and ROWNUM=1;

Need help on oracle query

I have two oracle tables, table 1 contains students info and the second table contains student transaction details. Now I want an sql query to bring out the report of the transaction details for each student. eg student ID, name, amount, transaction date etc.
Note, a student can have many transactions, so I want a situation where by if student with ID 1 bought 3 items, in the result of the query I want to see student ID 1 and the sum of 3 items bought.
I don't want the student ID to repeat 3 times and the number of items bought.
Thanks
EDIT:
Here's the query I have so far:
select
distinct(s.spriden_id),
s.spriden_last_name,
s.spriden_first_name,
t.tbraccd_detail_code,
t.sum(tbraccd_amount),
t.tbraccd_term_code,
t.tbraccd_user,
t.TBRACCD_DATE
from SPRIDEN s, TBRACCD t
where s.spriden_pidm = t.tbraccd_pidm
and t.tbraccd_term_code = 201320
and t.tbraccd_desc = 'Misc Book Store Charges';
(The first table is SPRIDEN while the second table is TBRACCD)
You can use GROUP BY to group students, as below:
select
s.spriden_id,
sum(t.tbraccd_amount),
from SPRIDEN s, TBRACCD t
where s.spriden_pidm = t.tbraccd_pidm
and t.tbraccd_term_code = 201320
and t.tbraccd_desc = 'Misc Book Store Charges'
GROUP BY s.spriden_id;
MODIFIED VERSION to select all columns:
select
s.spriden_id,
t.tbraccd_entry_date,
t.tbraccd_term_code,
t.tbraccd_user,
sum(t.tbraccd_amount)
from SPRIDEN s, TBRACCD t
where s.spriden_pidm = t.tbraccd_pidm
and t.tbraccd_term_code = 201320
and t.tbraccd_desc = 'Misc Book Store Charges'
GROUP BY
s.spriden_id,
t.tbraccd_entry_date,
t.tbraccd_term_code,
t.tbraccd_user;

Distinct by order date and products SQL Server 2008

This is my first post here so if I've been mistaken for my post and English I say sorry :)
I already do research in google and unfortunately until now I can't solve this.
This my problem, show the total orders by date on each product and I want to distinct by products
Here's my query sample
SELECT Orders.OrderDate
,SUM(OrderDetails.OrderDetailQuantity) AS totalOrdered
,Products.ProductId
FROM Orders
INNER JOIN OrderDetails ON Orders.OrderId = OrderDetails.OrderId
INNER JOIN Products ON OrderDetails.ProductId = Products.ProductId
GROUP BY Orders.OrderId
,Orders.OrderDate
,Products.ProductId
HAVING (CONVERT(VARCHAR, Orders.OrderDate, 23) BETWEEN #from AND #to)
Now, I want to distinct by product according to the between OrderDate, I know the big problem here is the DATE but I don't have any idea on how to distinct the date by this query.
Please help me. Thank you.
P.S.: if do you want to solve this in linq expression it would be highly accepted :)
Sample data and desired results would help. When you say "distinct by" I assume you mean group by. Note, in the WHERE clause you dont need to cast Order.OrderDate if you ensure that the time component of your #from & #to params are set correctly (to include each entire day). Its never a good idea to apply a cast operation to the left side of a comparison.
SELECT --cast(Orders.OrderDate as date),
Products.ProductId
SUM(OrderDetails.OrderDetailQuantity) AS totalOrdered,
FROM Orders
INNER JOIN OrderDetails ON Orders.OrderId = OrderDetails.OrderId
INNER JOIN Products ON OrderDetails.ProductId = Products.ProductId
where Orders.OrderDate between cast(#from as date) AND cast(#to as date)
GROUP
BY --cast(Orders.OrderDate as date),
Products.ProductId
-- to illustrate:
declare #From datetime = '1/1/2000 10:30:22',
#To datetime = '1/3/2000 9:11:31'
declare #orders table (i int, dt datetime)
insert into #orders
values(1, '1/1/2000 8:00'),(2, '1/2/2000 10:00'), (3, '1/4/2000 3:00')
-- only returns i=2
select *
from #orders
where dt between #From and #To
-- returns i=1 & i=2
select *
from #orders
where dt between cast(#From as date) and cast(#To as date)

Linq MAX in Where clause in Joined table query

i'm in a situation my mind is blocked en hope someone can help me.
I have two tables. One table with customers of a subsription service and one invoice table.
these tables are not linked with keys in the database for keeping history of invoices if customers are deleted. This way I have to query the customers table joining the invoiceheader table by another unique contraint (not know by the database). This constraint is using name and address together.
An invoice is send one time a year. In the invoice-header table the date is stored when the invoice is created. In a couple of years constomers can have multiple invoices.
i'm trying to create a linq query but i'm looking the wrong way for a solution I'm afraid.
who can point me the right way?
for now i have a query :
var temp = from c in context.customer
from i in context.invoiceheader
where c.name + c.address == i.name + i.address
&& i.invoicedate < DateTime.Now.Year
select c;
With this query I get all customers who have receive an invoice last year and stil have subscribed. The trouble is with new customers who never received an invoice.
What to do for customers where in this case they haven't any invoice records.?
summurized: I want to query the last know invoice. If this invoice is older than a year (previous year)or no invoice is sent at al, i wanna retreive a list of customers the should be sent a new invoice.
I guess that what you want is a left outer join - this way you should be able to get all the customers you need:
var customers = from c in context.customer
join i in context.invoiceheader
on c.Name + c.Address equals i.Name + i.Address
into g
from row in g.DefaultIfEmpty()
where row == null ||row.invoicedate < DateTime.Now.Year
select c;

Resources