sql can't check column1>column2 not working - sql

I have some problem in sql syntax.
i have products table and below two column
total_quantity (Its total quantity for products)
remain_alert_quantity
i want to get records that are below remain_alert_quantity
select * from products where total_quantity<remain_alert_quantity
but its not work .
can i check condition with column value in sql.
thanks and regards.

Given data is not sufficient so I am assuming few thing like,
you want to compare the product quantity with the remain_alert_quantity of same product.
There is some unique column in table (like product_id)
in this case try the below query.
select * from products a where a.total_quantity<
(select b.remain_alert_quantity from products b where b.product_id=a.product_id);

Related

How can I check if A COMPOSITE KEY doesn't exist in a table that I am appending to from a query on two other tables?

I am trying to append data from two tables in my dataBase to a third table with this code in a Microsoft Access database:
INSERT INTO percentage ( productId, productName, salesPrice, currentProvider, offerProvider, offerPrice, percentage )
SELECT products.productId AS productId, products.productName AS productName, products.pricePerUnitOrKg AS salesPrice, products.providerId AS currentProvider, productsByProviders.providerId AS offerProvider, productsByProviders.pricePerUnitOrKg AS offerPrice, Round(products.pricePerUnitOrKg/productsByProviders.pricePerUnitOrKg,3) AS percentage
FROM products INNER JOIN productsByProviders ON products.productId = productsByProviders.productId
Since the primary key in percentage is a composite key combined of the two fields: productId and offer provider, I want to make sure the row, (offer) - doesn't already exist in the percentage table with this code:
WHERE NOT Exists (SELECT 1 FROM percentage WHERE percentage.productId = productsByProvider.productId AND percentage.offerProvider = productsByProvider.providerId));
which I saw used here: How to use NOT EXISTS with COMPOSITE KEYS in SQL for inserting data from POJO
This isn't working, and I keep getting an error when trying to append to the table, which says:
And when I click yes, it doesn't append the rows that don't exist in the percentage table, which is what I want it to do.
Why is this happening?
I removed the NOT EXISTS part of the query, like June7 suggested in the comments, and it works, the error is still showing up, but for my purposes, this is enough.

How can i SUM records from a table to another after multiplying two columns

I have a table called orderItems which has two columns, quantity and unit price.It also has a foreign key ordernumber in that very table.
I have another table called ordergroup with primary key ordernumber, which contains SavedTotal column which is the order total based on quantity * unit price for all order item rows that reference that ordernumber.
Now what i struggle with is the sql query that can get all order items based on a certain ordernumber and calculate the total cost.
I have managed to do the multiplication but i am missing the SUM, here is my sql query(based on SQL Server) so far.
UPDATE OrderGroupNew
set OrderGroupNew.SavedTotal = OrderItemNew.UnitPrice*OrderItemNew.QUANTITY
FROM OrderItemNew
inner join OrderGroupNew on OrderItemNew.OrderNumber=OrderGroupNew.OrderNumber
any help is appreciated
UPDATE OrderGroupNew
SET SavedTotal = (
SELECT SUM(UnitPrice * Quantity)
FROM OrderItemNew
WHERE OrderNumber = OrderGroupNew.OrderNumber
)
You can use a TVP as well :
;With o as (
select OrderItemNew.OrderNumber as OrderNumber,
SUM(OrderItemNew.UnitPrice*OrderItemNew.QUANTITY) as OrderSum
Group by OrderItemNew.OrderNumber)
UPDATE OrderGroupNew
set OrderGroupNew.SavedTotal = o.OrderSum
FROM o
INNER JOIN OrderGroupNew on o.OrderNumber=OrderGroupNew.OrderNumber
Well the 1st answer is correct too. Choose the best in term of performance :) (dont know which will be the best, to be honest ! )

How to SQL distinct on only one column

I'm trying to apply DISTINCT on only one column.
The question is:
Who is ordering equipment where the description begins with "tennis" or "volleyball".
Include:
Customer number,
Stock number, and
Description
Do not repeat any rows.
This is what the tables look like: Items, Stock, Orders
This is my code:
select distinct
orders.customer_num, stock.stock_num, stock.description
from
orders
join
items on items.order_num = orders.order_num
join
stock on stock.stock_num = items.stock_num
where
stock.description like 'tennis%'
or stock.description like 'volleyball%';
The result is:
But I'm trying to get no repeating numbers on the CUSTOMER_NUM column.
Thank you..
There is a possibility that your join condition is wrong.please try joining item table to customer table with condition as items.customer_num =customer.customer_num .I am not sure whether it will work as we dont have correct data of these tables.
I'm not sure you'll see this, but I don't believe the rows aren't repeating. Look at the description. You're only getting one different item description per customer number which don't repeat for that customer number. You can see this by adding in: 'order by orders.customer_num, stock.stock_num;' to the end.

SQL-Oracle: Updating table column with sum of values from another table - using common key column/s criteria

Good Evening Stackoverflow team/members
Oracle version: 11g Release 11.1.0.6.0 - 64bit Production
I have two Tables: ORDERS and ITEMS.
ORDERS looks like this
ORDERS
ITEMS looks like this
enter image description here
Table ORDERS has 1 or more Order_Number each assigned to at least one depot or more. the column Total_Value SHOULD store exactly the sum of the item values associated to an order number. the table ITEMS in fact stores via parcel_code items values for a specific order_number/s.
my db has a bug for orders that have more than one depot assigned to an order number (e.g. order number 1 and 4) do not store the actual total value correctly.
in my case I cannot figure out the UPDATE statement that would select order numbers that would take the sum from the table ITEMS and link it via parcel_code and update the column total_value in the table ORDERS.
the result of my update should give this back:
for table orders i should get back for
order number 1:
for both rows total value 1120
order number 4
for both rows total value: 350
order number 2 and 3 as they are single depot order remain the same:
50 and 20
pseudo-code :
update ORDERS
set total_value = (select sum(I.item_value) from ITEMS I, ORDERS O where O.parcel_code = I.parcel_code)
i would update also those orders which have on e depot assigned as they will exactly the same.
i was looking at MERGE statements or INNER select queries. the problem i am facing is that my update must be dynamic. this means not driven by values but by columns joins as i may have to create a process that update this every day.
can someone help me?
You need to join the items and orders tables together, then select the sum of all item_values where your order_number is equal to the row which you are updating (in table o1).
update
Orders o1
set o1.total_value = (
select sum(i.item_value) from Items i
join Orders o2 on o2.parcel_code = i.parcel_code
where o2.Order_number = o1.Order_number
)

select column from child table in select clause

Is there any way in SQL server to get the child records to be displayed as the fields of the select clause of master table?
Suppose a master table Sales and a child table called Purchaseditems.
The SaleId is referenced in the Purchaseitems table.
So for a sale, there are lot of purchase items.
If so, how I can write a query to select each sales with its purchased items in the select clause?
This is a kind of dynamic column selection from child table. Is this possible in SQL server?
Not sure if i'm getting the wrong idea here, but isn't this just a simple join:
select s.saleid, p.*
from sales s, purchaseditems p
where s.salesid = p.salesid;