DB2, optimize an update query [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have this query:
update product a
set a.tsinsert = (select b.tsinsert
from h_product b
where b.tsinsert is not null and
a.product_id = b.product_id);
This query doesn't end.
Can I write it otherwise?

I used this query and it works:
merge into product a using (select distinct product_id ,TSINSERT from h_product where tsinsert is not null order by product_id ) b
on (a.product_id = b.product_id )
when matched then update set a.tsinsert = b.TSINSERT;

For this query:
update product a
set a.tsinsert = (select b.tsinsert
from h_product b
where b.tsinsert is not null and
a.product_id = b.product_id);
You want an index on h_product(product_id, tsinsert). I am surprised that the query doesn't have a where clause, because this will set a.tsinsert to NULL when there are no matches.

Related

UPDATE with condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I have a SELECT statement which return correct values, but I need to make price less by 25% for all fruits and vegetables. Is it possible to select columns I need and then update them.
SELECT product.id, product.product_title_id, product.manufacturer_id, product.price, product.comment
FROM product
JOIN product_title ON product.product_title_id = product_title.id
JOIN product_category ON product_title.product_category_id = product_category.id
WHERE product_category.name = 'fruits' OR product_category.name = 'vegetables'
not completely sure without seeing your data, but you could just use what #halfer suggested in an update statement.
UPDATE product
SET price = price*0.75
FROM product
JOIN product_title ON product.product_title_id = product_title.id
JOIN product_category ON product_title.product_category_id = product_category.id
WHERE product_category.name = 'fruits' OR product_category.name = 'vegetables'
db fiddle

Get CR_NO values for the same table in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Some can help me on this query? I need this
You can use the exists as follows:
Select cr_no, so, id
From your_table t
Where exists (select 1 from your_table tt
Where t.cr_no <> tt.cr_no
And t.so = tt.so
And t.id = tt.id)

How to Total up NULL results from JOIN Query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have written a left Join Query that returns all NULLS.
SELECT JourneyID,`TrainSeatID`, PassengerID FROM `TrainSeating`
LEFT JOIN Passenger ON TrainSeating.TrainSeatID = Passenger.PassTrainSeatID
WHERE PassengerID IS NULL;
Does anyone know how I can total up these NULLS and include the total number in the data set?
EDIT: I want to calculate the amount of NULL returns from a specific JourneyID.
Many thanks!
You can use COUNT() to get the number of rows in TrainSeating that have not passengers:
SELECT COUNT(*)
FROM TrainSeating LEFT JOIN
Passenger
ON TrainSeating.TrainSeatID = Passenger.PassTrainSeatID
WHERE PassengerID IS NULL;
EDIT:
If you want this per JourneyId then aggregate
SELECT ts.JourneyId, COUNT(*)
FROM TrainSeating ts LEFT JOIN
Passenger p
ON ts.TrainSeatID = p.PassTrainSeatID
WHERE p.PassengerID IS NULL
GROUP BY ts.JourneyId

How do I find missing data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have to find riskunits.descriptions that are missing on policies.
I can match riskunitid on policyid - but how do I find a policy that does not have a riskunit.description? I am new to SQL
Not knowing your exact table structure, we'll have to make do with some pseudo code:
select *
from policy p
where not exists (
select *
from riskunit r
where r.policyid = p.policyid
)
This will find policies with no riskunit record. If you expect there will always be a riskunit record, but the description may be null or an empty string, go with this instead:
select *
from policy p
join riskunit r
on r.policyid = p.policyid
where (r.description is null or r.description = '')

getting one to one result from one to many relationship [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
my situation is that i have one to many relation ,like order and orderdetails ,i need to get order which has single order details.
How about:
select *
from order
where order_number in
(select order_number
from order_details
group by order_number
having count(*) = 1)
SELECT O1.order_number
FROM Orders AS O1
WHERE 1 = (
SELECT COUNT(*)
FROM OrderDetails AS D1
WHERE O1.order_number = D1.order_number
);