How can I update if value is different or empty - sql

I want to update my column if the vlaue is different from last value or its empty. I came up with this sql but it gives this error:
missing FROM-clause entry for table "box_per_pallet"
SQL:
UPDATE products AS p
SET box_per_pallet[0] = (CASE WHEN p.box_per_pallet.length = 0 THEN 0 ELSE p.box_per_pallet[0] END)
WHERE sku = 'A' AND store_id = 1

This is what I came up with based on your input. ARRAY_LENGTH takes the array and the dimension you want to check the length of as parameters. This missing from clause is because Postgres thinks that p.box_per_pallet is something other than an array and it can't find that anywhere in the query. You can't use the dot operator on arrays like p.box_per_pallet.length. It's like saying, "find the length field on table box_per_pallet in schema p".
UPDATE products
SET box_per_pallet[0] = CASE WHEN ARRAY_LENGTH(box_per_pallet, 1) = 0
OR box_per_pallet IS NULL
OR box_per_pallet[0] <> 0 -- your new value?
THEN 0
ELSE box_per_pallet[0]
END
WHERE sku = 'A'
AND store_id = 1
;
Here is a link to a dbfiddle showing the idea.

Related

Where clause with a conditional condition

Case: I have a stored procedure in where I got all the information of a table.
I have 2 parameters in order to set the Where clause but one of those could be
0.
Question: How do I do a Case When or an If in my Where clause depending on my parameter value?
I want to apply the where clause only if the value is different from 0
if is 0 I don't want to do it.
Code:
#ID_ORDER,
#ID_SUPPLIER
Select *
From Orders ord
where #ID_SUPPLIER = ord.ID_SUPPLIER
AND CASE WHEN #ID_ORDER = 0 THEN ord.ID_ORDER = #ID_ORDER END
You don't. You just use and and or:
select *
from Orders ord
where ord.ID_SUPPLIER = #ID_SUPPLIER) and
(ord.ID_ORDER = #ID_ORDER or #ID_ORDER = 0);
Note that the logic you are attempting is backwards. This only applies the filter on id_order when the value is not 0.

How to replace a value in one field by a value from another field (different column) within the same view table (SQL)?

I'd like to know if it is possible to replace a value from one field by using a value from another column and a different row.
For Example, click this to view the table image.
I'd like the SumRedeemed value 400 in row 15 to be replaced by the value -1*(-395); the value -395 comes from the EarnPointsLeft in row 6 (both have the same CID meaning that they are the same person). Any suggestions?
You need this update statement:
update t
set t.sumredeemed = (-1) * (select earnpointsleft from FifoPtsView where cid = t.cid)
from FifoPtsView t
where t.cid = 5000100008 and t.earnpointsleft = 0
This will work if the select statement will return only 1 row.
you can simply update your table
update t
set t.sumredeemed = ABS(t2.earnpointsleft )
from FifoPtsView t join FifoPtsView t2 on t.cid = t.cid and isnull(t2.earnpointsleft,0)>0
if you want negative values you can remove ABS ,
please give me your feedbacks

WHERE conditions being listed in a column if they are met

I have a file that i receive each morning which contains details of customers whos information doesnt meet certain criteria, i have built a script with many WHERE conditions that, if met, will show customers information and put them in a file but im having trouble finding out why they are wrong.
As i have many conditions in the where clause, is there a way to show which column has the incorrect information
For example i could have a table like this:
NAME|ADDRESS |PHONE|COUNTRY
John|123avenue |12345|UK
My conditions could be
SELECT * FROM CUSTOMERS
WHERE NAME LIKE 'J%'
AND LEFT(PHONE,1) = '1'
so it would show in the file as two conditions are met, but as i have over 80 rows and 40 conditions, its hard to look at each row and find out why its in their.
Is there a way i can add a column which will tell me which WHERE condition has been met?
As worded, no. You should reverse your logic. Add fields that show what's wrong, then use those fields in a WHERE clause.
SELECT
*,
CASE WHEN LEFT(phone, 1) = '1' THEN 1 ELSE 0 END AS phone_starts_with_1,
CASE WHEN LEFT(name, 1) = 'Z' THEN 1 ELSE 0 END AS name_starts_with_z
FROM
customers
WHERE
phone_starts_with_1 = 1
OR name_starts_with_z = 1
Depending on which dialect of SQL you use, you may need to nest this, such that the new fields are resolved before you can use them in the WHERE clause...
SELECT
*
FROM
(
SELECT
*,
CASE WHEN LEFT(phone, 1) = '1' THEN 1 ELSE 0 END AS phone_starts_with_1,
CASE WHEN LEFT(name, 1) = 'Z' THEN 1 ELSE 0 END AS name_starts_with_z
FROM
customers
)
checks
WHERE
phone_starts_with_1 = 1
OR name_starts_with_z = 1

SQL displaying wrong records in OR condition

I am struggling to write query for the below scenario, can any one help me please?
I have two column error and priority both are numeric fields. need to fetch based on the condition. If i select the value 3 for example from UI screen, it has to look for record in the table.
if the value match in any of the column i have to return the record i have written like this
Select *
FROM WorkItems
WHERE [System.Title] like '%defect%'
AND [System.WorkItemType] in ('Incident','bug')
AND [ErrorClass] =3
OR [Microsoft.VSTS.Common.Priority] =3
AND [Customer] = 'XYZ'
order by [System.WorkItemType]
Its giving false record and fetching all the records with priority or errorclass = 3, i need the records only workitem type = incident or bug for error class=3 or priority = 3
its returning all the records from the table.
If i put and condition like this [ErrorClass] = 3 and [Microsoft.VSTS.Common.Priority] = 3 its returning only if both the value are 3. I need all the records which match 3 along with above condition
try
Select *
FROM WorkItems
WHERE System.Title like '%defect%'
AND System.WorkItemType in ('Incident', 'bug')
AND (ErrorClass = 3 OR Microsoft.VSTS.Common.Priority = 3)
AND Customer = 'XYZ'
order by System.WorkItemType

looping through a data table vb

I'm trying to loop through a data table that has multiple values for my constraint. How can I keep the first value and add together all the other values that match my constraint.
For i = 0 To ds.Tables(0).Rows.Count - 1
If ds.Tables(0).Rows(i).Item("TEND_POS_ID") = 8 Then
'This only returns the last value
'Value 1 = 2
'Value 2 = 7.5
'should = 9.5 but it is showing 7.5
tmpCoupon = ds.Tables(0).Rows(i).Item("TENDER_AMT")
End If
Next
txtCoupon.Text = tmpCoupon
If I understand your question correctly, you need to add the values of TENDER_AMT where the value in TEND_POS_ID is 8. If this is the case you could use the Select method of the DataTable
Dim rows = ds.Tables(0).Select("TEND_POS_ID = 8")
for each r in rows
tmpCoupon += Convert.ToDecimal(r("TENDER_AMD"))
Next
However, rembember that this kind of work (SUM a column grouping by another column) is usually better resolved by the query that you submit to the database. Something like this:
SELECT TEND_POS_ID, SUM(TENDER_AMD) FROM Table1 GROUP BY TEND_POS_ID