How to loop over a list of items of a table in sql - sql

i am new in sql and i want that , i have a table Customer
in which i added a new field Region now i want to do some
alteration in SQL . I want to get all records Customerand check on each
// This check will perfome for every customer let suppose we have our first customer
if currentcustomer.Country = 'US'
{
UPDATE currentcustomer SET currentcustomer.Region=currentcustomer.id
}
else
{
UPDATE currentcustomer SET currentcustomer.Region=currentcustomer.ZIP
}
this is what i want to do but i dont know that how to do this over all customers and how to do this in sql . Please Help me Thanks!!!

UPDATE Customer SET Region='AA'
WHERE Country = 'US'
UPDATE Customer SET Region='ZZ'
WHERE Country <> 'US'
Avoid looping within SQL - it is not designed for looping.
Try to formulate your requirements in set based terminology (I need to update all customers who's country is US to have region AA and all others to region ZZ).
Update:
Seeing your edit, and assuming it is sensible (compatible data types and meaningful data):
UPDATE Customer SET Region= id
WHERE Country = 'US'
UPDATE Customer SET Region= Zip
WHERE Country <> 'US'

Related

Updating table with COALESCE and JOIN PostgreSQL

My data set is a list of house sales. I am trying to update the address column so that any NULL values get replaced with an actual address. I am getting these replacement addresses from other inputs within the data. There are other sales of the same house so they share a "parcelid". I am doing an UPDATE with COALESCE and an INNER JOIN to complete the task. I am doing so with this code.
UPDATE house
SET property_address =
COALESCE(house1.property_address,house2.property_address)
FROM house AS house1
JOIN house AS house2
ON house1.parcelid = house2.parcelid
AND house1.uniqueid != house2.uniqueid
WHERE house1.property_address IS NULL
When I look at my 'house' table though, every single entry in the 'property_address' line has been updated to the same address. When I did some digging that address is the first address that occurs when I look at the join with this code.
SELECT
house1.parcelid,
house2.parcelid,
house1.property_address,
house2.property_address,
COALESCE(house1.property_address,house2.property_address)
FROM house AS house1
JOIN house AS house2
ON house1.parcelid = house2.parcelid
AND house1.uniqueid != house2.uniqueid
WHERE house1.property_address IS NULL
How to I get it to one ignore any addresses that are not null and two update to the appropriate address and not just keep copying the same address. Thanks for the help. (This is my first time asking a question here so any pointers on how to properly format would be appreciated)

Access Update Query Min Value

I'm trying to run an update query on the lowest value of an 'Rank' field. When I add in other criteria to the query, the query updates 0 records. Here's the query:
UPDATE Table_Address SET Table_Address.Priority = True
WHERE (
((Table_Address.Priority)=False) AND
((Table_Address.Order)=(SELECT MIN(Order) FROM Table_Address)) AND
((Table_Address.ID)=[Forms]![Form_UpdateAddressInfo]![ID]) AND
((Table_Address.Status)="Good" Or (Table_Address.Status)="Unknown"));
When I run the query without my criteria of: Table_Address.Priority=False and (Table_Address.Status)="Good" Or (Table_Address.Status)="Unknown"), the query will update the lowest 'Order' address. The moment I include the other criteria (ie: I want the lowest ordered address, address statuses = Good or Unknown, and an address status with a 'False' priority) it updates 0 records. I'm not too sure where I'm going wrong with this. Thanks.
If the guess in my previous comment is correct, this may be what you need:
UPDATE Table_Address
SET Table_Address.Priority = True
WHERE
Table_Address.ID=[Forms]![Form_UpdateAddressInfo]![ID]
AND Table_Address.Order=(
SELECT MIN(Order) FROM Table_Address
WHERE
Table_Address.Priority)=False
AND Table_Address.ID=[Forms]![Form_UpdateAddressInfo]![ID]
AND (Table_Address.Status="Good"
Or Table_Address.Status="Unknown")
);

Simulating For inside For in SQL

I'm using HP Vertica 7 as data warehouse DB.
I need to update two tables at the same time.
foreach (row r in table_1)
{
foreach (row r2 in table_2)
{
if(r.key1 == r2.key1 && r.key2 ==r2.key2 && r.soldQuantity > r2.Quantity)
{
updateRowInT1(); //r.Partner = r2.Partner;
updateRowInT2(); //r2.Quantity = r2.Quantity - r.soldQuantity;
break;
}
}
}
This part of code best describes what I need to do.
Is there any way to do this in SQL (with user-defined functions).
Because of changes in second table I can't use update and join. Or I can?
Also, this is part of ETL process done by Pentaho Kettle. Is perhaps way to do this inside this tool.?
Thanks
Edit:
I have changed code above.
What I need to achieve?
Regular Sales by Partner.
What's wrong right now?
When some SKU is sold in retail shop, I can see SKU LAST Partner. But, company I work for is buying same SKU from more Partners.
Slowly Changing Dimension is not applicable because when we switch partner on SKU, there is still some quantity of that SKU on shelf.
Assumption:
FIFO - first In first Out.
So, for initial matching, I need to compare (in date order) both sales and buying and to decrement bought quantity.
You probably should be thinking in a more set oriented way.
Assuming I understand what you are trying to do properly, I would do this in two update statements:
-- Update table_1's Partner based on a row in table_2.
UPDATE table_1 r
SET Partner = r2.Partner
FROM table_2 r2
WHERE r2.key1 = r.key1
AND r2.key2 = r.key2;
-- Reduce the Quantity in table_2 based on table_1's soldQuantity
UPDATE table_2 r2
SET Quantity = r2.Quantity - r.soldQuantity
FROM table_1 r
WHERE r.key1 = r2.key1
AND r.key2 = r2.key2
AND r2.Quantity >= r.soldQuantity;
-- Both of these should be done in a transaction so it is all or none. Commit the work.
COMMIT;
You can see the SQLFiddle here.

UPDATE SQL based on 2 conditions

Given the following table structure
Locations
LocationName|Easting|Northing
Incidents
LocationString|Easting|Northing|LocationName
LocationString is a badly formatted Subway Station Name that the user of the application can type any old rubbish in to. The eastings and Northings (Co-ordinates) are consistent however. Using them i can give the location a consistent name by looking those values up in a look up table.
In ACCESS SQL i would do the following
UPDATE INCIDENTS, Locations
SET Incidents.LocationName = Locations.LocationsName
WHERE Incidents.Easting = Locations.Easting
AND
Incidents.Northing = Locations.Northing
How do i accomplish the same in T-SQL?
UPDATE I
SET I.LocationName = L.LocationsName
FROM Incidents I
JOIN Locations L
ON I.Easting = L.Easting AND I.Northing = L.Northing

Updating a field within a table based on a field within the same table

HI ALL
I wish to update a row within my table from a row within that same table,
I have a table called INVENTORY. it stores PRICES for products.
I have SALES codes and I have STOCK codes which are related.
I wish to transfer all the PRICING from the SALES codes to the STOCK codes.
I wish to do something like this:
update INVENTORY
set PRICE = (SELECT PRICE WHERE CODE = "SALES CODE EQUIVALENT OF THE STOCK CODE IM IN")
WHERE
CODE = "SOME STOCK CODE"
a SALES CODE would look like this "U_12345", its STOCK code equivalent would look like "S_12345"
thanks
You're very close, you just need to specify which table you're selecting from as part of your sub-query...
update INVENTORY
set PRICE = (SELECT PRICE FROM your_table WHERE CODE = "SALES CODE EQUIVALENT OF THE STOCK CODE IM IN")
WHERE
CODE = "SOME STOCK CODE"
Even if "your_table" is INVENTORY, you still need to specify it in there.
The only time it gets 'tricky' is when your selecting from the same table that you're update, AND when you need a value from the updated record in the SELECT statement. In that case you need to differentiate between the two references, using an alias. For example...
update INVENTORY
set PRICE = (SELECT PRICE FROM INVENTORY AS [new_price] WHERE [new_price].CODE = INVENTORY.NEW_CODE)
WHERE
CODE = "SOME STOCK CODE"
UPDATE INVENTORY
SET PRICE = i_sales.PRICE
FROM INVENTORY, INVENTORY i_sales
WHERE INVENTORY.CODE LIKE 'S\_%'
AND i_sales.CODE = REPLACE(INVENTORY.Code, 'S', 'U')
This updates prices for all 'stock' records in INVENTORY with prices from the corresponding 'sales' records.
If you would prefer to update individual prices, change WHERE to something like this:
WHERE i_stock.CODE = 'S_12345'
or this:
WHERE i_stock.CODE IN ('S_12345', 'S_12346')
Note: The FROM syntax used is based on this doc page.