I try running the below code and I get an error saying
Unexpected CASE
I am not sure what's wrong with the code. My original table already has 5 columns. It has the Customer id, first name, last name, debt, and accountstatus. I have a blank column called "accountstatus" which I need to fill in using a case statement.
INSERT INTO customeraccounts (accountstatus)
CASE
WHEN debt = 0 THEN "closed"
WHEN debt > 0 THEN "open"
WHEN debt < 0 THEN "refund needed"
END
To fill the accountstatus using case statement You can do it as follows :
Update customeraccounts
set accountstatus = CASE
When debt = 0 then "closed"
When debt > 0 then "open"
When debt < 0 then "refund needed"
END
Related
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.
I am made a temp table of accounts in a database with booleans that provide insight about the accounts. Some customers have multiple accounts so I am grouping them together and was trying to look at the MAX(Boolean) to set a status field.
My query kinda looks like:
with t as (Select lngCustomerNumber,
Case
When 'Criteria for being Active' Then 1
End as blnActive,
Case
When 'Criteria for unexpired' Then 1
End as blnUnexpired
From AccountTable)
Select t.CustomerNumber,
Case
When Max(t.blnActive) = 1
AND Max(t.blnUnexpired) = 1 Then 'Active/Unexpired'
When Max(t.blnActive) = 1
AND Max(t.blnUnexpired) = 0 Then 'Active/Expired'
When Max(t.blnActive) = 0
AND Max(t.blnUnexpired) = 1 Then 'Inactive/Unexpired'
When Max(t.blnActive) = 0
AND Max(t.blnUnexpired) = 0 Then 'Inactive/Expired'
End As strLicenseStatus
From T
Group By t.CustomerNumber
Anything where it checks if the Max(Boolean) = 1 will calc to True correctly, but if I do Max(Boolean) = 0 or Max(Boolean) <> 1 then it does not calc to True when it should.
I have tested by just looking at the grouped Temp Table with each boolean bringing back its Max() value and the ones that should be 0 are coming back as 0.
As a workaround, I have tried
Where t.CustomerNumber NOT IN (SELECT t2.CustomerNumber
FROM t t2
WHERE t2.blnUnexpired = 1
AND t2.CustomerNumber = t.CustomerNumber )
And that does give me the results that I am looking for but I have millions of rows coming back so it has been timing out after many hours, where the previous method was able to run in less than an hour.
I have some other data in my query, the one presented is a much smaller version used to highlight my issue.
Any recommendations on how I can make this work?
Thank you.
When you are defining your blnActive and blnUnexpired cases, you only have the "1" case defined, which means if it doesn't meet these criteria, it will be null. Try adding else 0 to each case:
with t as (Select lngCustomerNumber,
Case
When 'Criteria for being Active' Then 1
Else 0
End as blnActive,
Case
When 'Criteria for unexpired' Then 1
Else 0
End as blnUnexpired
From AccountTable)
MS SQL Server tells me 'expecting EOS' under the conditional statement starting from CASE.
Can somebody figure out what is wrong with this code?
SELECT
goal,
AVG(pledged) AS avg_num_pledged,
AVG(backers) AS avg_num_backers
CASE WHEN currency='GBP' THEN goal*1.3
ELSEIF =currency ='CAD' THEN goal*0.76
ELSEIF currency ='AUD' THEN goal*0.71
ELSEIF currency ='NOK' THEN goal*0.11
ELSEIF currency ='EUR' THEN goal*1.18
ELSEIF currency ='MXN' THEN goal*0.048
ELSEIF currency='SEK' THEN goal*0.11
ELSEIF currency='NZD' THEN goal*0.67
ELSEIF currency='CHF' THEN goal*1.11
ELSEIF currency='DKK' THEN goal*0.16
ELSEIF currency='HKD' THEN goal*0.13
ELSEIF currency='SGD' THEN goal*0.74
ELSEIF currency='JPY' THEN goal*0.0095
ELSE goal
END AS currency_uniformed
FROM kickstarter;
As indicated in the comments earlier, the CASE syntax is incorrect. You can also move the multiplication outside the CASE expression. To get a runnable query there is also a grouping clause missing to handle the aggregation (AVG()) across the goal column.
SELECT goal,
AVG(
case currency
when 'GBP' then 1.3
when 'CAD' then 0.76
when 'AUD' then 0.71
...
else 1
end * goal) AS goal_currency_uniformed,
AVG(pledged) AS avg_num_pledged,
AVG(backers) AS avg_num_backers
FROM kickstarter
GROUP BY goal;
Please consider moving the exchange rates to a separate table. This will allow you to slim this query down a bit and has the additional advantage that you can update the exchange rates without reworking your code.
SELECT ks.goal,
AVG(er.rate * ks.goal) as goal_currency_uniformed,
AVG(pledged) AS avg_num_pledged,
AVG(backers) AS avg_num_backers
FROM kickstarter ks
JOIN exchangerates er ON er.currency = ks.currency
GROUP BY goal;
Fiddle to see everything in action.
I’m running a query from a table that has info on account numbers, settled charges , the write off figure, and the reason for the write off; so the table is called group_write_off and the table headings, account_id, charge_settled_date, total_charge_amount, amount_written_off, write_off_reason. What I want to do, is pull everything through, so
Select * from
group_write_off
but for any case where the write_off_reason includes the word ‘Audit’, I want amount_written_off to show as 0 (zero), for all others i want the original amount_written_off to show.
Many thanks.
Use CASE expression.
SELECT CASE WHEN write_off_reason='Audit' THEN 0 ELSE amount_written_off END
FROM group_write_off
Also try below for checking if Audit word is present in column write_off_reason by using INSTR() function
SELECT CASE WHEN INSTR(write_off_reason,'Audit') = 0 THEN 0 ELSE amount_written_off END
FROM group_write_off
If you want more words
SELECT CASE WHEN INSTR(write_off_reason,'Audit') = 0 THEN 0
WHEN INSTR(write_off_reason,'admin') = 0 THEN 0
ELSE amount_written_off END
FROM group_write_off
I have a query that has a select statement that contains the following:
,COUNT(u.[Unit])
,up.[Number_Of_Stops]
I need to only count the units where number of stops <> 0. This has more details in the query so I can't just say WHERE number_of_stops <> 0. It has to be within the select statement.
Thanks
Try:
SUM(CASE WHEN up.[Number_Of_Stops] != 0 THEN 1 ELSE 0 END) AS countWhereNumStopsNotZero
(Edit: original answer said "COUNT" not "SUM")