Update SQL Query with where condition - sql

I added a column in the Orders table called EventId and I have to fill the column with the right value for every row.
I want to achieve it with a more complex sql query like below:
UPDATE [dbo].[Orders] o
SET o.EventId = ????
WHERE o.Id IN (SELECT o.id, s.eventid FROM orders o
INNER JOIN orderskudiscounts osd ON o.id = osd.orderid
INNER JOIN skus s ON osd.skuid = s.id GROUP BY o.id, s.eventid)
I not sure how can I write the query successfully... I have more than 2 thousand mapping to do. So I should use a query...
Thank for any help

I am guessing that you want an update like this:
UPDATE o
SET o.EventId = s.eventid
FROM orders o INNER JOIN
orderskudiscounts osd
ON o.id = osd.orderid INNER JOIN
skus s
ON osd.skuid = s.id;

Related

Problem with Ambiguous Column Name in SQL script

I am new to SQL and using this online compiler.
https://www.programiz.com/sql/online-compiler/
I have the following script and I am getting the error: Error: ambiguous column name: Customers.customer_id
UPDATE
Customers
SET
last_name = 'cow'
FROM
Customers
INNER JOIN Orders ON Customers.customer_id = Orders.customer_id
WHERE
Orders.item = 'Keyboard'
There is a customer_id column in multiple tables, but I am specifying which table to pull each column from. Why is it still saying it is ambiguous?
An UPDATE statement with a join requires an alias for the table being updated. In that case, the following will work as expected. You can also alias the Orders table if desired.
UPDATE C
SET last_name = 'cow'
FROM
Customers C
INNER JOIN Orders
ON C.customer_id = Orders.customer_id
WHERE Orders.item = 'Keyboard';
UPDATE C
SET C.Last_Name = 'cow'
FROM Customers C
INNER JOIN Orders O on O.Customer_id = C.Customer_id
WHERE O.item = 'Keyboard'
updates with joins are a little different than selects.

SQL Command not properly ended trying to join 3 tables using query

Hello I am trying to add a Translated_Name column from the Product_descriptions table to my current query that is already joining two tables however the translated_name column is a type NVARCHAR2. Should I be using Inner Join for it or am I completely wrong?
select order_mode,customer_id,product_id from ORDERS
inner join ORDER_items on order_items.ORDER_ID=Orders.ORDER_ID
where exists(select customer_id from customers where orders.customer_id=customers.customer_id)
inner join product_descriptions on product_descriptions.translated_name = Orders.Customer_id
The where clause goes after the joins:
select
order_mode,
customer_id,
product_id
from orders o
inner join order_items oi
on oi.order_id = o.order_id
inner join product_descriptions pd
on pd.translated_name = o.customer_id
where exists(
select 1
from customers c
where o.customer_id = c.customer_id
)
Notes:
table aliases make the query easier to read and write
you should qualify the columns the the from clause with the alias of the table they belong to
I am quite suspicious about the join condition on product_descriptions, which involves customer_id; you might need to review that (without knowing your table structures, it is not possible to tell what the correct condition is)

Update based on the results of previous update statement

We had a requirement to update price values in the Orders table based on the productids from the Products table. This is simple:
update o
set o.price = p.price
from Orders o
inner join Products p
on o.productid = p.productid
Then the ask was to update the price from a different table (OrderPricing) based on a different column (orderpricingid) for the orders that were not updated by the previous query.
So I have another update statement:
update o
set o.price = op.price
from Orders o
inner join OrderPricing op
on o.orderpricingid = p.orderpricingid
where o.price is null
So my question is - can the two update statements be combined together? Is there a way to update based on the results of the previous update query?
You can do both in the same query, if you change the joins from inner to left, using coalesce:
update o
set o.price = coalesce(p.price, op.price, o.price)
from Orders o
left join Products p on o.productid = p.productid
left join OrderPricing op on o.orderpricingid = p.orderpricingid
This will update the price from the products table if the product exists there, if not, it will use the price from the OrderPricing table, but if that also don't exists it will just keep the original price.
You can try this way, as if o.price is null then you will update from order price so we can use case when in time of update
update o
set o.price= case when p.price is null then op.price else p.price end
from Orders o
inner join Products p
on o.productid = p.productid
inner join OrderPricing op
on o.orderpricingid = p.orderpricingid
I believe you need a left join:
update o
set o.price = coalesce(p.price, op.price)
from Orders o left join
Products p
on o.productid = p.productid left join
OrderPricing op
on o.orderpricingid = p.orderpricingid;

Multiple table record delete query incorrect syntax error in SQL Server

I am using a SQL Server database. I want to delete multiple table records in a single query using id.
But I am getting an
Incorrect Syntax error ','
I know my query might be wrong. I need a correct query or solution for this.
I have 4 tables: Products, ProductVideos, Pictures and Inventory. I need to delete all the records in a same product id in a single query.
My query is:
DELETE FROM Products, ProductVideos, Pictures, Inventory
USING Products
INNER JOIN ProductVideos
INNER JOIN Pictures
INNER JOIN Inventory
WHERE Products.Id = ProductVideos.ProductId
AND ProductVideos.ProductId = Pictures.ProductId
AND Pictures.ProductId = Inventory.ProductId
AND Products.Id = 1
Your question was wrong.
I think you should use this solution:
delete from [dbo].[Inventory]
where ProductID in (select Id from Products where Id = 1)
delete from [dbo].[Pictures]
where ProductID in (select Id from Products where Id = 1)
delete from [dbo].[ProductVideos]
where ProductID in (select Id from Products where Id = 1)
delete from [dbo].[Products]
where Id = 1
You can't delete multiple table's data in single query like this in SQL Server.
You have to delete data from each table one by one.
DECLARE #id INT
SET #id = 1
DELETE I
FROM Products P
INNER JOIN ProductVideos PV ON P.Id = PV.ProductId
INNER JOIN Pictures PC ON PC.ProductID = PV.ID
INNER JOIN Inventory I ON I.ProductId = PC.ProductId
WHERE P.Id = #id
DELETE PC
FROM Products P
INNER JOIN ProductVideos PV ON P.Id = PV.ProductId
INNER JOIN Pictures PC ON PC.ProductID = PV.ID
WHERE P.Id = #id
DELETE PV
FROM Products P
INNER JOIN ProductVideos PV ON P.Id = PV.ProductId
WHERE P.Id = #id
DELETE P
FROM Products P
WHERE P.Id = #id
While deleting it is very important to keep it in mind the order of tables and will be better if you use TRANSACTION in query.

SQL Query on SQL Server 2008

I'm trying to get only customers that ordered both a "Gas Range" and a "Washer". I'm getting Customers who ordered a "Gas Range" and not a "Washer" and customers with both. I need the customer that meets both conditions. I'm close but a little stuck. Below is the query that I have so far. Please let me know if you need more information.
My Tables - CUSTOMER(CUST_NUM, CUST_NAME), ORDER_LINE(ORDER_NUM, PART_NUM), ORDERS(ORDER_NUM, CUST_NUM), PART(PART_NUM, PART_DESCRIPTION)
SELECT C.CUST_NAME AS [Customer(s) that ordered a Gas Range and Washer]
FROM CUSTOMER C
INNER JOIN ORDERS O
ON C.CUST_NUM = O.CUST_NUM
INNER JOIN ORDER_LINE OL
ON O.ORDER_NUM = OL.ORDER_NUM
INNER JOIN PART P
ON OL.PART_NUM = P.PART_NUM
WHERE P.PART_DESCRIPTION IN ('GasRange','Washer')
GROUP BY C.CUST_NAME
try the following
SELECT C.CUST_NAME AS [Customer(s) that ordered a Gas Range and Washer]
FROM CUSTOMER C
INNER JOIN ORDERS O
ON C.CUST_NUM = O.CUST_NUM
INNER JOIN ORDER_LINE OL
ON O.ORDER_NUM = OL.ORDER_NUM
INNER JOIN PART P
ON OL.PART_NUM = P.PART_NUM
INNER JOIN ORDERS O2
ON C.CUST_NUM = O2.CUST_NUM
INNER JOIN ORDER_LINE OL2
ON O2.ORDER_NUM = OL2.ORDER_NUM
INNER JOIN PART P2
ON OL2.PART_NUM = P2.PART_NUM
WHERE P.PART_DESCRIPTION IN ('GasRange') and P2.PART_DESCRIPTION IN ('Washer')
GROUP BY C.CUST_NAME
EDIT: Had a further look and I'm afraid that this can't be simplified in any other way than using WITH and complicated aggregate functions, which I would say would be more complicated than this - I think the other solution suggested using WITH won't work - it joins incorrectly. You definitely can't remove order line, and you have to use the order twice as well - if it was used once, it will cover only when the customer ordered it within one order, which is not what you wanted ;)
Try this...
So basically you need to join your Parts table again to ensure the same customer ordered a "Gas Range" and a "Washer". An IN, like in your current query functions as an OR therefore you are not getting the expected result.
WITH CTE AS (
SELECT DISTINCT O.CUST_NUM FROM ORDERS O
INNER JOIN ORDER_LINE OL
ON O.ORDER_NUM = OL.ORDER_NUM
INNER JOIN PART P
ON OL.PART_NUM = P.PART_NUM
INNER JOIN PART P2
ON OL.PART_NUM = P2.PART_NUM
WHERE P.PART_DESCRIPTION IN ('GasRange')
AND P2.PART_DESCRIPTION IN ('Washer')
)
SELECT C.CUST_NAME AS [Customer(s) that ordered a Gas Range and Washer]
FROM CUSTOMER C
INNER JOIN CTE O
ON C.CUST_NUM = O.CUST_NUM